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

Merge branch 'main' into 'bugfix/139-add-purchase-date-to-frige_product-table'

Main

See merge request idatt2106-v23-03/backend!105
parents 3b0b2210 f9273909
No related branches found
No related tags found
No related merge requests found
package ntnu.idatt2016.v233.SmartMat.controller.group;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.dto.request.FridgeProductRequest;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.service.group.FridgeService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* Controller for fridges API, providing endpoints for fridge management
*
* @author Anders Austlid
* @version 1.0
* @since 24.04.2023
*/
@AllArgsConstructor
@RestController
@RequestMapping("/api/fridges")
public class FridgeController {
private final FridgeService fridgeService;
/**
* Gets the fridge of a group
* @param groupId the id of the group
* group must exist
* @return the fridge of the group if it exists, or a 404 if it doesn't
*/
@GetMapping("/group/{groupId}")
public ResponseEntity<Fridge> getFridgeByGroupId(@PathVariable("groupId") long groupId) {
return fridgeService.getFridgeByGroupId(groupId)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Adds a product to the fridge of a group
*
* @param request the request containing the group id and product id
* @return success if the product was added, bad request if the product was already in the fridge, or not found if the group or product doesn't exist
*/
@PostMapping("/group/product")
public ResponseEntity<String> addProductToFridge(@RequestBody FridgeProductRequest request) {
long groupId = request.groupId();
long productId = request.productId();
try {
fridgeService.getFridgeByGroupId(groupId).orElseThrow();
} catch (Exception e) {
return ResponseEntity.notFound().build();
}
try {
if (fridgeService.addProductToFridge(groupId, productId)) {
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
*
* @param groupId the id of the group
* group must exist
* group must have a fridge
* @param productId the id of the product
* @return success if the product was removed, bad request if the product wasn't in the fridge, or not found if the group or product doesn't exist
*/
@DeleteMapping("/group/{groupId}/product/{productId}")
public ResponseEntity<String> removeProductFromFridge(@PathVariable("groupId") long groupId, @PathVariable("productId") long productId) {
try {
fridgeService.getFridgeByGroupId(groupId).orElseThrow();
} catch (Exception e) {
return ResponseEntity.notFound().build();
}
try {
if (fridgeService.removeProductFromFridge(groupId, productId)) {
return ResponseEntity.ok("Success");
}
return ResponseEntity.badRequest().body("Product not found in the fridge");
} catch (Exception e) {
return ResponseEntity.status(500).body("Internal server error");
}
}
}
package ntnu.idatt2016.v233.SmartMat.dto.request;
/**
* FridgeProductRequest is a record class representing a request to add a product to a fridge.
* @param groupId the id of the group
* @param productId the id of the product
*/
public record FridgeProductRequest(long groupId, long productId) {
}
package ntnu.idatt2016.v233.SmartMat.entity;
package ntnu.idatt2016.v233.SmartMat.entity.group;
import java.util.List;
......@@ -15,9 +15,8 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
* Fridge is an entity class representing a fridge in the system.
*
* @author Anders
* @version 1.1.001
* @since 19.04.2023
*
* @version 1.1.002
* @since 15.04.2023
*/
@NoArgsConstructor
......@@ -38,6 +37,6 @@ public class Fridge{
@JoinTable(name = "fridge_product",
joinColumns = @JoinColumn(name = "fridge_id"),
inverseJoinColumns = @JoinColumn(name = "ean"))
@JsonIgnoreProperties("fridges")
@JsonIgnoreProperties({"allergies", "fridges"})
List<Product> products;
}
......@@ -6,7 +6,7 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.Fridge;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge;
import ntnu.idatt2016.v233.SmartMat.entity.Recipe;
import java.util.List;
......
package ntnu.idatt2016.v233.SmartMat.repository;
import ntnu.idatt2016.v233.SmartMat.entity.Fridge;
import java.util.List;
package ntnu.idatt2016.v233.SmartMat.repository.group;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Repository for Products
* @author Stian
* @version 1.0
*/
public interface FridgeRepository extends JpaRepository<Fridge, Long> {
import java.util.Optional;
public interface FridgeRepository extends JpaRepository<Fridge, Long> {
/**
* Gets all fridge items by their group id
* Gets the fridge of a group
*
* @param id the id of the group
* @return a list of fridge items
* @param groupId the id of the group
* @return an Optional containing the fridge of the group if it exists
*/
List<Fridge> findAllByGroupId(long id);
}
\ No newline at end of file
Optional<Fridge> findByGroupId(long groupId);
}
package ntnu.idatt2016.v233.SmartMat.service.group;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge;
import ntnu.idatt2016.v233.SmartMat.repository.group.FridgeRepository;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.service.product.ProductService;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
* Service for management of a group fridge
*
* @author Anders Austlid
* @version 1.0.1
* @since 25.04.2023
*/
@AllArgsConstructor
@Service
public class FridgeService {
private final FridgeRepository fridgeRepository;
private final ProductService productService;
/**
* Gets the fridge of a group
*
* @param groupId the id of the group
* @return the fridge of the group
*/
public Optional<Fridge> getFridgeByGroupId(long groupId) {
return fridgeRepository.findByGroupId(groupId);
}
/**
* Add a product to the fridge of a group
*
* @param groupId the id of the group
* group must exist
* @param ean the ean of the product
* @return true if the product was added
*/
public boolean addProductToFridge(long groupId, long ean) {
Optional<Product> product = productService.getProductById(ean);
Fridge fridge = fridgeRepository.findByGroupId(groupId).orElseThrow(() -> new IllegalArgumentException("Fridge does not exist"));
if (product.isPresent()) {
Product productToAdd = product.get();
if (fridge.getProducts().contains(productToAdd)) {
return false;
}
fridge.getProducts().add(productToAdd);
fridgeRepository.save(fridge);
return true;
} else {
return false;
}
}
/**
* Remove a product from the fridge of a group
*
* @param groupId the id of the group
* group must exist
* @param ean the ean of the product
* @return true if the product was removed
*/
public boolean removeProductFromFridge(long groupId, long ean) {
Optional<Product> product = productService.getProductById(ean);
Fridge fridge = fridgeRepository.findByGroupId(groupId).orElseThrow(() -> new IllegalArgumentException("Fridge does not exist"));
if (product.isPresent()) {
Product productToRemove = product.get();
if (!fridge.getProducts().contains(productToRemove)) {
return false;
}
fridge.getProducts().remove(productToRemove);
fridgeRepository.save(fridge);
return true;
} else {
return false;
}
}
}
......@@ -12,8 +12,8 @@ import java.util.Optional;
* Service for groups
*
* @author Anders Austlid
* @version 1.0
* @since 20.04.2023
* @version 1.1
* @since 25.04.2023
*/
@AllArgsConstructor
@Service
......
......@@ -3,6 +3,7 @@ package ntnu.idatt2016.v233.SmartMat.service.product;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import ntnu.idatt2016.v233.SmartMat.repository.ProductRepository;
import ntnu.idatt2016.v233.SmartMat.util.ProductUtil;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
......@@ -14,6 +15,7 @@ import java.util.Optional;
* @version 1.1
* @since 05.04.2023
*/
@Service
public class ProductService {
ProductRepository productRepository;
......
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