diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Favourite.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Favourite.java deleted file mode 100644 index 03f37e787763a595a0d12c23ac0bf70007ea03db..0000000000000000000000000000000000000000 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Favourite.java +++ /dev/null @@ -1,24 +0,0 @@ -package ntnu.idatt2016.v233.SmartMat.entity; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -@AllArgsConstructor -@Builder -@Entity(name = "favorite") -@Data -public class Favourite { - - @Id - @Column(name = "recipe_id") - long recipeId; - - @Column(name = "username") - String username; -} 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 7eb771dc2d56b0a221f636532f37825f519ccecc..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; @@ -10,13 +11,14 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import ntnu.idatt2016.v233.SmartMat.entity.product.Product; +import ntnu.idatt2016.v233.SmartMat.entity.user.User; /** * Recipe is an entity class representing a recipe in the system. * - * @author Anders & Stian - * @version 1.0.001 - * @since 19.04.2023 + * @author Anders & Stian + Birk + * @version 1.1 + * @since 25.04.2023 * */ @@ -43,5 +45,36 @@ public class Recipe { inverseJoinColumns = @JoinColumn(name = "ean")) @JsonIgnoreProperties({"recipes"}) List<Product> products; - + + @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/entity/user/User.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java index a04c39561bd8472c1fc0dc0af5f401618ba2d497..01ebf01dd306d5e85a6f039b8177558458f5bb20 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 @@ -9,6 +9,7 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import ntnu.idatt2016.v233.SmartMat.dto.enums.Authority; +import ntnu.idatt2016.v233.SmartMat.entity.Recipe; import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso; import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy; import org.springframework.security.core.GrantedAuthority; @@ -23,7 +24,7 @@ import java.util.*; * It implements the UserDetails interface. * * @author Anders and Birk - * @version 2.0.2 + * @version 2.0.3 * @since 25.04.2023 * */ @@ -74,6 +75,38 @@ public class User implements UserDetails { private List<Allergy> allergies; + @ManyToMany + @JoinTable( + name = "favorite_recipes", + joinColumns = @JoinColumn(name = "username"), + inverseJoinColumns = @JoinColumn(name = "recipe_id")) + @JsonIgnoreProperties({"users"}) + private List<Recipe> recipes; + + + /** + * adds an allergy to the user + * @param allergy the allergy to add to the user + */ + public void addAllergy(Allergy allergy){ + if (this.allergies == null) { + this.allergies = new ArrayList<>(); + } + this.allergies.add(allergy); + } + + /** + * adds a recipe to the user + * @param recipe the recipe to add to the user + */ + public void addRecipe(Recipe recipe){ + if (this.recipes == null) { + this.recipes = new ArrayList<>(); + } + this.recipes.add(recipe); + } + + /** * adds a group to the user * @param userGroupTable the userGroupTable to add to the user diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/FavoriteRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/FavoriteRepository.java deleted file mode 100644 index 7f266bf30ef18e5a7d59b34306dcf14f666c40ae..0000000000000000000000000000000000000000 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/FavoriteRepository.java +++ /dev/null @@ -1,27 +0,0 @@ -package ntnu.idatt2016.v233.SmartMat.repository; - -import java.util.List; - -import org.springframework.data.jpa.repository.JpaRepository; - -import ntnu.idatt2016.v233.SmartMat.entity.Favourite; - -public interface FavoriteRepository extends JpaRepository<Favourite, Long> { - - /** - * Gets all favorites by username - * - * @param id the username - * @return a list of all favorites if they exist - */ - List<Favourite> getAllByUsername(String name); - - /** - * Gets all favorites by recipe ID - * - * @param id the ID of the recipe - * @return a list of all favorites if they exist - */ - List<Favourite> getAllByRecipeId(long id); - -} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/FavouriteService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/FavouriteService.java deleted file mode 100644 index f4e2d1a4064b82c9ae48d95154332b0881c288fe..0000000000000000000000000000000000000000 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/FavouriteService.java +++ /dev/null @@ -1,77 +0,0 @@ -package ntnu.idatt2016.v233.SmartMat.service; - -import java.util.List; -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import ntnu.idatt2016.v233.SmartMat.dto.request.FavoriteRequest; -import ntnu.idatt2016.v233.SmartMat.entity.Favourite; -import ntnu.idatt2016.v233.SmartMat.repository.FavoriteRepository; - -/** - * Service for the favorites enitity - * - * @author Stian Lyng - * @version 1.1 - */ -@Service -public class FavouriteService { - - @Autowired - FavoriteRepository favoriteRepository; - - /** - * Create and save a shopping list to the database - * @param shoppingList the shopping list to save - * @return the saved shopping list - */ - public Favourite addFav(FavoriteRequest request) { - Favourite favourite = new Favourite(); - favourite.setRecipeId(request.getRecipeId()); - favourite.setUsername(request.getUsername()); - return favoriteRepository.save(favourite); - } - - /** - * Gets all favorites - * - * @return a list of all favorites if they exist - */ - public List<Favourite> getAllFavorites() { - return favoriteRepository.findAll(); - } - - /** - * Gets all favorites by recipe ID - * - * @param id the ID of the recipe - * @return a list of all favorites if they exist - */ - public List<Favourite> getAllFavoritesByRecipeId(long id) { - return favoriteRepository.getAllByRecipeId(id); - } - - /** - * Gets all favorites by username - * - * @param id the username of the user - * @return a list of all favorites if they exist - */ - public List<Favourite> getAllFavoritesByUsername(String name) { - return favoriteRepository.getAllByUsername(name); - } - - /** - * Deletes a favorite by its ID - * - * @param id the ID of the favorite entry - */ - public void deleteFavoriteById(long id) { - Optional<Favourite> favorite = favoriteRepository.findById(id); - if (favorite.isPresent()) { - favoriteRepository.deleteById(id); - } - } -} 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); + + + } + }