Commit 11a6100c authored by Almouhannad's avatar Almouhannad

(B) Add get medicines with prefix query

parent 39457335
......@@ -16,6 +16,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Medicines\Commands\" />
<Folder Include="Visits\Commands\" />
</ItemGroup>
</Project>
using Application.Abstractions.CQRS.Queries;
using Domain.Repositories;
using Domain.Shared;
namespace Application.Medicines.Queries.GetAllWithPrefix;
public class GetAllMedicinesWithPrefixHandler : IQueryHandler<GetAllMedicinesWithPrefixQuery, GetAllMedicinesWithPrefixResponse>
{
#region CTOR DI
private readonly IMedicinesRepository _medicinesRepository;
public GetAllMedicinesWithPrefixHandler(IMedicinesRepository medicinesRepository)
{
_medicinesRepository = medicinesRepository;
}
#endregion
public async Task<Result<GetAllMedicinesWithPrefixResponse>> Handle(GetAllMedicinesWithPrefixQuery request, CancellationToken cancellationToken)
{
#region 1. Fetch data from persistence
var medicinesFromPersistence = await _medicinesRepository.GetAllWithPrefix(request.Prefix);
if (medicinesFromPersistence.IsFailure)
return Result.Failure<GetAllMedicinesWithPrefixResponse>(medicinesFromPersistence.Error);
var medicines = medicinesFromPersistence.Value;
#endregion
return Result.Success<GetAllMedicinesWithPrefixResponse>(
GetAllMedicinesWithPrefixResponse.GetResponse(medicines));
}
}
using Application.Abstractions.CQRS.Queries;
namespace Application.Medicines.Queries.GetAllWithPrefix;
public class GetAllMedicinesWithPrefixQuery : IQuery<GetAllMedicinesWithPrefixResponse>
{
public string? Prefix { get; set; } = null;
}
using Domain.Entities.Medicals.Medicines;
namespace Application.Medicines.Queries.GetAllWithPrefix;
public class GetAllMedicinesWithPrefixResponse
{
public class GetAllMedicinesWithPrefixResponseItem
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public string Form { get; set; } = null!;
public int Amount { get; set; }
public decimal Dosage { get; set; }
}
public ICollection<GetAllMedicinesWithPrefixResponseItem> Medicines { get; set; } = null!;
public static GetAllMedicinesWithPrefixResponse GetResponse(ICollection<Medicine> medicines)
{
List<GetAllMedicinesWithPrefixResponseItem> result = new();
foreach (Medicine medicine in medicines)
{
result.Add(new GetAllMedicinesWithPrefixResponseItem
{
Id = medicine.Id,
Name = medicine.Name,
Form = medicine.MedicineForm.Name,
Amount = medicine.Amount,
Dosage = medicine.Dosage
});
}
return new GetAllMedicinesWithPrefixResponse
{
Medicines = result
};
}
}
using Domain.Entities.Medicals.Medicines;
using Domain.Repositories.Base;
using Domain.Shared;
namespace Domain.Repositories;
public interface IMedicinesRepository : IRepository<Medicine>
{
#region Get all with prefix
public Task<Result<ICollection<Medicine>>> GetAllWithPrefix(string? prefix = null);
#endregion
}
using Domain.Entities.Medicals.Medicines;
using Domain.Errors;
using Domain.Repositories;
using Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.Repositories.Base;
namespace Persistence.Repositories.Medicines;
public class MedicinesRepository : Repositroy<Medicine>, IMedicinesRepository
{
#region CTOR DI for context
public MedicinesRepository(ClinicsDbContext context) : base(context)
{
}
#endregion
#region Get all with prefix
public async Task<Result<ICollection<Medicine>>> GetAllWithPrefix(string? prefix = null)
{
try
{
var query = _context.Set<Medicine>().AsQueryable();
if (prefix is not null)
{
query = query.Where(medicine =>
medicine.Name.ToLower().StartsWith(prefix.ToLower()));
}
query = query.OrderBy(medicine => medicine.Name)
.Include(medicine => medicine.MedicineForm);
return await query.ToListAsync();
}
catch (Exception)
{
return Result.Failure<ICollection<Medicine>>(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.Medicines.Queries.GetAllWithPrefix;
using Domain.Entities.Identity.UserRoles;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Presentation.Controllers.Base;
namespace Presentation.Controllers;
[Route("api/Medicines")]
public class MedicinesController : ApiController
{
#region DI for MeditR sender
public MedicinesController(ISender sender) : base(sender)
{
}
#endregion
//[Authorize(Roles = Roles.DoctorName)]
[HttpGet]
public async Task<IActionResult> GetAllWithPrefix([FromQuery] string? prefix = null)
{
var query = new GetAllMedicinesWithPrefixQuery { Prefix = prefix };
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