Commit f09c96b8 authored by Almouhannad's avatar Almouhannad

(B) Add get all visits by patient id

parent f1c6c71b
......@@ -14,4 +14,8 @@
<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Visits\Commands\" />
</ItemGroup>
</Project>
using Application.Abstractions.CQRS.Queries;
using Domain.Repositories;
using Domain.Shared;
namespace Application.Visits.Queries.GetAllByPatientId;
public class GetAllVisitsByPatientIdHandler : IQueryHandler<GetAllVisitsByPatientIdQuery, GetAllVisitsByPatientIdResponse>
{
#region CTOR DI
private readonly IVisitsRepository _visitsRepository;
public GetAllVisitsByPatientIdHandler(IVisitsRepository visitsRepository)
{
_visitsRepository = visitsRepository;
}
#endregion
public async Task<Result<GetAllVisitsByPatientIdResponse>> Handle(GetAllVisitsByPatientIdQuery request, CancellationToken cancellationToken)
{
#region 1. Fetch data from persistence
var visitsFromPersistenceResult = await _visitsRepository.GetAllByPatientIdAsync(request.PatientId);
if (visitsFromPersistenceResult.IsFailure)
return Result.Failure<GetAllVisitsByPatientIdResponse>(visitsFromPersistenceResult.Error);
var visits = visitsFromPersistenceResult.Value;
#endregion
return GetAllVisitsByPatientIdResponse.GetResponse(visits);
}
}
using Application.Abstractions.CQRS.Queries;
namespace Application.Visits.Queries.GetAllByPatientId;
public class GetAllVisitsByPatientIdQuery : IQuery<GetAllVisitsByPatientIdResponse>
{
public int PatientId { get; set; }
}
using Domain.Entities.Visits;
using Domain.Entities.Visits.Relations.VisitMedicines;
namespace Application.Visits.Queries.GetAllByPatientId;
public class GetAllVisitsByPatientIdResponse
{
public class MedicineView
{
public string Name { get; set; } = null!;
public string Form { get; set; } = null!;
public int Amount { get; set; }
public decimal Dosage { get; set; }
public int Number { get; set; } // Number in visit
}
public class GetAllVisitsByPatientIdResponseItem
{
public string DoctorName { get; set; } = null!;
public string Diagnosis { get; set; } = null!;
public DateOnly Date { get; set; }
public ICollection<MedicineView> Medicines { get; set; } = null!;
}
public ICollection<GetAllVisitsByPatientIdResponseItem> Visits { get; set; } = null!;
public static GetAllVisitsByPatientIdResponse GetResponse(ICollection<Visit> visits)
{
List<GetAllVisitsByPatientIdResponseItem> result = new();
foreach (Visit visit in visits)
{
List<MedicineView> medicinesItem = new();
foreach (VisitMedicine visitMedicine in visit.Medicines)
{
medicinesItem.Add(new MedicineView
{
Name = visitMedicine.Medicine.Name,
Form = visitMedicine.Medicine.MedicineForm.Name,
Amount = visitMedicine.Medicine.Amount,
Dosage = visitMedicine.Medicine.Dosage,
Number = visitMedicine.Number
});
}
result.Add(new GetAllVisitsByPatientIdResponseItem
{
DoctorName = visit.Doctor.PersonalInfo.FullName,
Diagnosis = visit.Diagnosis,
Date = visit.Date,
Medicines = medicinesItem
});
}
return new GetAllVisitsByPatientIdResponse
{
Visits = result
};
}
}
using Domain.Entities.Visits;
using Domain.Repositories.Base;
using Domain.Shared;
namespace Domain.Repositories;
public interface IVisitsRepository : IRepository<Visit>
{
#region Get all by patient id
public Task<Result<ICollection<Visit>>> GetAllByPatientIdAsync(int patientId);
#endregion
}
using Domain.Entities.Visits;
using Domain.Errors;
using Domain.Repositories;
using Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.Repositories.Base;
namespace Persistence.Repositories.Visits;
public class VisitsRepository : Repositroy<Visit>, IVisitsRepository
{
#region CTOR DI For context
public VisitsRepository(ClinicsDbContext context) : base(context)
{
}
#endregion
#region Get all by patient id
public async Task<Result<ICollection<Visit>>> GetAllByPatientIdAsync(int patientId)
{
try
{
var query = _context.Set<Visit>()
.Where(visit => visit.PatientId == patientId)
.OrderByDescending(visit => visit.Date)
.Include(visit => visit.Doctor)
.ThenInclude(doctor => doctor.PersonalInfo)
.Include(visit => visit.Medicines)
.ThenInclude(visitMedicine => visitMedicine.Medicine)
.ThenInclude(medicine => medicine.MedicineForm);
var results = await query.ToListAsync();
return results;
}
catch (Exception)
{
return Result.Failure<ICollection<Visit>>(PersistenceErrors.Unknown);
}
}
#endregion
}
using Application.Doctors.Commands.ChangeStatusByUserId;
using Application.Doctors.Queries.GetAllDoctors;
using Application.Doctors.Queries.GetStatusByUserId;
using Application.Employees.Queries.GetAvailable;
using Application.Visits.Queries.GetAllByPatientId;
using Domain.Entities.Identity.UserRoles;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Presentation.Controllers.Base;
namespace Presentation.Controllers;
[Route("api/Doctors")]
public class VisitsController : ApiController
{
#region DI for MeditR sender
public VisitsController(ISender sender) : base(sender)
{
}
#endregion
//[Authorize(Roles = Roles.DoctorName)]
[HttpGet("{id:int}")]
public async Task<IActionResult> GetAllByPatientId([FromRoute(Name = "id")] int patientId)
{
GetAllVisitsByPatientIdQuery query = new GetAllVisitsByPatientIdQuery { PatientId = patientId };
var result = await _sender.Send(query);
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