feat: add a day command
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m24s

This commit is contained in:
2024-01-25 17:53:22 +03:00
parent c82b29e90e
commit 2bb7573ca0
4 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,12 @@
using MediatR;
using System;
namespace Mirea.Api.DataAccess.Application.Cqrs.Day.Commands.CreateDay;
public class CreateDayCommand : IRequest<int>
{
public required DayOfWeek Index { get; set; }
public required int PairNumber { get; set; }
public required int LessonId { get; set; }
public required int GroupId { get; set; }
}

View File

@ -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<CreateDayCommand, int>
{
public async Task<int> 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;
}
}