diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/UserGroupAssoController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/UserGroupAssoController.java index b8596ed7603fd708b76e953c9704c1d936c1ba28..e762e83a90cf90eee5d3971aa468e5fd94565a66 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/UserGroupAssoController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/UserGroupAssoController.java @@ -62,4 +62,20 @@ public class UserGroupAssoController { return userGroupAssoService.addPersonToGroup(username,linkCode,"USER").map(ResponseEntity::ok).orElseGet(()-> ResponseEntity.notFound().build()); } + /** + * Changes the authority level of a user in a group. + * + * @param groupId the ID of the group + * @param username the username of the user whose authority level is to be changed + * @param authority the new authority level of the user + * @return a ResponseEntity object containing the updated UserGroupAsso object and an HTTP status code of 200, + * or a ResponseEntity object with an HTTP status code of 404 if the group or user does not exist + */ + @PutMapping("/changeAuthority/{groupId}/{username}/{authority}") + public ResponseEntity<?> changeAuthority(@PathVariable("groupId") long groupId, + @PathVariable("username") String username, + @PathVariable("authority") String authority){ + return userGroupAssoService.changeAuthorityOfUser(username,groupId,authority).map(ResponseEntity::ok).orElseGet(() ->ResponseEntity.notFound().build()); + } + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/UserGroupAssoService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/UserGroupAssoService.java index 9dfb2ca2df60d3be55bdecf3114f03c7926d1f80..f07f9e911527497f0c8c60ba6bd020337ac3d5e0 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/UserGroupAssoService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/UserGroupAssoService.java @@ -26,7 +26,7 @@ public class UserGroupAssoService { UserGroupAsso userGroupTable1 = new UserGroupAsso(); userGroupTable1.setGroup(group); userGroupTable1.setUser(user); - userGroupTable1.setPrimaryGroup(true); + userGroupTable1.setPrimaryGroup(false); userGroupTable1.setGroupAuthority(authority); userGroupTable1.setId(UserGroupId.builder() .groupId(group.getGroupId()) @@ -84,8 +84,16 @@ public class UserGroupAssoService { return Optional.of(userGroupAsso); } + /** + * Adds a new user to a group. + * + * @param username the username of the user to add + * @param linkCode the link code of the group to which the user is to be added + * @param authority the authority level of the user in the group + * @return an Optional object containing the UserGroupAsso object of the user in the group, + * or an empty Optional if the group or user does not exist, or if the user is already in the group + */ public Optional<Object> addPersonToGroup(String username, String linkCode, String authority){ - Optional<Group> group = groupRepository.findByLinkCode(linkCode); Optional<User> user = userRepository.findByUsername(username); if(group.isEmpty()) return Optional.empty(); @@ -96,4 +104,22 @@ public class UserGroupAssoService { return Optional.of(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).get()); } + /** + * Changes the authority of a user in a group and updates the primary group if necessary. + * @param username the username of the user whose authority will be changed. + * @param groupId the ID of the group in which the user's authority will be changed. + * @param authority the new authority level for the user in the group. + * @return an Optional containing the updated UserGroupAsso object, or an empty Optional if the user or group does not exist, or if the user is not in the group. + */ + public Optional<Object> changeAuthorityOfUser(String username, long groupId, String authority){ + Optional<Group> group = groupRepository.findByGroupId(groupId); + Optional<User> user = userRepository.findByUsername(username); + if(group.isEmpty()) return Optional.empty(); + if(user.isEmpty()) return Optional.empty(); + if(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).isEmpty()) return Optional.of("User is not in this group!"); + save(user.get(),group.get(),authority); + changePrimaryGroup(userGroupAssoRepository.findFirstByUserAndPrimaryGroup(user.get(),true).get().getGroup().getGroupId(), group.get().getGroupId(),username); + return Optional.of(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).get()); + } + }