diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java index 859760b29bd38e53479ad7ac2ef07dfd00f52956..3c4dcce1e8e82d6e6b5d9aa2322560d144754cf2 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/RecipeController.java @@ -1,5 +1,6 @@ package ntnu.idatt2016.v233.SmartMat.controller; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.RestController; import lombok.AllArgsConstructor; @@ -59,7 +60,7 @@ public class RecipeController { public ResponseEntity<List<RecipeFridgeMatch>> getRecipeWithByProductsInFridge( @PathVariable("fridgeId") Long fridgeId, @PathVariable("recipeId") Long recipeId) { - List<RecipeFridgeMatch> recipe = recipeService.getRecipeWithFridgeProductMatch(fridgeId, recipeId); + List<RecipeFridgeMatch> recipe = recipeService.getRecipeWithFridgeProductMatch(fridgeId, recipeId); if (recipe.isEmpty()) { return ResponseEntity.notFound().build(); } else { @@ -67,4 +68,28 @@ public class RecipeController { } } + /** + * add recipe to favorites in user + * @param recipeId the id of the recipe + * @param authentication the authentication object + * @return 200 if the recipe was added to favorites, otherwise 404 + */ + @PostMapping("/favorite/{recipeId}") + public ResponseEntity<String> addRecipeToFavorites(@PathVariable("recipeId") Long recipeId, + Authentication authentication) { + return recipeService.addRecipeToFavorites(recipeId, authentication.getName()); + } + + /** + * remove recipe from favorites in user + * @param recipeId the id of the recipe + * @param authentication the authentication object + * @return 200 if the recipe was removed from favorites, otherwise 404 + */ + @DeleteMapping("/favorite/{recipeId}") + public ResponseEntity<String> removeRecipeFromFavorites(@PathVariable("recipeId") Long recipeId, + Authentication authentication) { + return recipeService.removeRecipeFromFavorites(recipeId, authentication.getName()); + } + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java index e857f458d53a0eec4bc8acbec59faa7aee44e97c..4129c44420e16c0b7fbfe83612d2e1b110cbc671 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java @@ -70,7 +70,7 @@ public class User implements UserDetails { @ManyToMany(mappedBy = "users", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) @JsonIgnoreProperties({"users"}) - private List<Recipe> recipes; + private Set<Recipe> recipes; @Column(name = "authority") @@ -114,7 +114,7 @@ public class User implements UserDetails { */ public void addRecipe(Recipe recipe){ if (this.recipes == null) { - this.recipes = new ArrayList<>(); + this.recipes = new HashSet<>(); } this.recipes.add(recipe); } @@ -194,5 +194,19 @@ public class User implements UserDetails { return this.enabled; } + @Override + public boolean equals(Object o){ + + if(o instanceof User user){ + return user.getUsername().equals(this.username); + } + return false; + } + + @Override + public int hashCode(){ + return Objects.hash(username); + } + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java index fbf4f6eb5b86a79ff98e12ed5100452f13b1ab1b..bdcb88886741e0155217d1154647c27c005fec17 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java @@ -9,7 +9,10 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import ntnu.idatt2016.v233.SmartMat.repository.user.UserRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; /** @@ -27,6 +30,9 @@ public class RecipeService { @Autowired private RecipeRepository recipeRepository; + @Autowired + private UserRepository userRepository; + /** * Creates a new recipe service * @param recipeRepository @@ -122,5 +128,56 @@ public class RecipeService { return result; } - + + /** + * Adds a recipe to a users favorites + * @param recipeId id of the recipe + * @param name name of the user + * @return ResponsEntity with succsess/fail message + */ + public ResponseEntity<String> addRecipeToFavorites(Long recipeId, String name) { + Optional<Recipe> recipe = recipeRepository.findById(recipeId); + Optional<User> user = userRepository.findByUsername(name); + if (recipe.isEmpty()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Could not find Recipe"); + } else if (user.isEmpty()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Could not find User"); + } else { + if (user.get().getRecipes().contains(recipe.get())) { + return ResponseEntity.status(HttpStatus.CONFLICT).body("Recipe already in favorites"); + } + + user.get().addRecipe(recipe.get()); + recipe.get().addUser(user.get()); + userRepository.save(user.get()); + return ResponseEntity.ok("Recipe added to favorites"); + } + + } + + /** + * Removes a recipe from a users favorites + * @param recipeId id of the recipe + * @param name name of the user + * @return ResponseEntity with succsess/fail message + */ + public ResponseEntity<String> removeRecipeFromFavorites(Long recipeId, String name) { + Optional<Recipe> recipe = recipeRepository.findById(recipeId); + Optional<User> user = userRepository.findByUsername(name); + + if (recipe.isEmpty()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Could not find Recipe"); + } else if (user.isEmpty()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Could not find User"); + } else { + if (!user.get().getRecipes().contains(recipe.get())) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Recipe not in favorites"); + } + + user.get().getRecipes().remove(recipe.get()); + recipe.get().getUsers().remove(user.get()); + userRepository.save(user.get()); + return ResponseEntity.ok("Recipe deleted from favorites"); + } + } } \ No newline at end of file