Initial commit
This commit is contained in:
@@ -32,14 +32,52 @@
|
||||
return BadRequest("No ingredients provided.");
|
||||
}
|
||||
|
||||
var shoppingList = await _shoppingListRepository.AddIngredientsToShoppingListAsync(request.RecipeId, request.IngredientIds);
|
||||
// Compute duplicates for this recipe in the current shopping list
|
||||
var existingShoppingList = await _context.ShoppingLists
|
||||
.Include(sl => sl.RecipeShoppingList)
|
||||
.ThenInclude(rsl => rsl.SelectedIngredients)
|
||||
.ThenInclude(si => si.RecipeIngredient)
|
||||
.Include(sl => sl.RecipeShoppingList)
|
||||
.ThenInclude(rsl => rsl.Recipe)
|
||||
.FirstOrDefaultAsync(sl => sl.RecipeShoppingList.Any(rsl => rsl.Recipe.Id == request.RecipeId));
|
||||
|
||||
var existingForRecipe = existingShoppingList?
|
||||
.RecipeShoppingList.FirstOrDefault(rsl => rsl.Recipe.Id == request.RecipeId);
|
||||
|
||||
var existingIds = existingForRecipe?.SelectedIngredients
|
||||
.Where(si => si.RecipeIngredient != null)
|
||||
.Select(si => si.RecipeIngredient.Id)
|
||||
.ToHashSet() ?? new HashSet<Guid>();
|
||||
|
||||
var skippedExisting = request.IngredientIds.Where(id => existingIds.Contains(id)).Distinct().ToList();
|
||||
var ingredientIdsToAdd = request.IngredientIds.Except(skippedExisting).Distinct().ToList();
|
||||
|
||||
if (ingredientIdsToAdd.Count == 0)
|
||||
{
|
||||
return Json(new
|
||||
{
|
||||
shoppingListId = existingShoppingList?.Id,
|
||||
skippedExisting,
|
||||
addedCount = 0,
|
||||
skippedCount = skippedExisting.Count,
|
||||
noAdditions = true,
|
||||
});
|
||||
}
|
||||
|
||||
var shoppingList = await _shoppingListRepository.AddIngredientsToShoppingListAsync(request.RecipeId, ingredientIdsToAdd);
|
||||
|
||||
if (shoppingList == null)
|
||||
{
|
||||
return BadRequest("Error creating shopping list.");
|
||||
}
|
||||
|
||||
return Json(new { shoppingListId = shoppingList.Id });
|
||||
return Json(new
|
||||
{
|
||||
shoppingListId = shoppingList.Id,
|
||||
skippedExisting,
|
||||
addedCount = ingredientIdsToAdd.Count,
|
||||
skippedCount = skippedExisting.Count,
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet("RecipeCount")]
|
||||
|
||||
Reference in New Issue
Block a user