diff --git a/Application/Cqrs/Day/Commands/CreateDay/CreateDayCommand.cs b/Application/Cqrs/Day/Commands/CreateDay/CreateDayCommand.cs new file mode 100644 index 0000000..661a9f4 --- /dev/null +++ b/Application/Cqrs/Day/Commands/CreateDay/CreateDayCommand.cs @@ -0,0 +1,12 @@ +using MediatR; +using System; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Day.Commands.CreateDay; + +public class CreateDayCommand : IRequest +{ + public required DayOfWeek Index { get; set; } + public required int PairNumber { get; set; } + public required int LessonId { get; set; } + public required int GroupId { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Day/Commands/CreateDay/CreateDayCommandHandler.cs b/Application/Cqrs/Day/Commands/CreateDay/CreateDayCommandHandler.cs new file mode 100644 index 0000000..9f689f7 --- /dev/null +++ b/Application/Cqrs/Day/Commands/CreateDay/CreateDayCommandHandler.cs @@ -0,0 +1,38 @@ +using MediatR; +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Application.Common.Exceptions; +using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; +using System.Threading; +using System.Threading.Tasks; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Day.Commands.CreateDay; + +public class CreateDayCommandHandler(IDayDbContext dbContext) : IRequestHandler +{ + public async Task Handle(CreateDayCommand request, CancellationToken cancellationToken) + { + var entity = await dbContext.Days + .FirstOrDefaultAsync(d => + d.LessonId == request.LessonId + && d.PairNumber == request.PairNumber + && d.GroupId == request.GroupId + && d.Index == request.Index, + cancellationToken: cancellationToken); + + if (entity != null) + throw new RecordExistException(typeof(Domain.Schedule.Day), entity.Id); + + var day = new Domain.Schedule.Day() + { + Index = request.Index, + PairNumber = request.PairNumber, + GroupId = request.GroupId, + LessonId = request.LessonId + }; + + var result = await dbContext.Days.AddAsync(day, cancellationToken); + await dbContext.SaveChangesAsync(cancellationToken); + + return result.Entity.Id; + } +} \ No newline at end of file diff --git a/Application/Cqrs/Day/Commands/DeleteDay/DeleteDayCommand.cs b/Application/Cqrs/Day/Commands/DeleteDay/DeleteDayCommand.cs new file mode 100644 index 0000000..b826679 --- /dev/null +++ b/Application/Cqrs/Day/Commands/DeleteDay/DeleteDayCommand.cs @@ -0,0 +1,8 @@ +using MediatR; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Day.Commands.DeleteDay; + +public class DeleteDayCommand : IRequest +{ + public required int Id { get; set; } +} \ No newline at end of file diff --git a/Application/Cqrs/Day/Commands/DeleteDay/DeleteDayCommandHandler.cs b/Application/Cqrs/Day/Commands/DeleteDay/DeleteDayCommandHandler.cs new file mode 100644 index 0000000..de6d1d8 --- /dev/null +++ b/Application/Cqrs/Day/Commands/DeleteDay/DeleteDayCommandHandler.cs @@ -0,0 +1,20 @@ +using MediatR; +using Microsoft.EntityFrameworkCore; +using Mirea.Api.DataAccess.Application.Common.Exceptions; +using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; +using System.Threading; +using System.Threading.Tasks; + +namespace Mirea.Api.DataAccess.Application.Cqrs.Day.Commands.DeleteDay; + +public class DeleteDayCommandHandler(ICampusDbContext dbContext) : IRequestHandler +{ + public async Task Handle(DeleteDayCommand request, CancellationToken cancellationToken) + { + var entity = await dbContext.Campuses.FirstOrDefaultAsync(d => d.Id == request.Id, cancellationToken: cancellationToken) ?? throw new NotFoundException(typeof(Domain.Schedule.Day), nameof(Domain.Schedule.Day.Id), request.Id); + + dbContext.Campuses.Remove(entity); + + await dbContext.SaveChangesAsync(cancellationToken); + } +} \ No newline at end of file