Commit 85e98ac3 authored by Almouhannad's avatar Almouhannad

(B) Add create visit command

parent 11a6100c
......@@ -17,6 +17,5 @@
<ItemGroup>
<Folder Include="Medicines\Commands\" />
<Folder Include="Visits\Commands\" />
</ItemGroup>
</Project>
......@@ -29,6 +29,8 @@ public class ChangeStatusByUserIdHandler : CommandHandlerBase<ChangeStatusByUser
#endregion
#region 2. Change status
if (request.Status == doctor.Status.Name)
return Result.Success();
var changeStatusResult = doctor.ChangeStatusTo(request.Status);
if (changeStatusResult.IsFailure)
return Result.Failure(changeStatusResult.Error);
......
using Application.Abstractions.CQRS.Commands;
namespace Application.Visits.Commands.Create;
public class CreateVisitCommand : ICommand
{
public class CreateVisitCommandItem
{
public int MedicineId { get; set; }
public int Number { get; set; }
}
public int DoctorUserId { get; set; }
public int PatientId { get; set; }
public string Diagnosis { get; set; } = null!;
public ICollection<CreateVisitCommandItem> Medicines { get; set; } = null!;
}
using Application.Abstractions.CQRS.Commands;
using Domain.Entities.Visits;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
namespace Application.Visits.Commands.Create;
public class CreateVisitCommandHandler : CommandHandlerBase<CreateVisitCommand>
{
#region CTOR DI
private readonly IUserRepository _userRepository;
private readonly IVisitsRepository _visitsRepository;
public CreateVisitCommandHandler(IUnitOfWork unitOfWork, IUserRepository userRepository, IVisitsRepository visitsRepository) : base(unitOfWork)
{
_userRepository = userRepository;
_visitsRepository = visitsRepository;
}
#endregion
public override async Task<Result> HandleHelper(CreateVisitCommand request, CancellationToken cancellationToken)
{
#region 1. Fetch doctor from persistence
var doctorFromPersistenceResult = await _userRepository.GetDoctorByDoctorUserIdAsync(request.DoctorUserId);
if (doctorFromPersistenceResult.IsFailure)
return Result.Failure(doctorFromPersistenceResult.Error);
var doctorId = doctorFromPersistenceResult.Value.Id;
#endregion
#region 2. Create visit
var createVisitResult = Visit.Create(request.PatientId, doctorId,
DateOnly.FromDateTime(DateTime.Today),request.Diagnosis);
if (createVisitResult.IsFailure)
return Result.Failure(createVisitResult.Error);
var saveVisitToPersistenceResult = await _visitsRepository.CreateAsync(createVisitResult.Value);
if (saveVisitToPersistenceResult.IsFailure)
return Result.Failure(saveVisitToPersistenceResult.Error);
var visit = saveVisitToPersistenceResult.Value;
#endregion
#region 3. Add medicines to visit
foreach (var medicine in request.Medicines)
{
var addResult = visit.AddMedicine(medicine.MedicineId, medicine.Number);
if (addResult.IsFailure)
return Result.Failure(addResult.Error);
}
#endregion
#region 4. Save visit to persistence
var saveResult = await _visitsRepository.UpdateAsync(visit);
if (saveResult.IsFailure)
return Result.Failure(saveResult.Error);
#endregion
return Result.Success();
}
}
......@@ -140,18 +140,18 @@ public sealed class Visit : Entity
#endregion
#region Add medicine
public Result AddMedicine(Medicine medicine, int number)
public Result AddMedicine(int medicineId, int number)
{
#region Create medicine to attach
Result<VisitMedicine> entry = VisitMedicine.Create(Id, medicine.Id, number);
Result<VisitMedicine> entry = VisitMedicine.Create(Id, medicineId, number);
if (entry.IsFailure)
return Result.Failure(entry.Error);
#endregion
#region Check duplicate
if (Medicines.Where(m => m.Medicine == medicine).ToList().Count > 0)
return Result.Failure(Errors.DomainErrors.VisitAlreadyHasThisMedicine);
#endregion
//#region Check duplicate
//if (Medicines.Where(m => m.Medicine.Id == medicineId).ToList().Count > 0)
// return Result.Failure(Errors.DomainErrors.VisitAlreadyHasThisMedicine);
//#endregion
_medicines.Add(entry.Value);
return Result.Success();
......
using Domain.Entities.Identity.Users;
using Domain.Entities.People.Doctors;
using Domain.Repositories.Base;
using Domain.Shared;
......@@ -48,6 +49,10 @@ public interface IUserRepository : IRepository<User>
public Task<Result> UpdateDoctorUserAsync(DoctorUser doctorUser);
#endregion
#region Get Doctor by doctor user
public Task<Result<Doctor>> GetDoctorByDoctorUserIdAsync(int doctorUserId);
#endregion
#endregion
#region Receptionists users
......
using Domain.Entities.Identity.Users;
using Domain.Entities.People.Doctors;
using Domain.Errors;
using Domain.Repositories;
using Domain.Shared;
......@@ -215,6 +216,25 @@ public class UserRepository : Repositroy<User>, IUserRepository
}
#endregion
#region Get Doctor by doctor user
public async Task<Result<Doctor>> GetDoctorByDoctorUserIdAsync(int doctorUserId)
{
try
{
var query = _context.Set<DoctorUser>()
.Where(doctorUser => doctorUser.Id == doctorUserId)
.Include(doctorUser => doctorUser.Doctor);
var result = await query.FirstAsync();
return result.Doctor;
}
catch (Exception)
{
return Result.Failure<Doctor>(PersistenceErrors.NotFound);
}
}
#endregion
#endregion
#region Receptionist users
......
......@@ -2,6 +2,7 @@
using Application.Doctors.Queries.GetAllDoctors;
using Application.Doctors.Queries.GetStatusByUserId;
using Application.Employees.Queries.GetAvailable;
using Application.Visits.Commands.Create;
using Application.Visits.Queries.GetAllByPatientId;
using Domain.Entities.Identity.UserRoles;
using MediatR;
......@@ -32,4 +33,14 @@ public class VisitsController : ApiController
return Ok(result.Value);
}
//[Authorize(Roles = Roles.DoctorName)]
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreateVisitCommand 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