Api-PWA/DocuMed.Repository/Handlers/Hospitals/DeleteHospitalCommandHandle...

19 lines
738 B
C#

namespace DocuMed.Repository.Handlers.Hospitals;
public class DeleteHospitalCommandHandler(IRepositoryWrapper repositoryWrapper) : IRequestHandler<DeleteHospitalCommand, Guid>
{
public async Task<Guid> Handle(DeleteHospitalCommand request, CancellationToken cancellationToken)
{
var ent = await repositoryWrapper.SetRepository<Hospital>().TableNoTracking
.FirstOrDefaultAsync(h => h.Id == request.Id, cancellationToken);
if (ent == null)
throw new BaseApiException(ApiResultStatusCode.NotFound, "Hospital not found");
repositoryWrapper.SetRepository<Hospital>().Delete(ent);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return ent.Id;
}
}