finalizare 1.0
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
namespace Application.Endpoints.MedicalHistories.ModifyMedicalHistory;
|
||||
|
||||
public class ModifyMedicalHistoryCommand
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public byte[] Content { get; set; } = [];
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories.ModifyMedicalHistory;
|
||||
|
||||
public class ModifyMedicalHistoryHandler(IMedicalHistoryRepository medicalHistoryRepository)
|
||||
{
|
||||
public async Task<BaseResponse> Handle(ModifyMedicalHistoryCommand request, CancellationToken token)
|
||||
{
|
||||
var validation = new ModifyMedicalHistoryValidator(medicalHistoryRepository);
|
||||
var validationResult = await validation.ValidateAsync(request, token);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var firstError = validationResult.Errors.FirstOrDefault();
|
||||
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
|
||||
var errorMessage = firstError.ErrorMessage;
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = errorCode,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var newMedicalHistory = await medicalHistoryRepository.GetByIdAsync(request.Id, token);
|
||||
newMedicalHistory.SetContent(request.Content);
|
||||
|
||||
await medicalHistoryRepository.UpdateAsync(newMedicalHistory, token);
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.NoContent,
|
||||
Message = "Medical record updated successfully",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories.ModifyMedicalHistory;
|
||||
|
||||
public class ModifyMedicalHistoryValidator : AbstractValidator<ModifyMedicalHistoryCommand>
|
||||
{
|
||||
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
|
||||
|
||||
public ModifyMedicalHistoryValidator(IMedicalHistoryRepository medicalHistoryRepository)
|
||||
{
|
||||
_medicalHistoryRepository = medicalHistoryRepository;
|
||||
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("Id is required")
|
||||
.MustAsync(BeExistingMedicalHistoryRecord).WithMessage("Medical history record does not exist");
|
||||
|
||||
RuleFor(x => x.Content)
|
||||
.NotEmpty().WithMessage("Description is required.");
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingMedicalHistoryRecord(Guid id, CancellationToken token)
|
||||
{
|
||||
var record = await _medicalHistoryRepository.GetByIdAsync(id, token);
|
||||
return record != null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user