From 9c1dceacf35c4eedcc3327786335db1b22bd43ae Mon Sep 17 00:00:00 2001 From: birkon <birkon@stud.ntnu.no> Date: Tue, 2 May 2023 14:05:44 +0200 Subject: [PATCH] added remove from group endpoint --- .../controller/group/GroupController.java | 45 +++++++++++++++++++ .../v233/SmartMat/entity/group/Group.java | 6 ++- .../v233/SmartMat/entity/user/User.java | 3 +- .../SmartMat/service/group/GroupService.java | 24 ++++++++++ 4 files changed, 74 insertions(+), 4 deletions(-) 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 e485351b..65097694 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 @@ -335,4 +335,49 @@ public class GroupController { return ResponseEntity.ok(groupService.getUserGroupAssoByUserName(auth.getName())); } + + /** + * Handles the HTTP DELETE request to remove a user from a group. + * @param groupId the ID of the group to get the members of + * @param username the username of the user to remove from the group + * @param auth the authentication object containing the username of the user + * @return a ResponseEntity object containing the list of groups the + * user is associated with and an HTTP status code of 200, + */ + @DeleteMapping("/removeUser/{groupId}/{username}") + public ResponseEntity<?> removeUserFromGroup(@PathVariable("groupId") long groupId, + @PathVariable("username") String username, + Authentication auth) { + Optional<User> groupAdminOpt = userService.getUserFromUsername(auth.getName()); + if (groupAdminOpt.isPresent()) { + User groupAdmin = groupAdminOpt.get(); + if (!(groupService.isUserAssociatedWithGroup(groupAdmin.getUsername(), groupId) + && (groupService.getUserGroupAssoAuthority(groupAdmin.getUsername(), groupId).equals("ADMIN")) + || groupAdmin.getUsername().equals(username))) + return ResponseEntity.status(HttpStatus.FORBIDDEN).body("You are not authorized to remove this user."); + } + + Optional<Group> groupOpt = groupService.getGroupById(groupId); + Optional<User> userOpt = userService.getUserFromUsername(username); + + if (groupOpt.isEmpty() || userOpt.isEmpty()) { + return ResponseEntity.notFound().build(); + } + + User user = userOpt.get(); + UserGroupAsso userGroupAsso = user.getGroup().stream() + .filter(asso -> asso.getGroup().getGroupId() == groupId) + .findFirst() + .orElse(null); + + if (userGroupAsso != null) { + groupService.removeUserFromGroup(userGroupAsso); + return ResponseEntity.ok("User removed successfully."); + } else { + return ResponseEntity.notFound().build(); + } + } + + + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java index c390052e..51dc151a 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java @@ -56,8 +56,10 @@ public class Group { private ShoppingList shoppingList; - @OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, - fetch = FetchType.LAZY, mappedBy = "group") + @OneToMany(cascade = {CascadeType.ALL}, + fetch = FetchType.LAZY, mappedBy = "group" + , orphanRemoval = true + ) @JsonIgnoreProperties("group") private List<UserGroupAsso> user = new ArrayList<>(); diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java index 8f9f5967..a3cb4f8f 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java @@ -55,8 +55,7 @@ public class User implements UserDetails { private Date dateOfBirth; - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) - @JoinColumn(name = "username") + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, mappedBy = "user") @JsonIgnore private List<UserGroupAsso> group; diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java index e29d843e..53daf543 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/GroupService.java @@ -1,5 +1,6 @@ package ntnu.idatt2016.v233.SmartMat.service.group; +import jakarta.transaction.Transactional; import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList; import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; @@ -229,4 +230,27 @@ public class GroupService { public List<UserGroupAsso> getUserGroupAssoByUserName(String username) { return userGroupAssoRepository.findAllByUserUsername(username); } + + /** + * removes user_group relatioon + * @param username the username of the user + * @param groupId the id of the group + * @return true if the user is the owner of the group, false otherwise + */ + @Transactional + public boolean removeUserFromGroup(UserGroupAsso userGroup) { + Group group = groupRepository.findByGroupId(userGroup.getGroup().getGroupId()) + .orElseThrow(() -> new IllegalArgumentException("Group does not exist")); + + group.getUser().remove(userGroup); + + if (group.getUser().isEmpty()) { + groupRepository.delete(group); + } else { + groupRepository.save(group); + } + + userGroupAssoRepository.delete(userGroup); + return true; + } } -- GitLab