api v2.0
This commit is contained in:
@@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user