From 72720f4b97f6a3c8ed131bd7b424f3a1d5b561e8 Mon Sep 17 00:00:00 2001
From: Anders Austlid <andemau@ntnu.no>
Date: Thu, 27 Apr 2023 13:08:49 +0200
Subject: [PATCH] Refactored group connection endpoint to take a request body,
 and perform some validation checks

---
 .../controller/group/GroupController.java     | 29 +++++++++++++++----
 .../request/group/GroupConnectionRequest.java |  9 ++++++
 .../SmartMat/service/group/GroupService.java  | 11 +++++++
 3 files changed, 43 insertions(+), 6 deletions(-)
 create mode 100644 src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/group/GroupConnectionRequest.java

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 d9a4e76d..b9b74e57 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
@@ -1,12 +1,14 @@
 package ntnu.idatt2016.v233.SmartMat.controller.group;
 
 import lombok.AllArgsConstructor;
+import ntnu.idatt2016.v233.SmartMat.dto.request.group.GroupConnectionRequest;
 import ntnu.idatt2016.v233.SmartMat.dto.request.group.GroupRequest;
 import ntnu.idatt2016.v233.SmartMat.dto.response.group.GroupResponse;
 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.*;
 
@@ -24,6 +26,7 @@ import java.util.List;
 @RequestMapping("/api/groups")
 public class GroupController {
     private final GroupService groupService;
+    private final UserService userService;
     private final UserGroupAssoService userGroupAssoService;
 
     /**
@@ -64,6 +67,11 @@ public class GroupController {
         if(groupRequest.groupName().equals("")) {
             return ResponseEntity.badRequest().body("Group name cannot be empty");
         }
+
+        if(userService.getUserFromUsername(groupRequest.username()).isEmpty()) {
+            return ResponseEntity.badRequest().body("User does not exist");
+        }
+
         Group group = new Group();
         group.setGroupName(groupRequest.groupName());
 
@@ -159,15 +167,24 @@ public class GroupController {
     /**
      * 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
+     * @param groupConnectionRequest the request object containing the username and link code of the user and group to be connected
      * @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());
+    @PostMapping("/connection")
+    public ResponseEntity<?> addConnection(@RequestBody GroupConnectionRequest groupConnectionRequest){
+        if(groupConnectionRequest.username().isEmpty() || groupConnectionRequest.linkCode().isEmpty()){
+            return ResponseEntity.badRequest().body("Username or link code cannot be empty");
+        }
+        if(groupService.getGroupByLinkCode(groupConnectionRequest.linkCode()).isEmpty()){
+            return ResponseEntity.badRequest().body("Invalid link code");
+        }
+        if(userService.getUserFromUsername(groupConnectionRequest.username()).isEmpty()){
+            return ResponseEntity.badRequest().body("Invalid username");
+        }
+
+        userGroupAssoService.addPersonToGroup(groupConnectionRequest.username(), groupConnectionRequest.linkCode(), "USER");
+        return ResponseEntity.ok().body(groupConnectionRequest.username());
     }
 
     /**
diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/group/GroupConnectionRequest.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/group/GroupConnectionRequest.java
new file mode 100644
index 00000000..90b8f27a
--- /dev/null
+++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/group/GroupConnectionRequest.java
@@ -0,0 +1,9 @@
+package ntnu.idatt2016.v233.SmartMat.dto.request.group;
+
+/**
+ * GroupConnectionRequest is a record class representing a request to connect to a group.
+ * @param username the username of the user connecting to the group
+ * @param linkCode the link code of the group
+ */
+public record GroupConnectionRequest(String username, String linkCode) {
+}
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 615b3e29..7cdf5cdd 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
@@ -147,4 +147,15 @@ public class GroupService {
             fridgeRepository.save(fridge.get());
         }
     }
+
+    /**
+     * Gets a group by link code
+     *
+     * @param linkCode the link code of the group
+     *                 the group must exist
+     * @return the group with the given link code
+     */
+    public Optional<Group> getGroupByLinkCode(String linkCode) {
+        return groupRepository.findByLinkCode(linkCode);
+    }
 }
-- 
GitLab