From 2d19a2d63a34ccc0fd81401f77f201a4193db851 Mon Sep 17 00:00:00 2001
From: Anders Austlid <andemau@ntnu.no>
Date: Mon, 24 Apr 2023 14:20:52 +0200
Subject: [PATCH] Added endpoint to get group level by groupId

---
 .../controller/group/GroupController.java       | 13 +++++++++++++
 .../repository/group/GroupRepository.java       | 17 +++++++++++++++--
 .../SmartMat/service/group/GroupService.java    | 10 ++++++++++
 3 files changed, 38 insertions(+), 2 deletions(-)

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 f0a8071d..e9840c07 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 70e47b4c..5d5ff0ad 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 79d10bcc..0c739253 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);
+    }
 }
-- 
GitLab