Make a Method to avoid reduntant code and add a similar method as Getting one Shoppinglist but this time gets All Shoppinglists

This commit is contained in:
Francesco Lorenzo D'Amico
2025-05-20 15:51:18 +02:00
parent dca567ba9f
commit e59b11b32f
2 changed files with 26 additions and 11 deletions
@@ -21,5 +21,7 @@
Task<ShoppingList?> GetByIdAsync(Guid shoppingListId);
Task<int> CountAllRecipeShoppinglistsAsync();
Task<IList<ShoppingList>> GetAllShoppingListsAsync();
}
}
@@ -111,20 +111,17 @@
public async Task<ShoppingList?> GetByIdAsync(Guid shoppingListId)
{
return await _context.ShoppingLists
.Include(sl => sl.RecipeShoppingList)
.ThenInclude(rsl => rsl.Recipe)
.Include(sl => sl.RecipeShoppingList)
.ThenInclude(rsl => rsl.SelectedIngredients)
.ThenInclude(si => si.RecipeIngredient)
.ThenInclude(ri => ri.Ingredient)
.Include(sl => sl.RecipeShoppingList)
.ThenInclude(rsl => rsl.SelectedIngredients)
.ThenInclude(si => si.RecipeIngredient)
.ThenInclude(ri => ri.Unit)
return await GetShoppingListQuery()
.FirstOrDefaultAsync(sl => sl.Id == shoppingListId);
}
public async Task<IList<ShoppingList>> GetAllShoppingListsAsync()
{
return await GetShoppingListQuery()
.OrderByDescending(sl => sl.CreatedAt)
.ToListAsync();
}
public async Task<IEnumerable<RecipeIngredientShoppingList>> GetIngredientsOfRecipeInListAsync(Guid shoppingListRecipeId)
{
return await _context.RecipeIngredientsShoppingLists
@@ -189,5 +186,21 @@
return await _context.RecipeShoppingLists
.CountAsync();
}
private IQueryable<ShoppingList> GetShoppingListQuery()
{
return _context.ShoppingLists
.Include(sl => sl.RecipeShoppingList)
.ThenInclude(rsl => rsl.Recipe)
.ThenInclude(r => r.MediaFiles)
.Include(sl => sl.RecipeShoppingList)
.ThenInclude(rsl => rsl.SelectedIngredients)
.ThenInclude(si => si.RecipeIngredient)
.ThenInclude(ri => ri.Ingredient)
.Include(sl => sl.RecipeShoppingList)
.ThenInclude(rsl => rsl.SelectedIngredients)
.ThenInclude(si => si.RecipeIngredient)
.ThenInclude(ri => ri.Unit);
}
}
}