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

add to shopping list

parent 0e058aa1
No related branches found
No related tags found
No related merge requests found
...@@ -28,10 +28,11 @@ import ntnu.idatt2016.v233.SmartMat.service.ShoppingListService; ...@@ -28,10 +28,11 @@ import ntnu.idatt2016.v233.SmartMat.service.ShoppingListService;
@RequestMapping("/api/shoppinglist") @RequestMapping("/api/shoppinglist")
public class ShoppingListController { public class ShoppingListController {
@Autowired
ShoppingListService shoppingListService; ShoppingListService shoppingListService;
@Autowired ProductService productService;
UserService userService; UserService userService;
...@@ -70,21 +71,25 @@ public class ShoppingListController { ...@@ -70,21 +71,25 @@ public class ShoppingListController {
* @return the shopping list with the added product, or an error if the shopping list ID or EAN is invalid * @return the shopping list with the added product, or an error if the shopping list ID or EAN is invalid
*/ */
@PostMapping("/addProduct/{shoppingListId}/{ean}") @PostMapping("/addProduct/{shoppingListId}/{ean}")
public ResponseEntity<ShoppingList> addItemToShoppingList(@PathVariable("shoppingListId") long shoppingListId, public ResponseEntity<?> addItemToShoppingList(@PathVariable("shoppingListId") long shoppingListId,
@PathVariable("ean") String ean, Authentication auth){ @PathVariable("ean") String ean, Authentication auth){
Optional<ShoppingList> shoppingList = shoppingListService.getShoppingListById(shoppingListId); Optional<ShoppingList> shoppingList = shoppingListService.getShoppingListById(shoppingListId);
if(shoppingList.isEmpty()) if(shoppingList.isEmpty())
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); return ResponseEntity.badRequest().body("shoppinglist not found");
if(productService.getProductById(Long.parseLong(ean)).isEmpty())
return ResponseEntity.badRequest().body("product not found");
Optional<User> user = userService.getUserFromUsername(auth.getName()); Optional<User> user = userService.getUserFromUsername(auth.getName());
if(user.isEmpty()) if(user.isEmpty())
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
if(user.get().getGroup().stream().anyMatch(userGroupAsso -> if(user.get().getGroup().stream().noneMatch(userGroupAsso ->
userGroupAsso.getGroup().getGroupId() == shoppingList.get().getGroup().getGroupId())) userGroupAsso.getGroup().getGroupId() == shoppingList.get().getGroup().getGroupId()))
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
...@@ -95,11 +100,16 @@ public class ShoppingListController { ...@@ -95,11 +100,16 @@ public class ShoppingListController {
if(product.isPresent()) if(product.isPresent())
return ResponseEntity.status(HttpStatus.CONFLICT).build(); return ResponseEntity.status(HttpStatus.CONFLICT).build();
Optional<ShoppingList> returnval = shoppingListService.addProductToShoppingList(shoppingList.get().getShoppingListID(), System.out.println("Adding product to shopping list : " + shoppingListId + " - " + ean);
Long.parseLong(ean));
return returnval.map(list -> ResponseEntity.status(HttpStatus.OK).body(list)) Optional<ShoppingList> returnval = shoppingListService.addProductToShoppingList(Long.parseLong(ean) ,
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build()); shoppingListId
);
return returnval.map(list -> ResponseEntity.status(HttpStatus.OK).body(list.getProducts()))
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_MODIFIED).build());
} }
......
...@@ -3,10 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.entity; ...@@ -3,10 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group; import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
...@@ -26,7 +23,7 @@ import java.util.List; ...@@ -26,7 +23,7 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
@Entity(name = "shopping_list") @Entity(name = "shopping_list")
@Data @Getter @Setter
public class ShoppingList { public class ShoppingList {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
...@@ -44,7 +41,7 @@ public class ShoppingList { ...@@ -44,7 +41,7 @@ public class ShoppingList {
name = "shopping_list_product", name = "shopping_list_product",
joinColumns = @JoinColumn(name = "shopping_list_id"), joinColumns = @JoinColumn(name = "shopping_list_id"),
inverseJoinColumns = @JoinColumn(name = "ean")) inverseJoinColumns = @JoinColumn(name = "ean"))
@JsonIgnoreProperties("shoppingList") @JsonIgnoreProperties("shoppingLists")
private List<Product> products; private List<Product> products;
......
...@@ -41,12 +41,14 @@ public class Product{ ...@@ -41,12 +41,14 @@ public class Product{
@ManyToMany(mappedBy = "products") @ManyToMany(mappedBy = "products")
@JsonIgnoreProperties({"products"}) @JsonIgnoreProperties({"products"})
@JsonIgnore
List<ShoppingList> shoppingLists; List<ShoppingList> shoppingLists;
@ManyToOne @ManyToOne
@JoinColumn(name = "category_name") @JoinColumn(name = "category_name")
@JsonIgnoreProperties({"products"}) @JsonIgnoreProperties({"products"})
@JsonIgnore
Category category; Category category;
@Column(name = "image_url") @Column(name = "image_url")
...@@ -70,6 +72,7 @@ public class Product{ ...@@ -70,6 +72,7 @@ public class Product{
name = "product_allergy", name = "product_allergy",
joinColumns = @JoinColumn(name = "ean"), joinColumns = @JoinColumn(name = "ean"),
inverseJoinColumns = @JoinColumn(name = "allergy_name")) inverseJoinColumns = @JoinColumn(name = "allergy_name"))
@JsonIgnore
List<Allergy> allergies; List<Allergy> allergies;
@OneToMany(cascade = CascadeType.ALL) @OneToMany(cascade = CascadeType.ALL)
......
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