diff --git a/api/src/main/java/no/freshify/api/controller/ShoppingListController.java b/api/src/main/java/no/freshify/api/controller/ShoppingListController.java index cfd8a31d1ff3578fb49d5cfa122b3bf9e9878d2a..2c5b8d1b933bf9397e6ea19810c99a70e8781b03 100644 --- a/api/src/main/java/no/freshify/api/controller/ShoppingListController.java +++ b/api/src/main/java/no/freshify/api/controller/ShoppingListController.java @@ -106,22 +106,22 @@ public class ShoppingListController { * @throws HouseholdNotFoundException If the household is not found * @throws ItemTypeNotFoundException If the new item type is invalid */ + @PreAuthorize("hasPermission(#householdId, 'household', 'SUPERUSER')") @PutMapping - public ResponseEntity<HttpStatus> updateShoppingListEntry(@PathVariable("id") long householdId, + public ResponseEntity<Object> updateShoppingListEntry(@PathVariable("id") long householdId, @RequestBody ShoppingListEntryEditRequest requestBody) - throws InvalidItemCountException, HouseholdNotFoundException, ItemTypeNotFoundException, ShoppingListEntryNotFoundException { - Household household = householdService.findHouseholdByHouseholdId(householdId); + throws InvalidItemCountException, ShoppingListEntryNotFoundException { + ShoppingListEntry entry = shoppingListEntryMapper.fromShoppingListEntryEditRequest(requestBody); + ShoppingListEntry oldEntry = shoppingListEntryService.findShoppingListEntryById(requestBody.getId()); - ShoppingListEntry updatedEntry = new ShoppingListEntry(); - updatedEntry.setId(shoppingListEntryService.findShoppingListEntryByItemType(householdId, requestBody.getId()).getId()); - updatedEntry.setType(itemTypeService.getItemTypeById(requestBody.getId())); - updatedEntry.setCount(requestBody.getCount()); - updatedEntry.setSuggested(requestBody.getSuggested()); - updatedEntry.setChecked(requestBody.getChecked()); - updatedEntry.setHousehold(household); + if (oldEntry.getHousehold().getId() != householdId) + return ResponseEntity.status(HttpStatus.FORBIDDEN).body("The item does not belong to the household"); - shoppingListEntryService.updateShoppingListEntry(updatedEntry); + entry.setHousehold(oldEntry.getHousehold()); + entry.setAddedBy(oldEntry.getAddedBy()); + entry.setType(oldEntry.getType()); + shoppingListEntryService.updateShoppingListEntry(entry); return ResponseEntity.ok().build(); } diff --git a/api/src/main/java/no/freshify/api/model/mapper/ShoppingListEntryMapper.java b/api/src/main/java/no/freshify/api/model/mapper/ShoppingListEntryMapper.java index 452d34fdcb43c7a5f52531b155b0c0cc6dc586c2..6bf5be7580d1d79f89f71b424c54037e88fc668a 100644 --- a/api/src/main/java/no/freshify/api/model/mapper/ShoppingListEntryMapper.java +++ b/api/src/main/java/no/freshify/api/model/mapper/ShoppingListEntryMapper.java @@ -16,4 +16,6 @@ public abstract class ShoppingListEntryMapper { public abstract List<ShoppingListEntryResponse> toShoppingListEntryResponse(List<ShoppingListEntry> shoppingListEntries); public abstract ShoppingListEntry fromShoppingListEntryRequest(ShoppingListEntryRequest shoppingListEntryRequest); + + public abstract ShoppingListEntry fromShoppingListEntryEditRequest(ShoppingListEntryEditRequest shoppingListEntryEditRequest); } diff --git a/api/src/main/java/no/freshify/api/service/ShoppingListEntryService.java b/api/src/main/java/no/freshify/api/service/ShoppingListEntryService.java index c251d5c89d253785f066caa8305bf21c256ae2c9..a81bec85d40462795f9e14676e007bc4511fc0e7 100644 --- a/api/src/main/java/no/freshify/api/service/ShoppingListEntryService.java +++ b/api/src/main/java/no/freshify/api/service/ShoppingListEntryService.java @@ -41,12 +41,22 @@ public class ShoppingListEntryService { shoppingListEntryRepository.save(shoppingListEntry); } - public void updateShoppingListEntry(ShoppingListEntry updatedEntry) throws InvalidItemCountException { + public void updateShoppingListEntry(ShoppingListEntry updatedEntry) throws InvalidItemCountException, ShoppingListEntryNotFoundException { logger.info("Updating shopping list entry"); if (updatedEntry.getCount() <= 0) { logger.warn("Invalid operation. More items are removed than in shopping list."); throw new InvalidItemCountException(); } + ShoppingListEntry oldEntry = shoppingListEntryRepository.findById(updatedEntry.getId()).orElse(null); + if (oldEntry == null) { + logger.warn("Shopping list entry not found"); + throw new ShoppingListEntryNotFoundException(); + } + + if (updatedEntry.getHousehold() == null) updatedEntry.setHousehold(oldEntry.getHousehold()); + if (updatedEntry.getType() == null) updatedEntry.setType(oldEntry.getType()); + if (updatedEntry.getAddedBy() == null) updatedEntry.setAddedBy(oldEntry.getAddedBy()); + shoppingListEntryRepository.save(updatedEntry); } diff --git a/api/src/test/java/no/freshify/api/service/ShoppingListEntryServiceTest.java b/api/src/test/java/no/freshify/api/service/ShoppingListEntryServiceTest.java index 96befd0185a18f07cf57cd5b25f9699986c0b487..311e30ce646b21e1810498b68a0cde57794096b9 100644 --- a/api/src/test/java/no/freshify/api/service/ShoppingListEntryServiceTest.java +++ b/api/src/test/java/no/freshify/api/service/ShoppingListEntryServiceTest.java @@ -80,7 +80,8 @@ public class ShoppingListEntryServiceTest { } @Test - public void testUpdateShoppingListEntry() throws InvalidItemCountException { + public void testUpdateShoppingListEntry() throws InvalidItemCountException, ShoppingListEntryNotFoundException { + Mockito.when(shoppingListEntryRepository.findById(shoppingListEntry.getId())).thenReturn(Optional.of(shoppingListEntry)); shoppingListEntry.setCount(2L); shoppingListEntryService.updateShoppingListEntry(shoppingListEntry);