Skip to content
Snippets Groups Projects
Commit e6651837 authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

added add/remove from favorite recipe endpoint

parent 1193559d
No related branches found
No related tags found
No related merge requests found
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());
}
}
......@@ -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);
}
}
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment