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 new file mode 100644 index 0000000000000000000000000000000000000000..e5af6f113f8f2b96df3e0919a846276e130061d6 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/UserGroupAssoController.java @@ -0,0 +1,38 @@ +package ntnu.idatt2016.v233.SmartMat.controller.group; + + +import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso; +import ntnu.idatt2016.v233.SmartMat.service.group.UserGroupAssoService; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * The UserGroupAssoController class is a REST controller that handles HTTP requests related to the user-group association. + * It provides endpoints for getting information about users in a group. + */ +@AllArgsConstructor +@RestController +@RequestMapping("/api/userGroup") +public class UserGroupAssoController { + + private final UserGroupAssoService userGroupAssoService; + + /** + * 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()); + } + +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/UserGroupAsso.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/UserGroupAsso.java index 687f1f2912abab41e35f7f5ec5e7a3fb32e45448..169031a859ffa1b46b7b252cdbb80933b8e0c747 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/UserGroupAsso.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/UserGroupAsso.java @@ -38,4 +38,7 @@ public class UserGroupAsso { @Column(name = "primary_group") private Boolean primaryGroup; + + @Column(name ="group_authority") + private String groupAuthority; } 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 aa389d502de1187f8e26f03997520ba5a492106c..e05736af7c6670bedbd8c864c79045fceaeccfc8 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 @@ -1,8 +1,33 @@ package ntnu.idatt2016.v233.SmartMat.repository.group; +import ntnu.idatt2016.v233.SmartMat.entity.group.Group; import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupId; import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; +import java.util.Optional; public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, UserGroupId> { + + + /** + * Creates a new user-group association with the specified user and group IDs and assigns the user as the primary group administrator with ADMIN authority. + * + * @param username the username of the user to add to the group + * @param groupId the ID of the group to add the user to + * @return an Optional containing the Group object for the newly created user-group association, or an empty Optional if the operation failed + */ + @Query(value = "insert into user_group ('username', 'group_id', 'primary_group','group_authority') values (:username , :groupId , TRUE , 'ADMIN')",nativeQuery = true) + Optional<Group> createConnectionAfterCreateGroup(String username, long groupId); + + /** + * Retrieves a list of UserGroupAsso objects for the specified Group object. + * + * @param group the Group object to retrieve information for + * @return a List of UserGroupAsso objects for the specified Group object + */ + List<UserGroupAsso> findAllByGroup(Group 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 63d196849336cd8439da664e340f1c72a7b7fe85..2fda7348d30b239845679b1f74937af6c5aaa2f0 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 @@ -121,5 +121,4 @@ public class GroupService { } return Optional.empty(); } - } 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 41d1875db674e1d79bcddb359d9fa09bb63ff94e..1bd8c29b51978c104f872170ec625ecc30784f19 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 @@ -6,14 +6,19 @@ import ntnu.idatt2016.v233.SmartMat.entity.group.Group; import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupId; import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso; import ntnu.idatt2016.v233.SmartMat.entity.user.User; +import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository; import ntnu.idatt2016.v233.SmartMat.repository.group.UserGroupAssoRepository; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.Optional; + @Service @AllArgsConstructor public class UserGroupAssoService { private UserGroupAssoRepository userGroupAssoRepository; + private GroupRepository groupRepository; public void save(User user, Group group, boolean primaryGroup) { UserGroupAsso userGroupTable1 = new UserGroupAsso(); @@ -31,4 +36,19 @@ public class UserGroupAssoService { group.addUser(userGroupTable1); } + + /** + * Retrieves a list of UserGroupAsso objects for the specified group ID. + * + * @param id the ID of the group to retrieve information for + * @return an Optional containing a list of UserGroupAsso objects for the specified group ID, or an empty Optional if no information is found + */ + public Optional<List<UserGroupAsso>> getInformationByGroupId(long id){ + if (groupRepository.findByGroupId(id).isPresent()){ + List<UserGroupAsso> list = userGroupAssoRepository.findAllByGroup(groupRepository.findByGroupId(id).get()); + if(!list.isEmpty()) return Optional.of(list); + } + return Optional.empty(); + } + }