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

Merge branch 'bugfix/237-fix-add-shoppinglist' into 'main'

Resolve "fix add shoppinglist"

Closes #237

See merge request idatt2106-v23-03/backend!178
parents 0e058aa1 1ac0254c
No related branches found
No related tags found
No related merge requests found
......@@ -28,10 +28,11 @@ import ntnu.idatt2016.v233.SmartMat.service.ShoppingListService;
@RequestMapping("/api/shoppinglist")
public class ShoppingListController {
@Autowired
ShoppingListService shoppingListService;
@Autowired
ProductService productService;
UserService userService;
......@@ -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
*/
@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){
Optional<ShoppingList> shoppingList = shoppingListService.getShoppingListById(shoppingListId);
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());
if(user.isEmpty())
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()))
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
......@@ -95,11 +100,16 @@ public class ShoppingListController {
if(product.isPresent())
return ResponseEntity.status(HttpStatus.CONFLICT).build();
Optional<ShoppingList> returnval = shoppingListService.addProductToShoppingList(shoppingList.get().getShoppingListID(),
Long.parseLong(ean));
System.out.println("Adding product to shopping list : " + shoppingListId + " - " + ean);
return returnval.map(list -> ResponseEntity.status(HttpStatus.OK).body(list))
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build());
Optional<ShoppingList> returnval = shoppingListService.addProductToShoppingList(Long.parseLong(ean) ,
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;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
......@@ -26,7 +23,7 @@ import java.util.List;
@AllArgsConstructor
@Builder
@Entity(name = "shopping_list")
@Data
@Getter @Setter
public class ShoppingList {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
......@@ -44,7 +41,7 @@ public class ShoppingList {
name = "shopping_list_product",
joinColumns = @JoinColumn(name = "shopping_list_id"),
inverseJoinColumns = @JoinColumn(name = "ean"))
@JsonIgnoreProperties("shoppingList")
@JsonIgnoreProperties("shoppingLists")
private List<Product> products;
......
......@@ -41,12 +41,14 @@ public class Product{
@ManyToMany(mappedBy = "products")
@JsonIgnoreProperties({"products"})
@JsonIgnore
List<ShoppingList> shoppingLists;
@ManyToOne
@JoinColumn(name = "category_name")
@JsonIgnoreProperties({"products"})
@JsonIgnore
Category category;
@Column(name = "image_url")
......@@ -70,6 +72,7 @@ public class Product{
name = "product_allergy",
joinColumns = @JoinColumn(name = "ean"),
inverseJoinColumns = @JoinColumn(name = "allergy_name"))
@JsonIgnore
List<Allergy> allergies;
@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