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

Merge branch 'bugfix/fridge-controller-delete-amount' into 'main'

Bugfix/fridge controller delete amount

Closes #224

See merge request idatt2106-v23-03/backend!169
parents 3301c0e9 397d8649
No related branches found
No related tags found
No related merge requests found
...@@ -67,11 +67,22 @@ public class FridgeController { ...@@ -67,11 +67,22 @@ public class FridgeController {
@DeleteMapping("/group/delete/product/{fridgeProductId}/{amount}") @DeleteMapping("/group/delete/product/{fridgeProductId}/{amount}")
public ResponseEntity<?> deleteAmountFridgeProduct(@PathVariable("fridgeProductId") long fridgeProductId, public ResponseEntity<?> deleteAmountFridgeProduct(@PathVariable("fridgeProductId") long fridgeProductId,
@PathVariable("amount") double amount){ @PathVariable("amount") String amountStr) {
return fridgeService.deleteAmountFromFridge(fridgeProductId,amount).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); 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 * Deletes a product from the fridge
* @param fridgeProductId the id of the fridge product association * @param fridgeProductId the id of the fridge product association
......
...@@ -38,23 +38,36 @@ public class GroupController { ...@@ -38,23 +38,36 @@ public class GroupController {
* @param groupName the name of the group * @param groupName the name of the group
* @return a ResponseEntity containing the group if it exists, or a 404 if it doesn't * @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){ public ResponseEntity<Group> getGroupByName(@PathVariable("groupName") String groupName){
return groupService.getGroupByName(groupName) return groupService.getGroupByName(groupName)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build()); .orElseGet(() -> ResponseEntity.notFound().build());
} }*/
/** /**
* Gets a group by its id * Gets a group by its id
* @param groupId the id of the group * @param groupId the id of the group
* @return a ResponseEntity containing the group if it exists, or a 404 if it doesn't * @return a ResponseEntity containing the group if it exists, or a 404 if it doesn't
*/ */
@GetMapping("/id/{groupId}") @GetMapping("/{groupId}")
public ResponseEntity<Group> getGroupById(@PathVariable("groupId") long groupId){ public ResponseEntity<?> getGroupById(@PathVariable("groupId") long groupId){
return groupService.getGroupById(groupId) Optional<Group> group = groupService.getGroupById(groupId);
.map(ResponseEntity::ok) if(group.isPresent()) {
.orElseGet(() -> ResponseEntity.notFound().build()); 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 { ...@@ -164,6 +177,8 @@ public class GroupController {
* Returns a response entity containing a list of UserGroupAsso objects related to the given groupId. * 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. * 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 * @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 * @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
*/ */
......
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