diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java index 35bed18ee90307ef81702aed4357594815ae39e7..9265609330a6a1164792c8cdcc9116e08afa3360 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/FridgeController.java @@ -67,11 +67,22 @@ public class FridgeController { @DeleteMapping("/group/delete/product/{fridgeProductId}/{amount}") public ResponseEntity<?> deleteAmountFridgeProduct(@PathVariable("fridgeProductId") long fridgeProductId, - @PathVariable("amount") double amount){ - return fridgeService.deleteAmountFromFridge(fridgeProductId,amount).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); + @PathVariable("amount") String amountStr) { + try { + double amount = Double.parseDouble(amountStr); + + if (amount < 0.0) { + return ResponseEntity.badRequest().body("Amount must be greater than or equal to 0."); + } + + return fridgeService.deleteAmountFromFridge(fridgeProductId, amount).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); + } catch (NumberFormatException e) { + return ResponseEntity.badRequest().body("Invalid amount format. Please provide a valid number."); + } } + /** * Deletes a product from the fridge * @param fridgeProductId the id of the fridge product association diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupController.java index 20d87eac40604cc4cb13cf966e08def340c27523..d646a5e51836736cc0a3818f8d08e0f520d25d9c 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupController.java @@ -38,23 +38,36 @@ public class GroupController { * @param groupName the name of the group * @return a ResponseEntity containing the group if it exists, or a 404 if it doesn't */ - @GetMapping("/{groupName}") + /*@GetMapping("/{groupName}") public ResponseEntity<Group> getGroupByName(@PathVariable("groupName") String groupName){ return groupService.getGroupByName(groupName) .map(ResponseEntity::ok) .orElseGet(() -> ResponseEntity.notFound().build()); - } + }*/ /** * Gets a group by its id * @param groupId the id of the group * @return a ResponseEntity containing the group if it exists, or a 404 if it doesn't */ - @GetMapping("/id/{groupId}") - public ResponseEntity<Group> getGroupById(@PathVariable("groupId") long groupId){ - return groupService.getGroupById(groupId) - .map(ResponseEntity::ok) - .orElseGet(() -> ResponseEntity.notFound().build()); + @GetMapping("/{groupId}") + public ResponseEntity<?> getGroupById(@PathVariable("groupId") long groupId){ + Optional<Group> group = groupService.getGroupById(groupId); + if(group.isPresent()) { + List<UserGroupAsso> users = group.get().getUser(); + Map<String, String> usernames = new HashMap<>(); + for (UserGroupAsso user : users) { + String userAuthority = groupService.getUserGroupAsso(user.getUser().getUsername(), groupId) + .get().getGroupAuthority(); + + if(groupService.getUserGroupAsso(user.getUser().getUsername(), groupId).isPresent()) { + usernames.put(user.getUser().getUsername(), userAuthority); + } + usernames.put(user.getUser().getUsername(), userAuthority); + } + return ResponseEntity.ok(usernames); + } + return ResponseEntity.badRequest().body("Group not found."); } /** @@ -164,6 +177,8 @@ public class GroupController { * Returns a response entity containing a list of UserGroupAsso objects related to the given groupId. * If no user-group associations are found for the given groupId, a not-found response entity is returned. * + * TODO: Remove? Seems pointless + * * @param groupId the ID of the group to retrieve user-group associations for * @return a response entity containing a list of UserGroupAsso objects related to the given groupId, or a not-found response entity if no associations are found */