api v2.0
This commit is contained in:
+3
-3
@@ -1,7 +1,7 @@
|
||||
namespace Application.Endpoints.MedicalHistories;
|
||||
|
||||
public class MedicalHistoryDTO
|
||||
public class MedicalHistoryCreateDto
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
public byte[] Description { get; set; } = [];
|
||||
}
|
||||
public byte[] Content { get; set; } = [];
|
||||
}
|
||||
@@ -3,25 +3,26 @@ using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories;
|
||||
|
||||
public class MedicalHistoryCreateValidation : AbstractValidator<MedicalHistoryDTO>
|
||||
public class MedicalHistoryCreateValidation : AbstractValidator<MedicalHistoryCreateDto>
|
||||
{
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public MedicalHistoryCreateValidation(IPacientRepository pacientRepository)
|
||||
public MedicalHistoryCreateValidation(IPatientRepository patientRepository)
|
||||
{
|
||||
_pacientRepository = pacientRepository;
|
||||
_patientRepository = patientRepository;
|
||||
|
||||
RuleFor(x => x.UserId)
|
||||
.NotEmpty().WithMessage("Pacient is required.")
|
||||
.MustAsync(BeExistingUser).WithMessage("Specified pacient id doesn't exist.");
|
||||
.NotEmpty().WithMessage("Patient is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MustAsync(BeExistingUser).WithMessage("Specified patient doesn't exist.")
|
||||
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.NotEmpty().WithMessage("Description is required.");
|
||||
RuleFor(x => x.Content)
|
||||
.NotEmpty().WithMessage("Description is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingUser(Guid userId, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _pacientRepository.GetByIdAsync(userId);
|
||||
return pacient != null;
|
||||
var patient = await _patientRepository.GetByIdAsync(userId);
|
||||
return patient != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,104 +6,142 @@ namespace Application.Endpoints.MedicalHistories;
|
||||
public class MedicalHistoryHandler
|
||||
{
|
||||
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
private readonly IMongoDbService _mongoDbService;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public MedicalHistoryHandler(IMedicalHistoryRepository medicalHistoryRepository, IPacientRepository pacientRepository)
|
||||
public MedicalHistoryHandler(IMedicalHistoryRepository medicalHistoryRepository,
|
||||
IPatientRepository patientRepository, IMongoDbService mongoDbService)
|
||||
{
|
||||
_medicalHistoryRepository = medicalHistoryRepository;
|
||||
_pacientRepository = pacientRepository;
|
||||
_patientRepository = patientRepository;
|
||||
_mongoDbService = mongoDbService;
|
||||
}
|
||||
public async Task<BaseResponse> HandleGet(Guid id)
|
||||
|
||||
public async Task<BaseResponse> HandleGetAll()
|
||||
{
|
||||
var medicalHistory = await _medicalHistoryRepository.GetByIdAsync(id).ConfigureAwait(false);
|
||||
if (medicalHistory != null)
|
||||
{
|
||||
var documents = await _medicalHistoryRepository.GetAllAsync().ConfigureAwait(false);
|
||||
if (documents.Any())
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Retrieved Medical History with id: {id}",
|
||||
Data = medicalHistory
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Retrieved medical histories",
|
||||
Data = documents.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = $"Medical History with id: {id} not found",
|
||||
StatusCode = HttpStatusCodes.NotFound,
|
||||
Message = "Medical histories not found",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleCreate(Guid userId, byte[] description)
|
||||
public async Task<BaseResponse> HandleGet(Guid id)
|
||||
{
|
||||
var validation = new MedicalHistoryCreateValidation(_pacientRepository);
|
||||
var validationResult = await validation.ValidateAsync(new MedicalHistoryDTO { UserId = userId, Description = description});
|
||||
var medicalHistory = await _medicalHistoryRepository.GetByIdAsync(id).ConfigureAwait(false);
|
||||
if (medicalHistory != null)
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.OK,
|
||||
Message = "Medical history successfully retrieved",
|
||||
Data = medicalHistory
|
||||
};
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.NotFound,
|
||||
Message = "Medical history not found in system.",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleCreate(MedicalHistoryCreateDto medicalHistoryCreateDto)
|
||||
{
|
||||
var validation = new MedicalHistoryCreateValidation(_patientRepository);
|
||||
var validationResult = await validation.ValidateAsync(medicalHistoryCreateDto);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
|
||||
var firstError = validationResult.Errors.FirstOrDefault();
|
||||
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
|
||||
var errorMessage = firstError.ErrorMessage;
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = string.Join(", ", errorMessage),
|
||||
StatusCode = errorCode,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var medicalHistory = new MedicalHistory
|
||||
{
|
||||
UserId = userId,
|
||||
Description = description
|
||||
UserId = medicalHistoryCreateDto.UserId,
|
||||
Content = medicalHistoryCreateDto.Content
|
||||
};
|
||||
|
||||
await _medicalHistoryRepository.AddAsync(medicalHistory);
|
||||
|
||||
//TODO add to MongoDB
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
StatusCode = HttpStatusCodes.Created,
|
||||
Message = "Medical history record registered successfully",
|
||||
Data = medicalHistory
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleUpdate(Guid id, MedicalHistoryDTO updateDto)
|
||||
public async Task<BaseResponse> HandleUpdate(MedicalHistoryUpdateDto updateDto)
|
||||
{
|
||||
var validation = new MedicalHistoryUpdateValidation(_medicalHistoryRepository, _pacientRepository);
|
||||
var validationResult = await validation.ValidateAsync(new MedicalHistoryUpdateDTO { Id = id, UserId = updateDto.UserId, Description = updateDto.Description});
|
||||
var validation = new MedicalHistoryUpdateValidation(_medicalHistoryRepository, _patientRepository);
|
||||
var validationResult = await validation.ValidateAsync(updateDto);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
|
||||
var firstError = validationResult.Errors.FirstOrDefault();
|
||||
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
|
||||
var errorMessage = firstError.ErrorMessage;
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = string.Join(", ", errorMessage),
|
||||
StatusCode = errorCode,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var medicalHistoryToUpdate = await _medicalHistoryRepository.GetByIdAsync(id);
|
||||
|
||||
if (medicalHistoryToUpdate == null)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = "Medical history not found for given Id",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
medicalHistoryToUpdate.UserId = updateDto.UserId;
|
||||
medicalHistoryToUpdate.Description = updateDto.Description;
|
||||
var medicalHistoryToUpdate = await _medicalHistoryRepository.GetByIdAsync(updateDto.Id);
|
||||
medicalHistoryToUpdate.Content = updateDto.Content;
|
||||
|
||||
await _medicalHistoryRepository.UpdateAsync(medicalHistoryToUpdate);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Pacient updated successfully",
|
||||
Data = medicalHistoryToUpdate
|
||||
StatusCode = HttpStatusCodes.NoContent,
|
||||
Message = "Medical record updated successfully",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleDelete(Guid id)
|
||||
{
|
||||
var medicalRecord = await _medicalHistoryRepository.GetByIdAsync(id);
|
||||
if (medicalRecord == null)
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.NotFound,
|
||||
Message = "Medical record is not in system.",
|
||||
Data = null
|
||||
};
|
||||
|
||||
await _medicalHistoryRepository.DeleteAsync(medicalRecord);
|
||||
|
||||
//TODO delete from MongoDB
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.NoContent,
|
||||
Message = null,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
namespace Application.Endpoints.MedicalHistories;
|
||||
|
||||
public class MedicalHistoryUpdateDTO
|
||||
public class MedicalHistoryUpdateDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public byte[] Description { get; set; } = [];
|
||||
}
|
||||
public byte[] Content { get; set; } = [];
|
||||
}
|
||||
@@ -3,25 +3,22 @@ using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories;
|
||||
|
||||
public class MedicalHistoryUpdateValidation : AbstractValidator<MedicalHistoryUpdateDTO>
|
||||
public class MedicalHistoryUpdateValidation : AbstractValidator<MedicalHistoryUpdateDto>
|
||||
{
|
||||
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public MedicalHistoryUpdateValidation(IMedicalHistoryRepository medicalHistoryRepository, IPacientRepository pacientRepository)
|
||||
public MedicalHistoryUpdateValidation(IMedicalHistoryRepository medicalHistoryRepository,
|
||||
IPatientRepository patientRepository)
|
||||
{
|
||||
_medicalHistoryRepository = medicalHistoryRepository;
|
||||
_pacientRepository = pacientRepository;
|
||||
_patientRepository = patientRepository;
|
||||
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("Id is required")
|
||||
.MustAsync(BeExistingMedicalHistoryRecord).WithMessage("Medical hostory record does not exist");
|
||||
.MustAsync(BeExistingMedicalHistoryRecord).WithMessage("Medical history record does not exist");
|
||||
|
||||
RuleFor(x => x.UserId)
|
||||
.NotEmpty().WithMessage("Pacient is required.")
|
||||
.MustAsync(BeExistingUser).WithMessage("Specified pacient id doesn't exist.");
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
RuleFor(x => x.Content)
|
||||
.NotEmpty().WithMessage("Description is required.");
|
||||
}
|
||||
|
||||
@@ -30,10 +27,4 @@ public class MedicalHistoryUpdateValidation : AbstractValidator<MedicalHistoryUp
|
||||
var record = await _medicalHistoryRepository.GetByIdAsync(guid);
|
||||
return record != null;
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingUser(Guid userId, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _pacientRepository.GetByIdAsync(userId);
|
||||
return pacient != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user