This commit is contained in:
andrei-mihnea-cerbu
2024-04-08 00:03:24 +03:00
parent 2ccec617a5
commit b48d5ee19e
168 changed files with 2877 additions and 1146 deletions
@@ -1,32 +1,33 @@
using Application.Services.Database;
using Application.Services.HashingAlgorithms;
namespace Application.Endpoints.Doctors.Profile;
public class DoctorProfileHandler
{
private readonly IDoctorRepository _doctorRepository;
private readonly IDoctorRepository _database;
private readonly IHashingAlgorithms _hashingAlgorithms;
public DoctorProfileHandler(IDoctorRepository doctorRepository)
public DoctorProfileHandler(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms)
{
_doctorRepository = doctorRepository;
_database = database;
_hashingAlgorithms = hashingAlgorithms;
}
public async Task<BaseResponse> HandleGet(Guid id)
{
var doctor = await _doctorRepository.GetByIdAsync(id).ConfigureAwait(false);
var doctor = await _database.GetByIdAsync(id).ConfigureAwait(false);
if (doctor != null)
{
return new BaseResponse
{
Success = true,
StatusCode = HttpStatusCodes.OK,
Message = $"Retrieved doctor with id: {id}",
Data = doctor
};
}
return new BaseResponse
{
Success = false,
StatusCode = HttpStatusCodes.NotFound,
Message = $"Doctor with id: {id} not found",
Data = null
};
@@ -34,88 +35,76 @@ public class DoctorProfileHandler
public async Task<BaseResponse> HandleGetAll()
{
var doctors = await _doctorRepository.GetAllAsync().ConfigureAwait(false);
var doctors = await _database.GetAllAsync().ConfigureAwait(false);
if (doctors.Any())
{
return new BaseResponse
{
Success = true,
StatusCode = HttpStatusCodes.OK,
Message = "Retrieved doctors",
Data = doctors.ToList()
};
}
return new BaseResponse
{
Success = false,
StatusCode = HttpStatusCodes.NotFound,
Message = "Doctors not found",
Data = null
};
}
public async Task<BaseResponse> HandleUpdate(Guid id, DoctorProfileDTO updateDto)
public async Task<BaseResponse> HandleUpdate(DoctorProfileUpdateDto doctorProfileUpdateDto)
{
var validation = new DoctorProfileValidation(_doctorRepository);
var validationResult = await validation.ValidateAsync(updateDto);
var validation = new DoctorProfileValidation(_database);
var validationResult = await validation.ValidateAsync(doctorProfileUpdateDto);
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 doctorToUpdate = await _doctorRepository.GetByIdAsync(id);
var doctorToUpdate = await _database.GetByIdAsync(doctorProfileUpdateDto.Id);
doctorToUpdate.SetEmail(doctorProfileUpdateDto.Email);
doctorToUpdate.SetPassword(_hashingAlgorithms.SHA256Algorithm(doctorProfileUpdateDto.Password));
doctorToUpdate.SetName(doctorProfileUpdateDto.Name);
doctorToUpdate.SetDescription(doctorProfileUpdateDto.Description);
if (doctorToUpdate == null)
{
return new BaseResponse
{
Success = false,
Message = "Doctor not found for given Id",
Data = null
};
}
doctorToUpdate.Email = updateDto.Email;
doctorToUpdate.Password = updateDto.Password;
doctorToUpdate.Name = updateDto.Name;
doctorToUpdate.Description = updateDto.Description;
await _doctorRepository.UpdateAsync(doctorToUpdate);
await _database.UpdateAsync(doctorToUpdate);
return new BaseResponse
{
Success = true,
Message = "Doctor updated successfully",
Data = doctorToUpdate
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
public async Task<BaseResponse> HandleDelete(Guid id)
{
var doctorToDelete = await _doctorRepository.GetByIdAsync(id);
var doctorToDelete = await _database.GetByIdAsync(id);
if (doctorToDelete == null)
{
return new BaseResponse
{
Success = false,
Message = $"Doctor with id: {id} does not exist",
StatusCode = HttpStatusCodes.NotFound,
Message = "Doctor was not found.",
Data = null
};
}
await _doctorRepository.DeleteAsync(doctorToDelete);
await _database.DeleteAsync(doctorToDelete);
return new BaseResponse
{
Success = true,
Message = $"Doctor with id: {id} was succesfully deleted",
Data = doctorToDelete
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
}
}