MireaBackend/Application/Cqrs/Day/Commands/DeleteDay/DeleteDayCommandHandler.cs
Polianin Nikita 2bb7573ca0
All checks were successful
.NET Test Pipeline / build-and-test (pull_request) Successful in 1m24s
feat: add a day command
2024-01-25 17:53:22 +03:00

20 lines
860 B
C#

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<DeleteDayCommand>
{
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);
}
}