finalizare 1.0
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
namespace Application.Endpoints.MedicalHistories.CreateMedicalHistory;
|
||||
|
||||
public class CreateMedicalHistoryCommand
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
public byte[] Content { get; set; } = [];
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
using Application.Endpoints.MedicalHistories.ManageAuthorization;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using Domain.Entities;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories.CreateMedicalHistory;
|
||||
|
||||
public class CreateMedicalHistoryHandler(
|
||||
IMedicalHistoryRepository medicalHistoryRepository,
|
||||
IPatientRepository patientRepository,
|
||||
IMedicalHistoryMongoDbService medicalHistoryMongoDbService)
|
||||
{
|
||||
public async Task<BaseResponse> Handle(CreateMedicalHistoryCommand request, CancellationToken token)
|
||||
{
|
||||
var validation = new CreateMedicalHistoryValidator(patientRepository);
|
||||
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 medicalHistory = new MedicalHistory();
|
||||
medicalHistory.SetUserId(request.UserId);
|
||||
medicalHistory.SetContent(request.Content);
|
||||
|
||||
var medicalHistoryId = medicalHistory.Id;
|
||||
var newMedicalHistory = new MedicalHistoryAuthorisationModel
|
||||
{
|
||||
Id = medicalHistoryId.ToString(),
|
||||
Authorisation = new List<string>()
|
||||
};
|
||||
|
||||
await medicalHistoryMongoDbService.AddAsync(newMedicalHistory, token);
|
||||
await medicalHistoryRepository.AddAsync(medicalHistory, token);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.Created,
|
||||
Message = "Medical history record registered successfully",
|
||||
Data = medicalHistory
|
||||
};
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
using Application.Services.Database.PostgreSQL;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories.CreateMedicalHistory;
|
||||
|
||||
public class CreateMedicalHistoryValidator : AbstractValidator<CreateMedicalHistoryCommand>
|
||||
{
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public CreateMedicalHistoryValidator(IPatientRepository patientRepository)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
|
||||
RuleFor(x => x.UserId)
|
||||
.NotEmpty().WithMessage("Patient is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MustAsync(BeExistingUser).WithMessage("Specified patient doesn't exist.")
|
||||
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
|
||||
|
||||
RuleFor(x => x.Content)
|
||||
.NotEmpty().WithMessage("Description is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingUser(Guid userId, CancellationToken token)
|
||||
{
|
||||
var patient = await _patientRepository.GetByIdAsync(userId, token);
|
||||
return patient != null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user