Commit f22d57e6 authored by Almouhannad's avatar Almouhannad

(B) Add change doctor status by user id command

parent 1ac4a103
using Application.Abstractions.CQRS.Commands;
namespace Application.Doctors.Commands.ChangeStatusByUserId;
public class ChangeStatusByUserIdCommand : ICommand
{
public int UserId { get; set; }
public string Status { get; set; } = null!;
}
using Application.Abstractions.CQRS.Commands;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
namespace Application.Doctors.Commands.ChangeStatusByUserId;
public class ChangeStatusByUserIdHandler : CommandHandlerBase<ChangeStatusByUserIdCommand>
{
#region CTOR DI
private readonly IDoctorsRepository _doctorsRepository;
private readonly IUserRepository _userRepository;
public ChangeStatusByUserIdHandler(IUnitOfWork unitOfWork, IDoctorsRepository doctorsRepository, IUserRepository userRepository) : base(unitOfWork)
{
_doctorsRepository = doctorsRepository;
_userRepository = userRepository;
}
#endregion
public override async Task<Result> HandleHelper(ChangeStatusByUserIdCommand request, CancellationToken cancellationToken)
{
#region 1. Fetch user and get doctor
var doctorUserFromPersistenceResult = await _userRepository.GetDoctorUserByIdAsync(request.UserId);
if (doctorUserFromPersistenceResult.IsFailure)
return Result.Failure(doctorUserFromPersistenceResult.Error);
var doctor = doctorUserFromPersistenceResult.Value.Doctor;
#endregion
#region 2. Change status
var changeStatusResult = doctor.ChangeStatusTo(request.Status);
if (changeStatusResult.IsFailure)
return Result.Failure(changeStatusResult.Error);
#endregion
#region 3. Persist changes
var updateResult = await _doctorsRepository.UpdateAsync(doctor);
if (updateResult.IsFailure)
return Result.Failure(updateResult.Error);
#endregion
return Result.Success();
}
}
...@@ -42,7 +42,7 @@ public class SendWaitingListRecordToDoctorHandler : CommandHandlerBase<SendWaiti ...@@ -42,7 +42,7 @@ public class SendWaitingListRecordToDoctorHandler : CommandHandlerBase<SendWaiti
if (doctorFromPersistence.IsFailure) if (doctorFromPersistence.IsFailure)
return Result.Failure(doctorFromPersistence.Error); return Result.Failure(doctorFromPersistence.Error);
var doctor = doctorFromPersistence.Value; var doctor = doctorFromPersistence.Value;
doctor.ChangeStatusTo(DoctorStatuses.Working); doctor.ChangeStatusTo(DoctorStatuses.Working.Name);
var changeStatusResult = await _doctorsRepository.UpdateAsync(doctor); var changeStatusResult = await _doctorsRepository.UpdateAsync(doctor);
if (changeStatusResult.IsFailure) if (changeStatusResult.IsFailure)
return Result.Failure(changeStatusResult.Error); return Result.Failure(changeStatusResult.Error);
......
...@@ -72,14 +72,21 @@ public sealed class Doctor : Entity ...@@ -72,14 +72,21 @@ public sealed class Doctor : Entity
#endregion #endregion
#region Change status #region Change status
public Result ChangeStatusTo(DoctorStatus status) public Result ChangeStatusTo(string status)
{ {
if (status == DoctorStatuses.Available || status == DoctorStatuses.Busy || status == DoctorStatuses.Working) var Available = DoctorStatuses.Available;
{ var Busy = DoctorStatuses.Busy;
Status = status; var Working = DoctorStatuses.Working;
if (status == Available.Name)
Status = Available;
else if (status == Busy.Name)
Status = Busy;
else if (status == Working.Name)
Status = Working;
else return Result.Failure(Errors.DomainErrors.InvalidValuesError);
return Result.Success(); return Result.Success();
}
return Result.Failure(Errors.DomainErrors.InvalidValuesError);
} }
#endregion #endregion
......
using Application.Doctors.Queries.GetAllDoctors; using Application.Doctors.Commands.ChangeStatusByUserId;
using Application.Doctors.Queries.GetAllDoctors;
using Application.Doctors.Queries.GetStatusByUserId; using Application.Doctors.Queries.GetStatusByUserId;
using Application.Employees.Queries.GetAvailable; using Application.Employees.Queries.GetAvailable;
using Domain.Entities.Identity.UserRoles; using Domain.Entities.Identity.UserRoles;
...@@ -51,4 +52,14 @@ public class DoctorsController : ApiController ...@@ -51,4 +52,14 @@ public class DoctorsController : ApiController
return HandleFailure(result); return HandleFailure(result);
return Ok(result.Value); return Ok(result.Value);
} }
//[Authorize(Roles = Roles.DoctorName)]
[HttpPost("Status")]
public async Task<IActionResult> ChangeStatusByUserId([FromBody] ChangeStatusByUserIdCommand command)
{
var result = await _sender.Send(command);
if (result.IsFailure)
return HandleFailure(result);
return Ok();
}
} }
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