Skip to content
Snippets Groups Projects
Commit 72720f4b authored by Anders Austlid's avatar Anders Austlid
Browse files

Refactored group connection endpoint to take a request body, and perform some validation checks

parent a116d73f
No related branches found
No related tags found
No related merge requests found
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());
}
/**
......
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) {
}
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment