public async Task <ApiResponse> GetAllMeals() { GetAllMealsQuery query = new GetAllMealsQuery(); var result = await _mediatr.Send(query); return(new ApiResponse("Successfully Returned All Meals", result)); }
public async Task <List <GetMealDto> > Handle(GetAllMealsQuery request, CancellationToken cancellationToken) { string sql = @"SELECT m.id as MealId, m.name as MealName, i.Id as IngredientId, i.Name as IngredientName FROM MEALS m INNER JOIN ingredients i ON i.mealid = m.id"; using SqlConnection connection = new SqlConnection(_connection.Value); var mealDic = new Dictionary <Guid, GetMealDto>(); var meals = await connection.QueryAsync <GetMealDto, IngredientGetDto, GetMealDto>(sql, (meal, ingredient) => { if (!mealDic.TryGetValue(meal.MealId, out var currentMeal)) { currentMeal = meal; mealDic.Add(currentMeal.MealId, meal); } currentMeal.Ingredients.Add(ingredient); return(currentMeal); }, splitOn : "MealId, IngredientId"); return(meals.Distinct().ToList()); }
public IReadOnlyList <Meal> Handle(GetAllMealsQuery query) { return(_context.Meals.OrderBy(m => m.Name).ToList()); }