Files
FACULTATE-HEALTHCARE_MANAGER/backend/Application/Endpoints/Pacients/Profile/PacientProfileHandler.cs
T
ElenitaMLG 55eaa0d53a Backend Pacients, Doctors and MedicalHistory endpoints
- added for Pacients:
POST: /register
POST: /login
POST: /reset_password
PUT: /profile
DELETE: /profile

- added for Doctors:
POST: /register
POST: /login
POST: /reset_password
PUT: /profile
DELETE: /profile

- added for MedicalHistories:
POST: /:id (only by pacient) - done
GET: /:id (only by pacient) - done
PUT: /:id (only by doctor) - done
PUT /grand_access - TODO
2024-04-07 00:23:13 +03:00

121 lines
3.3 KiB
C#

using Application.Services.Database;
namespace Application.Endpoints.Pacients.Profile;
public class PacientProfileHandler
{
private readonly IPacientRepository _pacientRepository;
public PacientProfileHandler(IPacientRepository pacientRepository)
{
_pacientRepository = pacientRepository;
}
public async Task<BaseResponse> HandleGet(Guid id)
{
var pacient = await _pacientRepository.GetByIdAsync(id).ConfigureAwait(false);
if (pacient != null)
{
return new BaseResponse
{
Success = true,
Message = $"Retrieved pacient with id: {id}",
Data = pacient
};
}
return new BaseResponse
{
Success = false,
Message = $"Pacient with id: {id} not found",
Data = null
};
}
public async Task<BaseResponse> HandleGetAll()
{
var pacients = await _pacientRepository.GetAllAsync().ConfigureAwait(false);
if (pacients.Any())
{
return new BaseResponse
{
Success = true,
Message = "Retrieved pacients",
Data = pacients.ToList()
};
}
return new BaseResponse
{
Success = false,
Message = "Pacients not found",
Data = null
};
}
public async Task<BaseResponse> HandleUpdate(Guid id, PacientProfileDTO updateDto)
{
var validation = new PacientProfileValidation(_pacientRepository);
var validationResult = await validation.ValidateAsync(updateDto);
if (!validationResult.IsValid)
{
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
return new BaseResponse
{
Success = false,
Message = string.Join(", ", errorMessage),
Data = null
};
}
var pacientToUpdate = await _pacientRepository.GetByIdAsync(id);
if (pacientToUpdate == null)
{
return new BaseResponse
{
Success = false,
Message = "Pacient not found for given Id",
Data = null
};
}
pacientToUpdate.Email = updateDto.Email;
pacientToUpdate.Password = updateDto.Password;
pacientToUpdate.Name = updateDto.Name;
await _pacientRepository.UpdateAsync(pacientToUpdate);
return new BaseResponse
{
Success = true,
Message = "Pacient updated successfully",
Data = pacientToUpdate
};
}
public async Task<BaseResponse> HandleDelete(Guid id)
{
var pacientToDelete = await _pacientRepository.GetByIdAsync(id);
if (pacientToDelete == null)
{
return new BaseResponse
{
Success = false,
Message = $"Pacient with id: {id} does not exist",
Data = null
};
}
await _pacientRepository.DeleteAsync(pacientToDelete);
return new BaseResponse
{
Success = true,
Message = $"Pacient with id: {id} was succesfully deleted",
Data = pacientToDelete
};
}
}