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 f0a8071d717fd530638ffe946685dd80f183b481..e9840c072f912159ea2caa4255de5600a6043059 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 @@ -59,4 +59,17 @@ public class GroupController { } return ResponseEntity.ok(groupService.createGroup(group)); } + + /** + * Gets the level of a group + * + * @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") + public ResponseEntity<Integer> getGroupLevel(@PathVariable("groupId") long groupId) { + return groupService.getGroupById(groupId) + .map(group -> ResponseEntity.ok(group.getLevel())) + .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 70e47b4cfee4fc40d8b82d4aba23d4eda4dc8c4a..5d5ff0ad46d65e92bfc4980329639ec1e62ae217 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 @@ -11,10 +11,15 @@ import java.util.Optional; * Repository for groups * * @author Stian Lyng, Anders Austlid - * @version 1.2 - * @since 21.04.2023 + * @version 1.3 + * @since 24.04.2023 */ public interface GroupRepository extends JpaRepository<Group, Long> { + /** + * Finds a group by group name + * @param name the name of the group + * @return the group with the given name if it exists + */ Optional<Group> findByGroupName(String name); /** @@ -23,4 +28,12 @@ public interface GroupRepository extends JpaRepository<Group, Long> { * @return list of groups with the given achievement */ List<Group> findAllByAchievementsAchievementName(String achievementName); + + /** + * Gets group level by group id + * + * @param id the id of the group + * @return the level of the group + */ + int getLevelByGroupId(long id); } 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 79d10bccc0ba123839de3243c3b62698141e6bea..0c7392532d8e47a54c89238fc5321128957a6b9a 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 @@ -53,4 +53,14 @@ public class GroupService { throw new IllegalArgumentException("Group already exists"); return groupRepository.save(group); } + + /** + * Gets the level of a group + * + * @param id the id of the group + * @return the level of the group + */ + public int getLevelByGroupId(long id) { + return groupRepository.getLevelByGroupId(id); + } }