Skip to content
Snippets Groups Projects

Add endpoint fridge

Merged Katarzyna Szlejter requested to merge add-endpoint-fridge into main
5 files
+ 181
8
Compare changes
  • Side-by-side
  • Inline
Files
5
package edu.ntnu.idatt210602.matsvinnbackend.controller;
import edu.ntnu.idatt210602.matsvinnbackend.model.*;
import edu.ntnu.idatt210602.matsvinnbackend.repo.AccountRepository;
import edu.ntnu.idatt210602.matsvinnbackend.repo.FridgeRepository;
import edu.ntnu.idatt210602.matsvinnbackend.repo.IngredientRepository;
import edu.ntnu.idatt210602.matsvinnbackend.repo.ItemRepository;
import edu.ntnu.idatt210602.matsvinnbackend.repo.*;
import edu.ntnu.idatt210602.matsvinnbackend.service.FridgeService;
import edu.ntnu.idatt210602.matsvinnbackend.service.LogService;
import org.slf4j.Logger;
@@ -17,6 +14,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.*;
import java.util.stream.Collectors;
/**
* Fridge controller
@@ -45,6 +43,9 @@ public class FridgeController {
@Autowired
FridgeService fridgeService;
@Autowired
RecipeRepository recipeRepository;
Logger logger = LoggerFactory.getLogger(FridgeController.class);
/**
@@ -67,7 +68,6 @@ public class FridgeController {
return fridgeService.addIngredients(loggedInAccount, ingredientList);
}
/**
* Add items to fridge
*
@@ -144,6 +144,7 @@ public class FridgeController {
logService.logAction(new Date(), request.action(), fridge.getIngredientList().get(ingredientIndex), loggedInAccount);
fridge.getIngredientList().remove(ingredientIndex);
logger.info("Ingredient removed from the fridge");
//TODO remove ingredient from repository
} else {
fridge.getIngredientList().get(ingredientIndex).setAmount(remainingAmount.serialize());
@@ -186,4 +187,91 @@ public class FridgeController {
//return updated ingredient
return fridge.getIngredientList().get(ingredientIndex);
}
/**
* Remove ingredients based on recipe
*
* Possible responses:
* HTTP 200 OK - ingredient amount removed
* HTTP 404 Not found - account/fridge/recipe not found
* @param request that contains recipe id and recipe scalar
* @return updated fridge ingredients
*/
@PutMapping(path = "/ingredients")
@ResponseStatus(code = HttpStatus.OK)
public @ResponseBody void removeIngredients(@RequestBody RemoveIngredientsRequest request) {
int recipeId = request.recipeId();
int recipeScalar = request.recipeScalar();
logger.info("Request to remove ingredients from fridge based on recipe with id: "+recipeId);
//Authentication
String authenticatedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
Account loggedInAccount = accountRepository.findByEmail(authenticatedUsername).orElseThrow();
Fridge fridge = fridgeRepository.findById(loggedInAccount.getFridge().getId()).orElseThrow();
Recipe recipe = recipeRepository.findById(recipeId).orElseThrow();
//Declare all variables
Amount recipeIngredientAmount;
Amount remainingAmount;
Amount currentFridgeIngredientAmount;
logger.info("Recipe ingredients: "+recipe.getIngredient());
//Loop through recipe ingredients
for (Ingredient recipeIngredient : recipe.getIngredient()) {
//current recipe ingredient amount multiplied with recipe scalar
recipeIngredientAmount = recipeIngredient.getAmount().deserialize().multiply(recipeScalar);
//remaining amount of that ingredient
remainingAmount = recipeIngredientAmount;
//Find all ingredients in fridge that match current recipe ingredient and sort based on expire date
List<Ingredient> fridgeIngredients = fridge.getIngredientList().stream().filter(i -> Objects.equals(i.getItem().getName(), recipeIngredient.getItem().getName())).collect(Collectors.toList());
fridgeIngredients.sort(Comparator.comparing(Ingredient::getExp_date));
logger.info("Fridge ingredients: "+fridgeIngredients);
//Loop through matching ingredients in fridge
for (Ingredient fridgeIngredient : fridgeIngredients) {
fridgeIngredient.setStatus(Ingredient.Status.RESERVED); //only for testing since no ingredients are reserved yet!!!
//Check if the ingredient is RESERVED and if there is any remaining amount of current recipe ingredient
if (fridgeIngredient.getStatus() == Ingredient.Status.RESERVED && remainingAmount.getQuantity() != 0) {
//amount of current fridge ingredient
currentFridgeIngredientAmount = fridgeIngredient.getAmount().deserialize();
//log consumed ingredient before it potentially gets removed
int ingredientIndex = fridge.getIngredientList().indexOf(fridgeIngredient);
logService.logAction(new Date(), Log.Action.CONSUMED, fridge.getIngredientList().get(ingredientIndex), loggedInAccount);
//if there is any remaining fridge ingredient after we subtracted recipe amount, save ingredient and set remaining amount to 0
if (currentFridgeIngredientAmount.subtract(remainingAmount).getQuantity() > 0) {
ingredientRepository.save(fridgeIngredient);
fridge.getIngredientList().get(ingredientIndex).setAmount(currentFridgeIngredientAmount.subtract(remainingAmount).serialize());
//remaining amount is now 0
remainingAmount.subtract(remainingAmount);
logger.info("Removed recipe amount of this ingredient and updated it in the fridge.");
//if current fridge ingredient is exactly the same as the remaining amount, remove ingredient and set remaining amount to 0
} else if (currentFridgeIngredientAmount.subtract(remainingAmount).getQuantity() == 0) {
fridge.getIngredientList().remove(ingredientIndex);
ingredientRepository.delete(fridgeIngredient);
//remaining amount is now 0
remainingAmount.subtract(remainingAmount);
logger.info("Ingredient removed from the fridge");
// if current fridge ingredient is not enough for remaining amount,
// remove ingredient and update remaining amount to the remainder and multiple with -1 to get positive number
} else {
remainingAmount = currentFridgeIngredientAmount.subtract(remainingAmount).multiply(-1);
fridge.getIngredientList().remove(ingredientIndex);
ingredientRepository.delete(fridgeIngredient);
logger.info("Ingredient removed from the fridge");
}
}
}
}
fridgeRepository.save(fridge);
}
}
Loading