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 13fad13236cb47833c8b7a67a02c745837d3f92a..3163e7518bf7a1b7129989b8d5b84302abc6e805 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 @@ -2,25 +2,27 @@ package ntnu.idatt2016.v233.SmartMat.controller.group; import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.entity.group.Group; +import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso; 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.*; +import java.util.List; + /** * Controller for groups API, providing endpoints for group management * - * @author Anders Austlid - * @version 1.0 - * @since 20.04.2023 + * @author Anders Austlid, Pedro Cardona + * @version 1.1 + * @since 27.04.2023 */ @AllArgsConstructor @RestController @RequestMapping("/api/groups") public class GroupController { private final GroupService groupService; - private final UserService userService; private final UserGroupAssoService userGroupAssoService; /** @@ -28,7 +30,7 @@ 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("/group/{groupName}") + @GetMapping("/{groupName}") public ResponseEntity<Group> getGroupByName(@PathVariable("groupName") String groupName){ return groupService.getGroupByName(groupName) .map(ResponseEntity::ok) @@ -40,7 +42,7 @@ public class GroupController { * @param groupId the id of the group * @return a ResponseEntity containing the group if it exists, or a 404 if it doesn't */ - @GetMapping("/group/id/{groupId}") + @GetMapping("/id/{groupId}") public ResponseEntity<Group> getGroupById(@PathVariable("groupId") long groupId){ return groupService.getGroupById(groupId) .map(ResponseEntity::ok) @@ -53,7 +55,7 @@ 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/{username}") + @PostMapping("/{username}") public ResponseEntity<Group> createGroup(@RequestBody Group group, @PathVariable("username") String username) { if(groupService.getGroupById(group.getGroupId()).isPresent()) { @@ -73,7 +75,7 @@ public class GroupController { * @param groupId the id of the group * @return a ResponseEntity containing the level of the group if it exists, or a 404 if it doesn't */ - @GetMapping("/group/{groupId}/level") + @GetMapping("/{groupId}/level") public ResponseEntity<Long> getGroupLevel(@PathVariable("groupId") long groupId) { return groupService.getGroupById(groupId) .map(group -> ResponseEntity.ok(group.getLevel())) @@ -87,7 +89,7 @@ public class GroupController { * @param exp the new experience points of the group * @return a ResponseEntity containing the new level of the group, or a 404 Not Found response if no Group with the given ID was found */ - @PutMapping("/group/{groupId}/newLevel/{exp}") + @PutMapping("/{groupId}/newLevel/{exp}") public ResponseEntity<Long> setNewLevel(@PathVariable("groupId") long groupId, @PathVariable("exp") long exp) { return groupService.setLevelByGroupId(groupId, exp) .map(group -> ResponseEntity.ok(group.getLevel())) @@ -101,7 +103,7 @@ public class GroupController { * @param groupId the ID of the group to query * @return a ResponseEntity containing the progress of the current level as a percentage, or a 404 Not Found response if no Group with the given ID was found */ - @GetMapping("/group/{groupId}/progress") + @GetMapping("/{groupId}/progress") public ResponseEntity<Integer> getProgressOfLevel(@PathVariable("groupId") long groupId) { return groupService.getProgressOfLevel(groupId) .map(ResponseEntity::ok) @@ -114,10 +116,68 @@ public class GroupController { * @param groupId the ID of the group to update * @return a ResponseEntity with a Boolean value indicating whether the operation was successful */ - @PutMapping("/group/{groupId}/changeOpen") + @PutMapping("/{groupId}/changeOpen") public ResponseEntity<Boolean> changeOpenValue(@PathVariable("groupId") long groupId) { return groupService.OpenOrCloseGroup(groupId) .map(ResponseEntity::ok) .orElseGet(() -> ResponseEntity.notFound().build()); } + + /** + * 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. + * + * @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 + */ + @GetMapping("/information/{groupId}") + public ResponseEntity<List<UserGroupAsso>> getInformationByGroupId(@PathVariable("groupId") long groupId){ + 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()); + } + + /** + * 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/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java deleted file mode 100644 index af91ac2e63165c7dba1097a85961615b446752b2..0000000000000000000000000000000000000000 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java +++ /dev/null @@ -1,89 +0,0 @@ -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; -import org.mockito.MockitoAnnotations; -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; -import static org.mockito.Mockito.when; - -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 - void getGroupByName() { - Group group = new Group(); - group.setGroupName("testGroup"); - - when(groupService.getGroupByName("testGroup")).thenReturn(Optional.of(group)); - - GroupController controller = new GroupController(groupService,userService,userGroupAssoService); - - ResponseEntity<Group> response = controller.getGroupByName("testGroup"); - - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(group, response.getBody()); - } - - @Test - void getGroupById() { - Group group = new Group(); - group.setGroupId(1L); - - when(groupService.getGroupById(1L)).thenReturn(Optional.of(group)); - - GroupController controller = new GroupController(groupService,userService,userGroupAssoService); - - ResponseEntity<Group> response = controller.getGroupById(1L); - - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(group, response.getBody()); - } - - @Test - void createGroup() { - Group group = new Group(); - group.setGroupId(1L); - group.setGroupName("testGroup"); - - when(groupService.createGroup(group)).thenReturn(group); - - GroupController controller = new GroupController(groupService,userService,userGroupAssoService); - - ResponseEntity<Group> response = controller.createGroup(group,"olavPro"); - - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(group, response.getBody()); - } -} diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/UserGroupAssoControllerTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/UserGroupAssoControllerTest.java deleted file mode 100644 index d89954f2ca0ea6adbaebf6650870d351749b7021..0000000000000000000000000000000000000000 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/UserGroupAssoControllerTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package ntnu.idatt2016.v233.SmartMat.controller.group; - -import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso; -import ntnu.idatt2016.v233.SmartMat.service.group.UserGroupAssoService; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; - -import java.util.Arrays; -import java.util.List; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -public class UserGroupAssoControllerTest { - - @InjectMocks - private UserGroupAssoController userGroupAssoController; - - @Mock - private UserGroupAssoService userGroupAssoService; - - private List<UserGroupAsso> userGroupAssoList; - - @BeforeEach - public void setUp() { - UserGroupAsso userGroupAsso1 = new UserGroupAsso(); - UserGroupAsso userGroupAsso2 = new UserGroupAsso(); - // Set properties for userGroupAsso1 and userGroupAsso2 - userGroupAssoList = Arrays.asList(userGroupAsso1, userGroupAsso2); - } - - @Test - public void getInformationByGroupId_found() { - long groupId = 1L; - when(userGroupAssoService.getInformationByGroupId(groupId)).thenReturn(Optional.of(userGroupAssoList)); - - ResponseEntity<List<UserGroupAsso>> response = userGroupAssoController.getInformationByGroupId(groupId); - - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(userGroupAssoList, response.getBody()); - } - - @Test - public void getInformationByGroupId_notFound() { - long groupId = 1L; - when(userGroupAssoService.getInformationByGroupId(groupId)).thenReturn(Optional.empty()); - - ResponseEntity<List<UserGroupAsso>> response = userGroupAssoController.getInformationByGroupId(groupId); - - assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); - } -}