diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Recipe.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Recipe.java index 991c53fb3721dbf5cfd86bf4f13f2c7d85cd742a..c958cac42e64891ed33a6e2f1162371a25b60e41 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Recipe.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Recipe.java @@ -1,5 +1,6 @@ package ntnu.idatt2016.v233.SmartMat.entity; +import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -48,5 +49,32 @@ public class Recipe { @ManyToMany(mappedBy = "recipes") @JsonIgnoreProperties({"recipes"}) List<User> users; - + + + /** + * Adds a product to the recipe + * @param product product to add + */ + public void addProduct(Product product){ + + if(products == null){ + products = new ArrayList<>(); + } + + products.add(product); + } + + /** + * Adds a user to the recipe + * used for adding favorites + * @param user user to add + */ + public void addUser(User user){ + + if(users == null){ + users = new ArrayList<>(); + } + + users.add(user); + } } 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 b1b99398107725a8023044bbc68668765506987b..b96e20279fe8b69fb8950b968097900146c222de 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/RecipeService.java @@ -1,6 +1,7 @@ package ntnu.idatt2016.v233.SmartMat.service; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; +import ntnu.idatt2016.v233.SmartMat.entity.user.User; import ntnu.idatt2016.v233.SmartMat.repository.RecipeRepository; import java.util.List; @@ -89,4 +90,16 @@ public class RecipeService { recipeRepository.deleteById(id); } + + /** + * Adds a user to a recipe + * used for adding favorite recipes + * @param recipe recipe to add user to + * @param user user to add to recipe + */ + public void addUserToRecipe(Recipe recipe, User user){ + recipe.addUser(user); + recipeRepository.save(recipe); + } + } \ No newline at end of file diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java index 23ee57f5c00fe09caddde305109ef386c9a98bf2..08db7eecab721cecc1291eacfcc824b141e5cd1f 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java @@ -1,8 +1,10 @@ package ntnu.idatt2016.v233.SmartMat.service.user; import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.entity.Recipe; import ntnu.idatt2016.v233.SmartMat.entity.user.User; import ntnu.idatt2016.v233.SmartMat.repository.user.UserRepository; +import ntnu.idatt2016.v233.SmartMat.service.RecipeService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; @@ -21,6 +23,8 @@ public class UserService { private UserRepository userRepository; + private RecipeService recipeService; + /** * gets user from username out of database @@ -97,4 +101,21 @@ public class UserService { public Optional<User> getUserFromEmail(String email) { return userRepository.findByEmail(email); } + + /** + * adds recipe to users favorite recipes + * @param username username of user + * @param recipeId id of recipe + * @throws RuntimeException if user or recipe does not exist or user + */ + public void addFavoriteRecipe(String username, long recipeId) throws RuntimeException{ + User user = userRepository.findByUsername(username) + .orElseThrow(()-> new UsernameNotFoundException("did not find user")); + Recipe tempRecipe = recipeService.getRecipeById(recipeId) + .orElseThrow(()-> new RuntimeException("did not find recipe")); + user.addRecipe(tempRecipe); + + recipeService.addUserToRecipe(tempRecipe, user); + userRepository.save(user); + } } diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepositoryTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepositoryTest.java index 2dcac5a6c76796bd08cb8e36fd0d85be81a8f968..6674f3310e794624c4dd5bd34ea560ae2ea958a7 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepositoryTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepositoryTest.java @@ -3,12 +3,15 @@ package ntnu.idatt2016.v233.SmartMat.repository; import static org.junit.jupiter.api.Assertions.*; import java.util.List; +import java.util.Optional; +import ntnu.idatt2016.v233.SmartMat.entity.user.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import ntnu.idatt2016.v233.SmartMat.entity.Recipe; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; @DataJpaTest public class RecipeRepositoryTest { @@ -16,6 +19,9 @@ public class RecipeRepositoryTest { @Autowired private RecipeRepository recipeRepository; + @Autowired + private TestEntityManager entityManager; + @Test public void testGetByName() { Recipe recipe = Recipe.builder() @@ -74,4 +80,39 @@ public class RecipeRepositoryTest { assertEquals(recipe2.getName(), foundRecipes.get(1).getName()); } + @Test + void shouldAddRecipeToFavorite(){ + Recipe recipe = Recipe.builder() + .name("Pizza Margherita") + .description("The classic Italian pizza") + .build(); + recipeRepository.save(recipe); + long id = recipe.getId(); + + User user = User.builder() + .username("testuser") + .password("password") + .enabled(true) + .email("test@test.com") + .build(); + + entityManager.persist(user); + + user.addRecipe(recipe); + recipe.addUser(user); + + entityManager.persist(user); + recipeRepository.save(recipe); + + + Optional<Recipe> foundRecipe = recipeRepository.findById(id); + + assertTrue(foundRecipe.isPresent()); + + assertTrue(foundRecipe.get().getUsers().size() > 0); + assertTrue(entityManager.find(User.class, user.getUsername()).getRecipes().size() > 0); + + + } + }