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 88a6a5741e7b62993215f0792c2baf7f37ef96fc..13fad13236cb47833c8b7a67a02c745837d3f92a 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 @@ -3,6 +3,8 @@ package ntnu.idatt2016.v233.SmartMat.controller.group; import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.entity.group.Group; import ntnu.idatt2016.v233.SmartMat.service.group.GroupService; +import ntnu.idatt2016.v233.SmartMat.service.group.UserGroupAssoService; +import ntnu.idatt2016.v233.SmartMat.service.user.UserService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -18,6 +20,8 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/api/groups") public class GroupController { private final GroupService groupService; + private final UserService userService; + private final UserGroupAssoService userGroupAssoService; /** * Gets a group by its name @@ -49,15 +53,18 @@ public class GroupController { * @param group the group to create * @return a ResponseEntity containing the created group if it was created successfully, or a 400 if it wasn't */ - @PostMapping("/group") - public ResponseEntity<Group> createGroup(@RequestBody Group group) { + @PostMapping("/group/{username}") + public ResponseEntity<Group> createGroup(@RequestBody Group group, + @PathVariable("username") String username) { if(groupService.getGroupById(group.getGroupId()).isPresent()) { return ResponseEntity.badRequest().build(); } if(group.getGroupName().equals("")) { return ResponseEntity.badRequest().build(); } - return ResponseEntity.ok(groupService.createGroup(group)); + Group group1 = groupService.createGroup(group); + userGroupAssoService.addPersonToGroup(username,group1.getLinkCode(), "ADMIN"); + return ResponseEntity.ok(group1); } /** 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 4500f8be0bdc62265cb9d6545c158c7fac351370..b8596ed7603fd708b76e953c9704c1d936c1ba28 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 @@ -32,10 +32,34 @@ public class UserGroupAssoController { return userGroupAssoService.getInformationByGroupId(groupId).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } + /** + * Handles the HTTP PUT request to change the primary group of a user. + * + * @param username the username of the user whose primary group is to be changed + * @param newId the ID of the new primary group + * @param oldId the ID of the old primary group + * @return a ResponseEntity object containing an HTTP status code and the updated UserGroupAsso object, + * or a ResponseEntity object with an HTTP status code indicating that the request was not successful + */ @PutMapping("/markNewPrimary/{username}/{oldId}/{newId}") public ResponseEntity<?> markNewPrimaryGroup(@PathVariable("username") String username, @PathVariable("newId") long newId, @PathVariable("oldId") long oldId){ return userGroupAssoService.changePrimaryGroup(oldId,newId,username).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } + + /** + * Handles the HTTP POST request to add a new connection between a user and a group. + * + * @param username the username of the user to add to the group + * @param linkCode the code of the group to which the user is to be added + * @return a ResponseEntity object containing an HTTP status code and the newly created UserGroupAsso object, + * or a ResponseEntity object with an HTTP status code indicating that the request was not successful + */ + @PostMapping("/connection/{username}/{linkCode}") + public ResponseEntity<?> addConnection(@PathVariable("username") String username, + @PathVariable("linkCode") String linkCode){ + return userGroupAssoService.addPersonToGroup(username,linkCode,"USER").map(ResponseEntity::ok).orElseGet(()-> ResponseEntity.notFound().build()); + } + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java index 135482e8af9c0e1c8b35cf150ba88fdd3558e17c..9465917aa7f4fe71a8b2cb6c88c5528d3ed1b611 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/GroupRepository.java @@ -50,4 +50,7 @@ public interface GroupRepository extends JpaRepository<Group, Long> { @Query(value = "SELECT link_code FROM groups", nativeQuery = true) List<String> findAllLinkCode(); + + Optional<Group> findByLinkCode(String linkCode); + } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/UserGroupAssoRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/UserGroupAssoRepository.java index 3d3905d5bd01fdc362a35ed10b0127b590711a62..12cbc62829ca7b76e60f1dfe8d0be6919c87fa0e 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/UserGroupAssoRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/UserGroupAssoRepository.java @@ -31,5 +31,23 @@ public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, Us */ List<UserGroupAsso> findAllByGroup(Group group); + /** + * Finds all UserGroupAsso objects associated with the specified group and user. + * + * @param group the group for which to find the user-group associations + * @param user the user for which to find the user-group associations + * @return an optional object containing a list of UserGroupAsso objects associated with the specified group and user, + * or an empty optional if no such user-group associations exist in the database + */ Optional<UserGroupAsso> findAllByGroupAndUser(Group group, User user); + + /** + * Finds the first UserGroupAsso object associated with the specified user and primary group status. + * + * @param user the user for which to find the user-group association + * @param primaryGroup the primary group status for which to find the user-group association + * @return an optional object containing the first UserGroupAsso object associated with the specified user and primary group status, + * or an empty optional if no such user-group associations exist in the database + */ + Optional<UserGroupAsso> findFirstByUserAndPrimaryGroup(User user, boolean primaryGroup); } 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 736dabbc7e78fd003da06ffdb74227f14a97126e..9dfb2ca2df60d3be55bdecf3114f03c7926d1f80 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 @@ -68,18 +68,32 @@ public class UserGroupAssoService { Optional<Group> newGroup = groupRepository.findByGroupId(newId); Optional<User> user = userRepository.findByUsername(username); - if (oldGroup.isEmpty()) return Optional.empty(); if (newGroup.isEmpty()) return Optional.empty(); if (user.isEmpty()) return Optional.empty(); - UserGroupAsso userGroupAsso = userGroupAssoRepository.findAllByGroupAndUser(oldGroup.get(),user.get()).get(); - userGroupAsso.setPrimaryGroup(false); - - userGroupAssoRepository.save(userGroupAsso); + UserGroupAsso userGroupAsso; + if (oldGroup.isPresent()){ + userGroupAsso = userGroupAssoRepository.findAllByGroupAndUser(oldGroup.get(),user.get()).get(); + userGroupAsso.setPrimaryGroup(false); + userGroupAssoRepository.save(userGroupAsso); + } userGroupAsso = userGroupAssoRepository.findAllByGroupAndUser(newGroup.get(),user.get()).get(); userGroupAsso.setPrimaryGroup(true); userGroupAssoRepository.save(userGroupAsso); return Optional.of(userGroupAsso); } + + 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(); + if(user.isEmpty()) return Optional.empty(); + if(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).isPresent()) return Optional.of("User is already 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()); + } + } diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java index cef46e453576e9991d25b7f3f2b435f612177f2d..af91ac2e63165c7dba1097a85961615b446752b2 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java @@ -1,7 +1,10 @@ package ntnu.idatt2016.v233.SmartMat.controller.group; import ntnu.idatt2016.v233.SmartMat.entity.group.Group; +import ntnu.idatt2016.v233.SmartMat.entity.user.User; import ntnu.idatt2016.v233.SmartMat.service.group.GroupService; +import ntnu.idatt2016.v233.SmartMat.service.group.UserGroupAssoService; +import ntnu.idatt2016.v233.SmartMat.service.user.UserService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -10,6 +13,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.userdetails.UserDetailsService; +import java.sql.Date; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -20,12 +24,21 @@ class GroupControllerTest { @Mock private GroupService groupService; + @Mock + private UserService userService; + + @Mock + private UserGroupAssoService userGroupAssoService; + @Mock private UserDetailsService userDetailsService; + private User user; + @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); + user = User.builder().username("olavPro").firstName("Olav").lastName("Gamer").email("OlavGamer@gamil.com").dateOfBirth(Date.valueOf("1999-09-09")).build(); } @Test @@ -35,7 +48,7 @@ class GroupControllerTest { when(groupService.getGroupByName("testGroup")).thenReturn(Optional.of(group)); - GroupController controller = new GroupController(groupService); + GroupController controller = new GroupController(groupService,userService,userGroupAssoService); ResponseEntity<Group> response = controller.getGroupByName("testGroup"); @@ -50,7 +63,7 @@ class GroupControllerTest { when(groupService.getGroupById(1L)).thenReturn(Optional.of(group)); - GroupController controller = new GroupController(groupService); + GroupController controller = new GroupController(groupService,userService,userGroupAssoService); ResponseEntity<Group> response = controller.getGroupById(1L); @@ -66,9 +79,9 @@ class GroupControllerTest { when(groupService.createGroup(group)).thenReturn(group); - GroupController controller = new GroupController(groupService); + GroupController controller = new GroupController(groupService,userService,userGroupAssoService); - ResponseEntity<Group> response = controller.createGroup(group); + ResponseEntity<Group> response = controller.createGroup(group,"olavPro"); assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals(group, response.getBody());