27 lines
1.2 KiB
C#
27 lines
1.2 KiB
C#
using MediatR;
|
|
using FnbEngine.Domain.AggregatesModel.RecipeAggregate;
|
|
|
|
namespace FnbEngine.API.Application.Queries;
|
|
|
|
public record GetRecipesByShopQuery(Guid ShopId) : IRequest<IEnumerable<RecipeDto>>;
|
|
|
|
public record RecipeDto(Guid Id, Guid ProductId, Guid ShopId, string Name, string? Instructions,
|
|
int PrepTimeMinutes, bool IsActive, DateTime CreatedAt, List<RecipeIngredientDto> Ingredients);
|
|
|
|
public record RecipeIngredientDto(Guid Id, string IngredientName, decimal Quantity, string Unit, decimal CostPerUnit);
|
|
|
|
public class GetRecipesByShopQueryHandler : IRequestHandler<GetRecipesByShopQuery, IEnumerable<RecipeDto>>
|
|
{
|
|
private readonly IRecipeRepository _repo;
|
|
public GetRecipesByShopQueryHandler(IRecipeRepository repo) => _repo = repo;
|
|
|
|
public async Task<IEnumerable<RecipeDto>> Handle(GetRecipesByShopQuery req, CancellationToken ct)
|
|
{
|
|
var recipes = await _repo.GetByShopIdAsync(req.ShopId, ct);
|
|
return recipes.Select(r => new RecipeDto(
|
|
r.Id, r.ProductId, r.ShopId, r.Name, r.Instructions, r.PrepTimeMinutes, r.IsActive, r.CreatedAt,
|
|
r.Ingredients.Select(i => new RecipeIngredientDto(i.Id, i.IngredientName, i.Quantity, i.Unit, i.CostPerUnit)).ToList()
|
|
));
|
|
}
|
|
}
|