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

added add favoirte method to services and tests

parent 3b9ece88
No related branches found
No related tags found
No related merge requests found
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);
}
}
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
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);
}
}
......@@ -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);
}
}
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