From 10a9e5a7b0a2a19da6977239d6f335addf346ce0 Mon Sep 17 00:00:00 2001 From: birkon <birkon@stud.ntnu.no> Date: Tue, 25 Apr 2023 11:41:15 +0200 Subject: [PATCH] added add favoirte method to services and tests --- .../v233/SmartMat/entity/Recipe.java | 30 +++++++++++++- .../v233/SmartMat/service/RecipeService.java | 13 ++++++ .../SmartMat/service/user/UserService.java | 21 ++++++++++ .../repository/RecipeRepositoryTest.java | 41 +++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) 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 991c53fb..c958cac4 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 b1b99398..b96e2027 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 23ee57f5..08db7eec 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 2dcac5a6..6674f331 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); + + + } + } -- GitLab