From f7998a17983f32bf413c09bc9c60569144ae4b08 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 8 Jan 2024 14:42:52 +0300 Subject: [PATCH 1/7] feat: add project --- Application/Application.csproj | 15 +++++++++++++++ Backend.sln | 4 +++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 Application/Application.csproj diff --git a/Application/Application.csproj b/Application/Application.csproj new file mode 100644 index 0000000..76e4455 --- /dev/null +++ b/Application/Application.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + disable + enable + Winsomnia + 1.0.0-a0 + 1.0.0.0 + 1.0.0.0 + Mirea.Api.DataAccess.Application + $(AssemblyName) + + + \ No newline at end of file diff --git a/Backend.sln b/Backend.sln index 5c85ec6..f78e7d8 100644 --- a/Backend.sln +++ b/Backend.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.34330.188 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Domain\Domain.csproj", "{C27FB5CD-6A70-4FB2-847A-847B34806902}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Domain", "Domain\Domain.csproj", "{C27FB5CD-6A70-4FB2-847A-847B34806902}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Endpoint", "Endpoint\Endpoint.csproj", "{F3A1D12E-F5B2-4339-9966-DBF869E78357}" EndProject @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Elements of the solution", README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application", "Application\Application.csproj", "{E7F0A4D4-B032-4BB9-9526-1AF688F341A4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU From 8028d400053ef02c7638fea7efc9a56c96f9adec Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 8 Jan 2024 14:43:05 +0300 Subject: [PATCH 2/7] feat: add an interface for standard saving changes --- Application/Interfaces/DbContexts/IDbContextBase.cs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Application/Interfaces/DbContexts/IDbContextBase.cs diff --git a/Application/Interfaces/DbContexts/IDbContextBase.cs b/Application/Interfaces/DbContexts/IDbContextBase.cs new file mode 100644 index 0000000..2b650ff --- /dev/null +++ b/Application/Interfaces/DbContexts/IDbContextBase.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; +using System.Threading; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts; + +public interface IDbContextBase +{ + Task SaveChangesAsync(CancellationToken cancellationToken); +} \ No newline at end of file From a389eb0a707c40a6e914d4f1aa5f76bbc98e774b Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 8 Jan 2024 14:50:21 +0300 Subject: [PATCH 3/7] feat: add an interface for working with db set --- Application/Application.csproj | 8 ++++++++ .../Interfaces/DbContexts/Schedule/ICampusDbContext.cs | 9 +++++++++ .../Interfaces/DbContexts/Schedule/IDayDbContext.cs | 9 +++++++++ .../Interfaces/DbContexts/Schedule/IFacultyDbContext.cs | 9 +++++++++ .../Interfaces/DbContexts/Schedule/IGroupDbContext.cs | 9 +++++++++ .../DbContexts/Schedule/ILectureHallDbContext.cs | 9 +++++++++ .../Interfaces/DbContexts/Schedule/ILessonDbContext.cs | 9 +++++++++ .../Schedule/ILessonToTypeOfOccupationDbContext.cs | 9 +++++++++ .../DbContexts/Schedule/IProfessorDbContext.cs | 9 +++++++++ .../DbContexts/Schedule/IProfessorToLessonDbContext.cs | 9 +++++++++ .../DbContexts/Schedule/ITypeOfOccupationDbContext.cs | 9 +++++++++ 11 files changed, 98 insertions(+) create mode 100644 Application/Interfaces/DbContexts/Schedule/ICampusDbContext.cs create mode 100644 Application/Interfaces/DbContexts/Schedule/IDayDbContext.cs create mode 100644 Application/Interfaces/DbContexts/Schedule/IFacultyDbContext.cs create mode 100644 Application/Interfaces/DbContexts/Schedule/IGroupDbContext.cs create mode 100644 Application/Interfaces/DbContexts/Schedule/ILectureHallDbContext.cs create mode 100644 Application/Interfaces/DbContexts/Schedule/ILessonDbContext.cs create mode 100644 Application/Interfaces/DbContexts/Schedule/ILessonToTypeOfOccupationDbContext.cs create mode 100644 Application/Interfaces/DbContexts/Schedule/IProfessorDbContext.cs create mode 100644 Application/Interfaces/DbContexts/Schedule/IProfessorToLessonDbContext.cs create mode 100644 Application/Interfaces/DbContexts/Schedule/ITypeOfOccupationDbContext.cs diff --git a/Application/Application.csproj b/Application/Application.csproj index 76e4455..c03a6c1 100644 --- a/Application/Application.csproj +++ b/Application/Application.csproj @@ -12,4 +12,12 @@ $(AssemblyName) + + + + + + + + \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/ICampusDbContext.cs b/Application/Interfaces/DbContexts/Schedule/ICampusDbContext.cs new file mode 100644 index 0000000..40cd23b --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/ICampusDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface ICampusDbContext : IDbContextBase +{ + DbSet Campuses { get; set; } +} \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/IDayDbContext.cs b/Application/Interfaces/DbContexts/Schedule/IDayDbContext.cs new file mode 100644 index 0000000..f34d77a --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/IDayDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface IDayDbContext : IDbContextBase +{ + DbSet Days { get; set; } +} \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/IFacultyDbContext.cs b/Application/Interfaces/DbContexts/Schedule/IFacultyDbContext.cs new file mode 100644 index 0000000..166c5cc --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/IFacultyDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface IFacultyDbContext : IDbContextBase +{ + DbSet Faculties { get; set; } +} \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/IGroupDbContext.cs b/Application/Interfaces/DbContexts/Schedule/IGroupDbContext.cs new file mode 100644 index 0000000..1fb85d7 --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/IGroupDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface IGroupDbContext : IDbContextBase +{ + DbSet Groups { get; set; } +} \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/ILectureHallDbContext.cs b/Application/Interfaces/DbContexts/Schedule/ILectureHallDbContext.cs new file mode 100644 index 0000000..04f1a37 --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/ILectureHallDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface ILectureHallDbContext : IDbContextBase +{ + DbSet LectureHalls { get; set; } +} \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/ILessonDbContext.cs b/Application/Interfaces/DbContexts/Schedule/ILessonDbContext.cs new file mode 100644 index 0000000..b29561a --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/ILessonDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface ILessonDbContext : IDbContextBase +{ + DbSet Lessons { get; set; } +} \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/ILessonToTypeOfOccupationDbContext.cs b/Application/Interfaces/DbContexts/Schedule/ILessonToTypeOfOccupationDbContext.cs new file mode 100644 index 0000000..0dacc7e --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/ILessonToTypeOfOccupationDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface ILessonToTypeOfOccupationDbContext : IDbContextBase +{ + DbSet LessonToTypeOfOccupations { get; set; } +} \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/IProfessorDbContext.cs b/Application/Interfaces/DbContexts/Schedule/IProfessorDbContext.cs new file mode 100644 index 0000000..bb4b0d7 --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/IProfessorDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface IProfessorDbContext : IDbContextBase +{ + DbSet Professors { get; set; } +} \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/IProfessorToLessonDbContext.cs b/Application/Interfaces/DbContexts/Schedule/IProfessorToLessonDbContext.cs new file mode 100644 index 0000000..ba6aba5 --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/IProfessorToLessonDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface IProfessorToLessonDbContext : IDbContextBase +{ + DbSet ProfessorToLessons { get; set; } +} \ No newline at end of file diff --git a/Application/Interfaces/DbContexts/Schedule/ITypeOfOccupationDbContext.cs b/Application/Interfaces/DbContexts/Schedule/ITypeOfOccupationDbContext.cs new file mode 100644 index 0000000..0c18dfa --- /dev/null +++ b/Application/Interfaces/DbContexts/Schedule/ITypeOfOccupationDbContext.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Domain.Schedule; + +namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; + +public interface ITypeOfOccupationDbContext : IDbContextBase +{ + DbSet TypeOfOccupations { get; set; } +} \ No newline at end of file From 924f97332fadafdea3ee021e5012134767b50d4f Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 8 Jan 2024 14:50:52 +0300 Subject: [PATCH 4/7] style: sort reference --- Application/Interfaces/DbContexts/IDbContextBase.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Application/Interfaces/DbContexts/IDbContextBase.cs b/Application/Interfaces/DbContexts/IDbContextBase.cs index 2b650ff..c06bb69 100644 --- a/Application/Interfaces/DbContexts/IDbContextBase.cs +++ b/Application/Interfaces/DbContexts/IDbContextBase.cs @@ -1,5 +1,5 @@ -using System.Threading.Tasks; -using System.Threading; +using System.Threading; +using System.Threading.Tasks; namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts; From 386272d493f1b549b2cd418d713d5834bdc428f8 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 8 Jan 2024 15:02:03 +0300 Subject: [PATCH 5/7] feat: add validation behavior --- .../Common/Behaviors/ValidationBehavior.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Application/Common/Behaviors/ValidationBehavior.cs diff --git a/Application/Common/Behaviors/ValidationBehavior.cs b/Application/Common/Behaviors/ValidationBehavior.cs new file mode 100644 index 0000000..6cd5946 --- /dev/null +++ b/Application/Common/Behaviors/ValidationBehavior.cs @@ -0,0 +1,30 @@ +using FluentValidation; +using MediatR; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace Mirea.Api.DataAccess.Application.Common.Behaviors; + +public class ValidationBehavior(IEnumerable> validators) + : IPipelineBehavior where TRequest : IRequest +{ + public Task Handle(TRequest request, + RequestHandlerDelegate next, CancellationToken cancellationToken) + { + var context = new ValidationContext(request); + var failures = validators + .Select(v => v.Validate(context)) + .SelectMany(result => result.Errors) + .Where(failure => failure != null) + .ToList(); + + if (failures.Count != 0) + { + throw new ValidationException(failures); + } + + return next(); + } +} \ No newline at end of file From cfbd847d9abb773661ea9618614916d5b9f675ac Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 8 Jan 2024 15:04:34 +0300 Subject: [PATCH 6/7] feat: add DI --- Application/DependencyInjection.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Application/DependencyInjection.cs diff --git a/Application/DependencyInjection.cs b/Application/DependencyInjection.cs new file mode 100644 index 0000000..2686e15 --- /dev/null +++ b/Application/DependencyInjection.cs @@ -0,0 +1,19 @@ +using MediatR; +using FluentValidation; +using Microsoft.Extensions.DependencyInjection; +using System.Reflection; +using Mirea.Api.DataAccess.Application.Common.Behaviors; + +namespace Mirea.Api.DataAccess.Application; + +public static class DependencyInjection +{ + public static IServiceCollection AddApplication(this IServiceCollection services) + { + services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())); + services.AddValidatorsFromAssemblies(new[] { Assembly.GetExecutingAssembly() }); + services.AddTransient(typeof(IPipelineBehavior<,>), + typeof(ValidationBehavior<,>)); + return services; + } +} \ No newline at end of file From 9e9d4e06fd85fc889a795e9272e3ccf52126718b Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Mon, 8 Jan 2024 16:11:01 +0300 Subject: [PATCH 7/7] feat: add mapping --- Application/Application.csproj | 4 +++ .../Common/Mappings/AssemblyMappingProfile.cs | 28 +++++++++++++++++++ Application/Common/Mappings/IMapWith.cs | 9 ++++++ Application/DependencyInjection.cs | 4 +-- 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 Application/Common/Mappings/AssemblyMappingProfile.cs create mode 100644 Application/Common/Mappings/IMapWith.cs diff --git a/Application/Application.csproj b/Application/Application.csproj index c03a6c1..2003517 100644 --- a/Application/Application.csproj +++ b/Application/Application.csproj @@ -13,6 +13,10 @@ + + + + diff --git a/Application/Common/Mappings/AssemblyMappingProfile.cs b/Application/Common/Mappings/AssemblyMappingProfile.cs new file mode 100644 index 0000000..68a9e8e --- /dev/null +++ b/Application/Common/Mappings/AssemblyMappingProfile.cs @@ -0,0 +1,28 @@ +using AutoMapper; +using System; +using System.Linq; +using System.Reflection; + +namespace Mirea.Api.DataAccess.Application.Common.Mappings; + +public class AssemblyMappingProfile : Profile +{ + public AssemblyMappingProfile(Assembly assembly) => + ApplyMappingsFromAssembly(assembly); + + private void ApplyMappingsFromAssembly(Assembly assembly) + { + var types = assembly.GetExportedTypes() + .Where(type => type.GetInterfaces() + .Any(i => i.IsGenericType && + i.GetGenericTypeDefinition() == typeof(IMapWith<>))) + .ToList(); + + foreach (var type in types) + { + var instance = Activator.CreateInstance(type); + var methodInfo = type.GetMethod("Mapping"); + methodInfo?.Invoke(instance, new[] { this }); + } + } +} \ No newline at end of file diff --git a/Application/Common/Mappings/IMapWith.cs b/Application/Common/Mappings/IMapWith.cs new file mode 100644 index 0000000..390e4e0 --- /dev/null +++ b/Application/Common/Mappings/IMapWith.cs @@ -0,0 +1,9 @@ +using AutoMapper; + +namespace Mirea.Api.DataAccess.Application.Common.Mappings; + +public interface IMapWith +{ + void Mapping(Profile profile) => + profile.CreateMap(typeof(T), GetType()); +} \ No newline at end of file diff --git a/Application/DependencyInjection.cs b/Application/DependencyInjection.cs index 2686e15..61c3eab 100644 --- a/Application/DependencyInjection.cs +++ b/Application/DependencyInjection.cs @@ -1,8 +1,8 @@ -using MediatR; using FluentValidation; +using MediatR; using Microsoft.Extensions.DependencyInjection; -using System.Reflection; using Mirea.Api.DataAccess.Application.Common.Behaviors; +using System.Reflection; namespace Mirea.Api.DataAccess.Application;