api v2.0
This commit is contained in:
@@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -1,9 +1,10 @@
|
||||
namespace Application.Endpoints.Doctors.Profile;
|
||||
|
||||
public class DoctorProfileDTO
|
||||
public class DoctorProfileUpdateDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Doctors.Profile;
|
||||
|
||||
public class DoctorProfileValidation : AbstractValidator<DoctorProfileDTO>
|
||||
public class DoctorProfileValidation : AbstractValidator<DoctorProfileUpdateDto>
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
@@ -11,30 +11,41 @@ public class DoctorProfileValidation : AbstractValidator<DoctorProfileDTO>
|
||||
{
|
||||
_doctorRepository = doctorRepository;
|
||||
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MustAsync(IsDoctorRegistered).WithMessage("Doctor is registered in system")
|
||||
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required.")
|
||||
.EmailAddress().WithMessage("Invalid email format.")
|
||||
.MustAsync(BeUniqueEmail).WithMessage("Email in use by another doctor.");
|
||||
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MustAsync(BeUniqueEmail).WithMessage("Email in use by another doctor.")
|
||||
.WithErrorCode(HttpStatusCodes.Conflict.ToString());
|
||||
|
||||
RuleFor(x => x.Password)
|
||||
.NotEmpty().WithMessage("Password is required.")
|
||||
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.");
|
||||
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.")
|
||||
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required.")
|
||||
.MinimumLength(3).WithMessage("Name must be at least 3 characters long.");
|
||||
.NotEmpty().WithMessage("Name is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
|
||||
.MinimumLength(3).WithMessage("Name must be at least 3 characters long.")
|
||||
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(3000).WithMessage("Description must not exceed 3000 characters.");
|
||||
.MaximumLength(3000).WithMessage("Description must not exceed 3000 characters.")
|
||||
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
|
||||
}
|
||||
|
||||
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var doctor = await _doctorRepository.GetByIdAsync(id);
|
||||
return doctor == null;
|
||||
}
|
||||
|
||||
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var doctor = await _doctorRepository.FindByEmailAsync(email);
|
||||
if (doctor != null)
|
||||
{
|
||||
return doctor.Email.Equals(email, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
return doctor == null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user