Commit f89e1817 authored by Almouhannad's avatar Almouhannad

(B) Add update doctor user commands

parent ac879c28
using Application.Abstractions.CQRS.Commands;
namespace Application.Users.Commands.UpdateDoctorPersonalInfo;
public class UpdateDoctorPersonalInfoCommand : ICommand
{
public int Id { get; set; }
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.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
namespace Application.Users.Commands.UpdateDoctorPersonalInfo;
public class UpdateDoctorPersonalInfoHandler : CommandHandlerBase<UpdateDoctorPersonalInfoCommand>
{
#region CTOR DI
private readonly IUserRepository _userRepository;
public UpdateDoctorPersonalInfoHandler(IUnitOfWork unitOfWork, IUserRepository userRepository) : base(unitOfWork)
{
_userRepository = userRepository;
}
#endregion
public override async Task<Result> HandleHelper(UpdateDoctorPersonalInfoCommand request, CancellationToken cancellationToken)
{
#region 1. Fetch user from database
var userFromDbResult = await _userRepository.GetDoctorUserByIdAsync(request.Id);
if (userFromDbResult.IsFailure)
return Result.Failure(userFromDbResult.Error);
#endregion
#region 2. Update personal info
var updateResult = userFromDbResult.Value.Doctor.PersonalInfo.UpdateDetails(
request.FirstName, request.MiddleName, request.LastName);
if (updateResult.IsFailure)
return Result.Failure(updateResult.Error);
#endregion
#region 3.Save changes
var saveChangesResult = await _userRepository.UpdateDoctorUserAsync(userFromDbResult.Value);
if (saveChangesResult.IsFailure)
return Result.Failure(saveChangesResult.Error);
#endregion
return Result.Success();
}
}
using Application.Abstractions.CQRS.Commands;
namespace Application.Users.Commands.UpdateDoctorUser;
public class UpdateDoctorUserCommand : ICommand
{
public int Id { get; set; }
public string UserName { get; set; } = null!;
public string? Password { get; set; } = null;
}
using Application.Abstractions.CQRS.Commands;
using Domain.Errors;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
namespace Application.Users.Commands.UpdateDoctorUser;
public class UpdateDoctorUserHandler : CommandHandlerBase<UpdateDoctorUserCommand>
{
#region CROR DI
private readonly IUserRepository _userRepository;
public UpdateDoctorUserHandler(IUnitOfWork unitOfWork, IUserRepository userRepository) : base(unitOfWork)
{
_userRepository = userRepository;
}
#endregion
public override async Task<Result> HandleHelper(UpdateDoctorUserCommand request, CancellationToken cancellationToken)
{
#region 1. Fetch user from Db
var userFromDbResult = await _userRepository.GetDoctorUserByIdAsync(request.Id);
if (userFromDbResult.IsFailure)
return Result.Failure(userFromDbResult.Error);
var user = userFromDbResult.Value;
#endregion
#region 2. Check unique username
var uniqueUserNameResult = await _userRepository.IsUserNameAvailableAsunc(request.UserName);
if (uniqueUserNameResult.IsFailure)
return Result.Failure(uniqueUserNameResult.Error);
if (uniqueUserNameResult.Value == false)
return Result.Failure(IdentityErrors.TakenUserName);
#endregion
#region 3. change username
var updateUserNameResult = await _userRepository.ChangeUserName(user.User, request.UserName);
if (updateUserNameResult.IsFailure)
return Result.Failure(updateUserNameResult.Error);
#endregion
#region 4. Change password
if (request.Password is not null)
{
var updatePasswordResult = await _userRepository.ChangePassword(user.User, request.Password);
if (updatePasswordResult.IsFailure)
return Result.Failure(updatePasswordResult.Error);
}
#endregion
return Result.Success();
}
}
...@@ -63,5 +63,15 @@ public sealed class User : Entity ...@@ -63,5 +63,15 @@ public sealed class User : Entity
} }
#endregion #endregion
#region Update userName
public Result UpdateUserName(string userName)
{
if (userName is null)
return Result.Failure(DomainErrors.InvalidValuesError);
UserName = userName;
return Result.Success();
}
#endregion
#endregion #endregion
} }
using Domain.Primitives; using Domain.Errors;
using Domain.Primitives;
using Domain.Shared; using Domain.Shared;
namespace Domain.Entities.People.Shared; namespace Domain.Entities.People.Shared;
...@@ -50,5 +51,17 @@ public sealed class PersonalInfo : Entity ...@@ -50,5 +51,17 @@ public sealed class PersonalInfo : Entity
} }
#endregion #endregion
#region Update details
public Result UpdateDetails(string firstName, string middleName, string lastName)
{
if (firstName is null || middleName is null || lastName is null)
return Result.Failure(DomainErrors.InvalidValuesError);
FirstName = firstName;
MiddleName = middleName;
LastName = lastName;
return Result.Success();
}
#endregion
#endregion #endregion
} }
...@@ -17,6 +17,14 @@ public interface IUserRepository : IRepository<User> ...@@ -17,6 +17,14 @@ public interface IUserRepository : IRepository<User>
public Task<Result<bool>> IsUserNameAvailableAsunc(string userName); public Task<Result<bool>> IsUserNameAvailableAsunc(string userName);
#endregion #endregion
#region Change password
public Task<Result> ChangePassword(User user, string password);
#endregion
#region Change username
public Task<Result> ChangeUserName(User user, string userName);
#endregion
#region Doctor users #region Doctor users
#region Get doctor user by user name full #region Get doctor user by user name full
...@@ -36,6 +44,10 @@ public interface IUserRepository : IRepository<User> ...@@ -36,6 +44,10 @@ public interface IUserRepository : IRepository<User>
public Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser); public Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser);
#endregion #endregion
#region Update doctor user
public Task<Result> UpdateDoctorUserAsync(DoctorUser doctorUser);
#endregion
#endregion #endregion
#region Receptionists users #region Receptionists users
......
...@@ -76,6 +76,45 @@ public class UserRepository : Repositroy<User>, IUserRepository ...@@ -76,6 +76,45 @@ public class UserRepository : Repositroy<User>, IUserRepository
} }
#endregion #endregion
#region Change password
public async Task<Result> ChangePassword(User user, string password)
{
try
{
var updatePasswordResult = user.SetHashedPassword(_passwordHasher.Hash(password));
if (updatePasswordResult.IsFailure)
return Result.Failure(updatePasswordResult.Error);
_context.Set<User>().Update(user);
await _context.SaveChangesAsync();
return Result.Success();
}
catch (Exception)
{
return Result.Failure(PersistenceErrors.UnableToCompleteTransaction);
}
}
#endregion
#region Change username
public async Task<Result> ChangeUserName(User user, string userName)
{
try
{
var updateUserNameResult = user.UpdateUserName(userName);
if (updateUserNameResult.IsFailure)
return Result.Failure(updateUserNameResult.Error);
_context.Set<User>().Update(user);
await _context.SaveChangesAsync();
return Result.Success();
}
catch (Exception)
{
return Result.Failure(PersistenceErrors.UnableToCompleteTransaction);
}
}
#endregion
#region Doctor users #region Doctor users
#region Get doctor user by user name full #region Get doctor user by user name full
...@@ -158,6 +197,22 @@ public class UserRepository : Repositroy<User>, IUserRepository ...@@ -158,6 +197,22 @@ public class UserRepository : Repositroy<User>, IUserRepository
} }
#endregion #endregion
#region Update doctor user
public async Task<Result> UpdateDoctorUserAsync(DoctorUser doctorUser)
{
try
{
_context.Set<DoctorUser>().Update(doctorUser);
await _context.SaveChangesAsync();
return Result.Success();
}
catch (Exception)
{
return Result.Failure(PersistenceErrors.UnableToCompleteTransaction);
}
}
#endregion
#endregion #endregion
#region Receptionist users #region Receptionist users
......
using Application.Users.Commands.Login; using Application.Users.Commands.Login;
using Application.Users.Commands.RegisterDoctor; using Application.Users.Commands.RegisterDoctor;
using Application.Users.Commands.RegisterReceptionist; using Application.Users.Commands.RegisterReceptionist;
using Application.Users.Commands.UpdateDoctorPersonalInfo;
using Application.Users.Commands.UpdateDoctorUser;
using Application.Users.Queries.GetAllDoctorUsers; using Application.Users.Queries.GetAllDoctorUsers;
using Application.Users.Queries.GetAllReceptionistsUsers; using Application.Users.Queries.GetAllReceptionistsUsers;
using Application.Users.Queries.GetDoctorUserById; using Application.Users.Queries.GetDoctorUserById;
...@@ -70,6 +72,27 @@ public class UsersController : ApiController ...@@ -70,6 +72,27 @@ public class UsersController : ApiController
return HandleFailure(result); return HandleFailure(result);
return Ok(result.Value); return Ok(result.Value);
} }
[Authorize(Roles = Roles.AdminName)]
[HttpPut("Doctors")]
public async Task<IActionResult> UpdateDoctorPersonalInfo([FromBody] UpdateDoctorPersonalInfoCommand command)
{
var result = await _sender.Send(command);
if (result.IsFailure)
return HandleFailure(result);
return Ok();
}
[Authorize(Roles = Roles.AdminName)]
[HttpPut("Doctors/Users")]
public async Task<IActionResult> UpdateDoctorUser([FromBody] UpdateDoctorUserCommand command)
{
var result = await _sender.Send(command);
if (result.IsFailure)
return HandleFailure(result);
return Ok();
}
#endregion #endregion
#region Receptionist #region Receptionist
......
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