Skip to content
Snippets Groups Projects
Commit da4ce094 authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Price attirubute was added

parent 860843e5
No related branches found
No related tags found
No related merge requests found
......@@ -112,24 +112,5 @@ public class FridgeController {
return fridgeService.wasteProductFromFridge(fridgeProductId).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Deletes all products in a fridge
* @param fridgeId the id of the fridge
* @return success if the products were deleted, bad request if the fridge doesn't exist
*
@DeleteMapping("/delete/all/{fridgeId}")
public ResponseEntity<String> deleteAllProductsInFridge(@PathVariable("fridgeId") long fridgeId) {
try {
boolean success = fridgeService.deleteAllProductsInFridge(fridgeId);
if (success){
return ResponseEntity.ok("Success");
}
return ResponseEntity.badRequest().body("Fridge not found");
} catch (Exception e) {
return ResponseEntity.status(500).body("Internal server error");
}
}
*/
}
......@@ -3,7 +3,6 @@ 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 fridgeProductId, long groupId, long ean, int amount, int days) {
public record FridgeProductRequest(long fridgeProductId, long groupId, long ean, int amount, int days, double price) {
}
......@@ -53,4 +53,7 @@ public class Waste {
@Column(name = "unit")
String unit;
@Column(name = "buy_price")
double buyPrice;
}
......@@ -40,5 +40,8 @@ public class FridgeProductAsso {
@Column(name = "amount")
private double amount;
@Column(name = "buy_price")
private double buyPrice;
}
......@@ -72,6 +72,10 @@ public class FridgeService {
public Optional<Object> addProductToFridge(FridgeProductRequest fridgeProductRequest) {
Optional<Product> product = productService.getProductById(fridgeProductRequest.ean());
Optional<Fridge> fridge = fridgeRepository.findByGroupGroupId(fridgeProductRequest.groupId());
double price = 100.0;
if(fridgeProductRequest.price() != 0.0){
price = fridgeProductRequest.price();
}
if(product.isEmpty() || fridge.isEmpty()) return Optional.empty();
......@@ -81,6 +85,7 @@ public class FridgeService {
.amount(fridgeProductRequest.amount())
.daysToExpiration(fridgeProductRequest.days())
.purchaseDate(java.sql.Date.valueOf(LocalDate.now()))
.buyPrice(price)
.build());
fridgeRepository.save(fridge.get());
......@@ -192,7 +197,7 @@ public class FridgeService {
group.setLevel(GroupUtil.getLevel(group.getPoints()));
}
groupRepository.save(group);
return Optional.of(wasteRepository.save(Waste.builder().amount(fridgeProductAsso1.getAmount()).unit(fridgeProductAsso1.getEan().getUnit()).ean(fridgeProductAsso1.getEan()).groupId(fridgeProductAsso1.getFridgeId().getGroup()).timestamp(new Timestamp(System.currentTimeMillis())).build()));
return Optional.of(wasteRepository.save(Waste.builder().buyPrice(fridgeProductAsso1.getBuyPrice()).amount(fridgeProductAsso1.getAmount()).unit(fridgeProductAsso1.getEan().getUnit()).ean(fridgeProductAsso1.getEan()).groupId(fridgeProductAsso1.getFridgeId().getGroup()).timestamp(new Timestamp(System.currentTimeMillis())).build()));
}
......
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