Commit 71900f50 authored by Almouhannad's avatar Almouhannad

(B) Add send to doctor command

parent a741124b
using Application.Abstractions.CQRS.Commands;
namespace Application.WaitingList.Commands.SendWaitingListRecordToDoctor;
public class SendWaitingListRecordToDoctorCommand : ICommand
{
public int WaitingListRecordId { get; set; }
public int PatientId { get; set; }
public int DoctorId { get; set; }
}
using Application.Abstractions.CQRS.Commands;
using Application.Abstractions.Notifications.Doctors;
using Application.Abstractions.Notifications.Doctors.NewVisitNotifications;
using Domain.Entities.People.Doctors.Shared.DoctorStatusValues;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
namespace Application.WaitingList.Commands.SendWaitingListRecordToDoctor;
public class SendWaitingListRecordToDoctorHandler : CommandHandlerBase<SendWaitingListRecordToDoctorCommand>
{
#region CTOR DI
private readonly IPatientsRepository _patientsRepository;
private readonly IUserRepository _userRepository;
private readonly IDoctorsRepository _doctorsRepository;
private readonly IWaitingListRepository _waitingListRepository;
private readonly IDoctorsNotificationService _doctorsNotificationService;
public SendWaitingListRecordToDoctorHandler(IUnitOfWork unitOfWork, IPatientsRepository patientsRepository, IUserRepository userRepository, IDoctorsRepository doctorsRepository, IWaitingListRepository waitingListRepository, IDoctorsNotificationService doctorsNotificationService) : base(unitOfWork)
{
_patientsRepository = patientsRepository;
_userRepository = userRepository;
_doctorsRepository = doctorsRepository;
_waitingListRepository = waitingListRepository;
_doctorsNotificationService = doctorsNotificationService;
}
#endregion
public override async Task<Result> HandleHelper(SendWaitingListRecordToDoctorCommand request, CancellationToken cancellationToken)
{
#region 1. Delete record
var recordFromPersistence = await _waitingListRepository.GetByIdAsync(request.WaitingListRecordId);
if (recordFromPersistence.IsFailure)
return Result.Failure(recordFromPersistence.Error);
var record = recordFromPersistence.Value;
await _waitingListRepository.DeleteAsync(record);
#endregion
#region 2. Set doctor status to working
var doctorFromPersistence = await _doctorsRepository.GetByIdAsync(request.DoctorId);
if (doctorFromPersistence.IsFailure)
return Result.Failure(doctorFromPersistence.Error);
var doctor = doctorFromPersistence.Value;
doctor.ChangeStatusTo(DoctorStatuses.Working);
var changeStatusResult = await _doctorsRepository.UpdateAsync(doctor);
if (changeStatusResult.IsFailure)
return Result.Failure(changeStatusResult.Error);
#endregion
#region 3. Get doctor user
var doctorUserFromPersistence = await _doctorsRepository.GetUserByIdAsync(request.DoctorId);
if (doctorUserFromPersistence.IsFailure)
return Result.Failure(doctorUserFromPersistence.Error);
var doctorUser = doctorUserFromPersistence.Value;
#endregion
#region 4. Get patient
var patientFromPersistence = await _patientsRepository.GetByIdFullAsync(request.PatientId);
if (patientFromPersistence.IsFailure)
return Result.Failure(patientFromPersistence.Error);
var patient = patientFromPersistence.Value;
#endregion
#region 5. Send notification
var notification = NewVisitNotification.Create(request.PatientId, patient.PersonalInfo.FirstName, request.DoctorId, doctorUser.Id);
await _doctorsNotificationService.SendNewVisitNotification(notification);
#endregion
return Result.Success();
}
}
using Domain.Entities.People.Doctors;
using Domain.Entities.Identity.Users;
using Domain.Entities.People.Doctors;
using Domain.Repositories.Base;
using Domain.Shared;
......@@ -9,4 +10,8 @@ public interface IDoctorsRepository : IRepository<Doctor>
#region Get available
public Task<Result<ICollection<Doctor>>> GetAvailableDoctors();
#endregion
#region Get user
public Task<Result<DoctorUser>> GetUserByIdAsync(int id);
#endregion
}
using Domain.Entities.People.Doctors;
using Domain.Entities.Identity.Users;
using Domain.Entities.People.Doctors;
using Domain.Entities.People.Doctors.Shared.DoctorStatusValues;
using Domain.Errors;
using Domain.Repositories;
......@@ -34,6 +35,14 @@ public class DoctorsRepository : Repositroy<Doctor>, IDoctorsRepository
}
#endregion
#region Update method
public override Task<Result> UpdateAsync(Doctor entity)
{
_context.Entry(entity.Status).State = EntityState.Unchanged;
return base.UpdateAsync(entity);
}
#endregion
#region Get available
public async Task<Result<ICollection<Doctor>>> GetAvailableDoctors()
{
......@@ -52,4 +61,24 @@ public class DoctorsRepository : Repositroy<Doctor>, IDoctorsRepository
}
}
#endregion
#region Get user
public async Task<Result<DoctorUser>> GetUserByIdAsync(int id)
{
try
{
var query = _context.Set<DoctorUser>()
.Include(doctorUser => doctorUser.Doctor)
.Where(doctorUser => doctorUser.Doctor.Id == id)
.Include(doctorUser => doctorUser.User);
var result = await query.FirstAsync();
return result;
}
catch (Exception)
{
return Result.Failure<DoctorUser>(PersistenceErrors.NotFound);
}
}
#endregion
}
......@@ -2,6 +2,7 @@
using Application.Employees.Commands.CreateEmployee;
using Application.WaitingList.Commands.CreateWaitingListRecord;
using Application.WaitingList.Commands.DeleteWaitingListRecord;
using Application.WaitingList.Commands.SendWaitingListRecordToDoctor;
using Application.WaitingList.Queries;
using Domain.Entities.Identity.UserRoles;
using MediatR;
......@@ -56,4 +57,13 @@ public class WaitingListController : ApiController
return Ok();
}
[HttpPost("SendToDoctor")]
public async Task<IActionResult> SendToDoctor([FromBody] SendWaitingListRecordToDoctorCommand 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