namespace DocuMed.Repository.Handlers.MedicalHistories;

public class DeleteMedicalHistoryCommandHandler(IRepositoryWrapper repositoryWrapper) : IRequestHandler<DeleteMedicalHistoryCommand, Guid>
{
    public async Task<Guid> Handle(DeleteMedicalHistoryCommand template, CancellationToken cancellationToken)
    {
        var ent = await repositoryWrapper.SetRepository<MedicalHistory>().TableNoTracking
            .FirstOrDefaultAsync(m => m.Id == template.Id, cancellationToken);
        if (ent == null)
            throw new AppException("شرح حال پیدا نشد", ApiResultStatusCode.NotFound);

        repositoryWrapper.SetRepository<MedicalHistory>().Delete(ent);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return ent.Id;
    }
}