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

added remove + add item to shopping list methods

parent 948bb1be
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ import lombok.Data; ...@@ -9,6 +9,7 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
...@@ -42,4 +43,14 @@ public class ShoppingList { ...@@ -42,4 +43,14 @@ public class ShoppingList {
@JsonIgnoreProperties("shoppingList") @JsonIgnoreProperties("shoppingList")
private List<Product> products; private List<Product> products;
/**
* Adds a product to the shopping list
* @param product the product to add to the shopping list
*/
public void addProduct(Product product) {
if (products == null)
products = new ArrayList<>();
products.add(product);
}
} }
...@@ -97,4 +97,14 @@ public class Product{ ...@@ -97,4 +97,14 @@ public class Product{
public String toString(){ public String toString(){
return String.valueOf(this.ean); return String.valueOf(this.ean);
} }
/**
* Adds a shopping list to the product
* @param shoppingList the shopping list to add to the product
*/
public void addShoppingList(ShoppingList shoppingList) {
if (shoppingLists == null)
shoppingLists = new ArrayList<>();
shoppingLists.add(shoppingList);
}
} }
\ No newline at end of file
...@@ -4,6 +4,7 @@ import java.util.List; ...@@ -4,6 +4,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.repository.product.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -23,6 +24,9 @@ public class ShoppingListService { ...@@ -23,6 +24,9 @@ public class ShoppingListService {
@Autowired @Autowired
ShoppingListRepository shoppingListRepository; ShoppingListRepository shoppingListRepository;
@Autowired
ProductRepository productRepository;
/** /**
* Create and save a shopping list to the database * Create and save a shopping list to the database
* @param shoppingListRequest the shopping list to save * @param shoppingListRequest the shopping list to save
...@@ -76,8 +80,41 @@ public class ShoppingListService { ...@@ -76,8 +80,41 @@ public class ShoppingListService {
} }
} }
public Product addProductToShoppingList(long ean, long shoppingListId){ /**
return null; * Adds a product to a shopping list
* @param ean the ean of the product to add
* @param shoppingListId the id of the shopping list to add the product to
* @return the product that was added to the shopping list
*/
public Optional<ShoppingList> addProductToShoppingList(long ean, long shoppingListId){
shoppingListRepository.findById(shoppingListId).ifPresent(shoppingList -> {
productRepository.findById(ean).ifPresent(product -> {
shoppingList.addProduct(product);
product.addShoppingList(shoppingList);
productRepository.save(product);
shoppingListRepository.save(shoppingList);
});
});
return shoppingListRepository.findById(shoppingListId);
} }
/**
* Removes a product from a shopping list
* @param ean the ean of the product to remove
* @param shoppingListId the id of the shopping list to remove the product from
* @return the shopping list that the product was removed from
*/
public Optional<ShoppingList> removeProductFromShoppingList(long ean, long shoppingListId){
shoppingListRepository.findById(shoppingListId).ifPresent(shoppingList -> {
productRepository.findById(ean).ifPresent(product -> {
shoppingList.getProducts().remove(product);
product.getShoppingLists().remove(shoppingList);
productRepository.save(product);
shoppingListRepository.save(shoppingList);
});
});
return shoppingListRepository.findById(shoppingListId);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment