finalizare 1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-05-21 12:10:53 +03:00
parent f7795f7519
commit 1cc1d34003
11268 changed files with 2102399 additions and 10909 deletions
@@ -0,0 +1,7 @@
namespace Application.Endpoints.MedicalHistories.CreateMedicalHistory;
public class CreateMedicalHistoryCommand
{
public Guid UserId { get; set; }
public byte[] Content { get; set; } = [];
}
@@ -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
};
}
}
@@ -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;
}
}