Commit a741124b authored by Almouhannad's avatar Almouhannad

(B) Add get available doctors query

parent 90825204
using Application.Abstractions.CQRS.Queries;
using Domain.Repositories;
using Domain.Shared;
namespace Application.Employees.Queries.GetAvailable;
public class GetAvailableDoctorsHandler : IQueryHandler<GetAvailableDoctorsQuery, GetAvailableDoctorsResponse>
{
#region CTOR DI
private readonly IDoctorsRepository _doctorsRepository;
public GetAvailableDoctorsHandler(IDoctorsRepository repository)
{
_doctorsRepository = repository;
}
#endregion
public async Task<Result<GetAvailableDoctorsResponse>> Handle(GetAvailableDoctorsQuery request, CancellationToken cancellationToken)
{
#region 1. Fetch data from persistence
var doctorsFromPersistence = await _doctorsRepository.GetAvailableDoctors();
if (doctorsFromPersistence.IsFailure)
return Result.Failure<GetAvailableDoctorsResponse>(doctorsFromPersistence.Error);
var doctors = doctorsFromPersistence.Value;
#endregion
return GetAvailableDoctorsResponse.GetResponse(doctors);
}
}
using Application.Abstractions.CQRS.Queries;
namespace Application.Employees.Queries.GetAvailable;
public class GetAvailableDoctorsQuery : IQuery<GetAvailableDoctorsResponse>
{
}
using Domain.Entities.People.Doctors;
namespace Application.Employees.Queries.GetAvailable;
public class GetAvailableDoctorsResponse
{
public class GetAvailableDoctorsResponseItem
{
public int Id { get; set; }
public string Name { get; set; } = null!;
}
public ICollection<GetAvailableDoctorsResponseItem> AvailableDoctors { get; set; } = null!;
public static GetAvailableDoctorsResponse GetResponse(ICollection<Doctor> doctors)
{
List<GetAvailableDoctorsResponseItem> response = new();
foreach (var doctor in doctors)
{
var responseItem = new GetAvailableDoctorsResponseItem
{
Id = doctor.Id,
Name = doctor.PersonalInfo.FullName
};
response.Add(responseItem);
}
return new GetAvailableDoctorsResponse
{
AvailableDoctors = response
};
}
}
using Domain.Entities.People.Doctors; using Domain.Entities.People.Doctors;
using Domain.Repositories.Base; using Domain.Repositories.Base;
using Domain.Shared;
namespace Domain.Repositories; namespace Domain.Repositories;
public interface IDoctorsRepository : IRepository<Doctor> public interface IDoctorsRepository : IRepository<Doctor>
{ {
#region Get available
public Task<Result<ICollection<Doctor>>> GetAvailableDoctors();
#endregion
} }
using Domain.Entities.People.Doctors; using Domain.Entities.People.Doctors;
using Domain.Entities.People.Doctors.Shared.DoctorStatusValues;
using Domain.Errors; using Domain.Errors;
using Domain.Repositories; using Domain.Repositories;
using Domain.Shared; using Domain.Shared;
...@@ -33,4 +34,22 @@ public class DoctorsRepository : Repositroy<Doctor>, IDoctorsRepository ...@@ -33,4 +34,22 @@ public class DoctorsRepository : Repositroy<Doctor>, IDoctorsRepository
} }
#endregion #endregion
#region Get available
public async Task<Result<ICollection<Doctor>>> GetAvailableDoctors()
{
try
{
var query = _context.Set<Doctor>()
.Include(doctor => doctor.Status)
.Where(doctor => doctor.Status == DoctorStatuses.Available)
.Include(doctor => doctor.PersonalInfo);
var result = await query.ToListAsync();
return result;
}
catch (Exception)
{
return Result.Failure<ICollection<Doctor>>(PersistenceErrors.Unknown);
}
}
#endregion
} }
using Application.Doctors.Queries.GetAllDoctors; using Application.Doctors.Queries.GetAllDoctors;
using Application.Employees.Queries.GetAvailable;
using MediatR; using MediatR;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Presentation.Controllers.Base; using Presentation.Controllers.Base;
...@@ -25,4 +26,15 @@ public class DoctorsController : ApiController ...@@ -25,4 +26,15 @@ public class DoctorsController : ApiController
return HandleFailure(result); return HandleFailure(result);
return Ok(result.Value); return Ok(result.Value);
} }
//[Authorize(Roles = Roles.ReceptionistName)]
[HttpGet("Available")]
public async Task<IActionResult> GetAllAvailable()
{
var query = new GetAvailableDoctorsQuery();
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