Skip to content
Snippets Groups Projects
Commit ebd57c16 authored by Anders Austlid's avatar Anders Austlid
Browse files

Trying to fix bug in fridge product endpoints

parent 7e278532
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.sql.Date;
import java.util.Optional;
/**
* Controller for fridges API, providing endpoints for fridge management
......@@ -61,22 +62,20 @@ public class FridgeController {
long groupId = request.groupId();
long productId = request.productId();
try {
fridgeService.getFridgeByGroupId(groupId).orElseThrow();
} catch (Exception e) {
Optional<Fridge> fridgeOpt = fridgeService.getFridgeByGroupId(groupId);
if (fridgeOpt.isEmpty()) {
return ResponseEntity.notFound().build();
}
try {
if (fridgeService.addProductToFridge(groupId,productId, request.amount(), request.days()).isPresent()) {
fridgeService.addProductToFridge(groupId, productId, request.amount(), request.days());
return ResponseEntity.ok("Success");
}
return ResponseEntity.badRequest().body("Product already exists in the fridge");
} catch (Exception e) {
return ResponseEntity.status(500).body("Internal server error");
}
}
/**
* Removes a product from the fridge of a group
* todo: remove the date parameter when the frontend is done
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment