MireaBackend/Application/Cqrs/Day/Commands/DeleteDay/DeleteDayCommandHandler.cs

20 lines
860 B
C#
Raw Normal View History

2024-01-25 17:53:22 +03:00
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);
}
}