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

Added FridgeRepository and FridgeService. Started writing FridgeController

parent 9375c8c7
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.entity.Fridge;
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 groupId the id of the group
* group must exist
* @param productId the id of the product
* @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("")
}
package ntnu.idatt2016.v233.SmartMat.entity.group;
import ntnu.idatt2016.v233.SmartMat.entity.Fridge;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface FridgeRepository extends JpaRepository<Fridge, Long> {
/**
* Gets the fridge of a group
*
* @param groupId the id of the group
* @return an Optional containing the fridge of the group if it exists
*/
Optional<Fridge> findByGroupId(long groupId);
}
package ntnu.idatt2016.v233.SmartMat.service.group;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.Fridge;
import ntnu.idatt2016.v233.SmartMat.entity.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
* @since 24.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 addProduct(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()) {
fridge.getProducts().add(product.get());
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 removeProduct(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()) {
fridge.getProducts().remove(product.get());
fridgeRepository.save(fridge);
return true;
} else {
return false;
}
}
}
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