Commit f228f27b authored by Almouhannad's avatar Almouhannad

(B) Add register doctor

parent d739d7b0
using Application.Abstractions.CQRS.Commands;
namespace Application.Users.Commands.RegisterDoctor;
public class RegisterDoctorCommand : ICommand<RegisterDoctorResponse>
{
public string UserName { get; set; } = null!;
public string Password { get; set; } = null!;
public string FirstName { get; set; } = null!;
public string MiddleName { get; set; } = null!;
public string LastName { get; set; } = null!;
}
using Application.Abstractions.CQRS.Commands;
using Domain.Entities.Identity.Users;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
namespace Application.Users.Commands.RegisterDoctor;
public class RegisterDoctorHandler : CommandHandlerBase<RegisterDoctorCommand, RegisterDoctorResponse>
{
#region CTOR DI
private readonly IUserRepository _userRepository;
public RegisterDoctorHandler(IUnitOfWork unitOfWork, IUserRepository userRepository) : base(unitOfWork)
{
_userRepository = userRepository;
}
#endregion
public override async Task<Result<RegisterDoctorResponse>> HandleHelper(RegisterDoctorCommand request, CancellationToken cancellationToken)
{
#region 1. Create doctor user
Result<DoctorUser> doctorUserResult = DoctorUser.Create(
request.UserName, request.Password,
request.FirstName, request.MiddleName, request.LastName
);
if (doctorUserResult.IsFailure)
return Result.Failure<RegisterDoctorResponse>(doctorUserResult.Error);
#endregion
#region 2. Register (save to DB)
var registerResult = await _userRepository.RegisterDoctorAsync(doctorUserResult.Value);
if (registerResult.IsFailure)
return Result.Failure<RegisterDoctorResponse>(registerResult.Error);
#endregion
return RegisterDoctorResponse.GetResponse(registerResult.Value);
}
}
using Domain.Entities.Identity.Users;
using Domain.Entities.People.Doctors;
using Domain.Errors;
using Domain.Shared;
namespace Application.Users.Commands.RegisterDoctor;
public class RegisterDoctorResponse
{
public int Id { get; set; }
public Doctor Doctor { get; set; } = null!;
public static Result<RegisterDoctorResponse> GetResponse(DoctorUser doctorUser)
{
if (doctorUser is null)
return Result.Failure<RegisterDoctorResponse>(IdentityErrors.NotFound);
return new RegisterDoctorResponse
{
Id = doctorUser.Id,
Doctor = doctorUser.Doctor
};
}
}
......@@ -27,6 +27,8 @@ public sealed class User : Entity
#endregion
#region Methods
#region Static factory
public static Result<User> Create(string userName, string hashedPassword, string role)
{
if (userName is null || hashedPassword is null || role is null)
......@@ -49,4 +51,17 @@ public sealed class User : Entity
return new User(0, userName, hashedPassword, selectedRole.Value);
}
#endregion
#region Set HASHED password
public Result SetHashedPassword(string hashedPassword)
{
if (hashedPassword is null)
return Result.Failure(DomainErrors.InvalidValuesError);
HashedPassword = hashedPassword;
return Result.Success();
}
#endregion
#endregion
}
......@@ -7,4 +7,5 @@ public static class IdentityErrors
public static Error InvalidRole => new("Identity.InvalidRole", "Role specified for user is invalid");
public static Error NotFound => new("Identity.NotFound", "المستخدم غير مسجّل في النظام");
public static Error PasswordMismatch => new("Identity.PasswordMismatch", "كلمة السر غير صحيحة");
public static Error UnableToRegister => new("Identity.UnableToRegister", "تعذر تسجيل المستخدم");
}
......@@ -9,8 +9,27 @@ public interface IUserRepository : IRepository<User>
public Task<Result<User>> GetByUserNameFullAsync(string userName);
#region Verify password
public Task<Result<User?>> VerifyPasswordAsync(string userName, string password);
#endregion
#region Get doctor user by user name full
public Task<Result<DoctorUser>> GetDoctorUserByUserNameFullAsync(string userName);
#endregion
#region Get receptionist user by user name full
public Task<Result<ReceptionistUser>> GetReceptionistUserByUserNameFullAsync(string userName);
#endregion
#region Register doctor
public Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser);
#endregion
#region Register receptionist
public Task<Result<ReceptionistUser>> RegisterReceptionistAsync(ReceptionistUser receptionistUser);
#endregion
}
......@@ -21,10 +21,13 @@ public class UserRepository : Repositroy<User>, IUserRepository
#endregion
#region Create method
public override Task<Result<User>> CreateAsync(User entity)
public override async Task<Result<User>> CreateAsync(User entity)
{
_context.Entry(entity.Role).State = EntityState.Unchanged;
return base.CreateAsync(entity);
var passwordResult = entity.SetHashedPassword(_passwordHasher.Hash(entity.HashedPassword));
if (passwordResult.IsFailure)
return Result.Failure<User>(passwordResult.Error);
return await base.CreateAsync(entity);
}
#endregion
......@@ -96,4 +99,49 @@ public class UserRepository : Repositroy<User>, IUserRepository
return result.First();
}
#endregion
#region Register doctor
public async Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser)
{
_context.Entry(doctorUser.User.Role).State = EntityState.Unchanged;
_context.Entry(doctorUser.Doctor.Status).State = EntityState.Unchanged;
var passwordResult = doctorUser.User.SetHashedPassword(_passwordHasher.Hash(doctorUser.User.HashedPassword));
if (passwordResult.IsFailure)
return Result.Failure<DoctorUser>(passwordResult.Error);
try
{
var createdDoctorUser = await _context.Set<DoctorUser>().AddAsync(doctorUser);
await _context.SaveChangesAsync();
return createdDoctorUser.Entity;
}
catch (Exception)
{
return Result.Failure<DoctorUser>(IdentityErrors.UnableToRegister);
}
}
#endregion
#region Register receptionist
public async Task<Result<ReceptionistUser>> RegisterReceptionistAsync(ReceptionistUser receptionistUser)
{
_context.Entry(receptionistUser.User.Role).State = EntityState.Unchanged;
var passwordResult = receptionistUser.User.SetHashedPassword(_passwordHasher.Hash(receptionistUser.User.HashedPassword));
if (passwordResult.IsFailure)
return Result.Failure<ReceptionistUser>(passwordResult.Error);
try
{
var createdreceptionistUser = await _context.Set<ReceptionistUser>().AddAsync(receptionistUser);
await _context.SaveChangesAsync();
return createdreceptionistUser.Entity;
}
catch (Exception)
{
return Result.Failure<ReceptionistUser>(IdentityErrors.UnableToRegister);
}
}
#endregion
}
using Application.Users.Commands.Login;
using Application.Users.Commands.RegisterDoctor;
using Domain.Entities.Identity.UserRoles;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Presentation.Controllers.Base;
namespace Presentation.Controllers;
......@@ -25,4 +29,16 @@ public class UsersController : ApiController
return Ok(result.Value);
}
[Authorize(Roles =Roles.AdminName)]
[HttpPost("Doctors")]
public async Task<IActionResult> RegisterDoctor([FromBody] RegisterDoctorCommand command)
{
var result = await _sender.Send(command);
if (result.IsFailure)
return HandleFailure(result);
return Ok(result.Value);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment