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

Working on refactor/bugfix for group post endpoint

parent c05ae9ab
No related branches found
No related tags found
No related merge requests found
...@@ -59,26 +59,30 @@ public class GroupController { ...@@ -59,26 +59,30 @@ public class GroupController {
* @param groupRequest the group to create * @param groupRequest the group to create
* @return a ResponseEntity containing the created group if it was created successfully, or a 400 if it wasn't * @return a ResponseEntity containing the created group if it was created successfully, or a 400 if it wasn't
*/ */
@PostMapping("/{username}") @PostMapping("/group")
public ResponseEntity<Group> createGroup(@RequestBody Group group, public ResponseEntity<GroupResponse> createGroup(@RequestBody GroupRequest groupRequest) {
@PathVariable("username") String username) {
if(group.getGroupName().equals("") || if(groupRequest.groupName().equals("") ||
userService.getUserFromUsername(username).isEmpty() || userService.getUserFromUsername(groupRequest.username()).isEmpty() ||
groupService.getGroupById(group.getGroupId()).isPresent()) { groupService.getGroupByName(groupRequest.groupName()).isPresent()) {
return ResponseEntity.badRequest().build(); return ResponseEntity.badRequest().build();
} }
Group group = new Group();
group.setGroupName(groupRequest.groupName());
Group group1 = groupService.createGroup(group); Group createdGroup = groupService.createGroup(group);
group1.addUser(UserGroupAsso.builder() createdGroup.addUser(UserGroupAsso.builder()
.groupAuthority("ADMIN") .groupAuthority("ADMIN")
.group(group1) .group(createdGroup)
.user(userService.getUserFromUsername(username).get()) .user(userService.getUserFromUsername(groupRequest.username()).get())
.build()); .build());
return groupService.updateGroup(group1).map(ResponseEntity::ok) GroupResponse groupResponse = new GroupResponse(createdGroup.getGroupId(), createdGroup.getLinkCode());
.orElseGet(() -> ResponseEntity.badRequest().build());
groupService.updateGroup(createdGroup);
return ResponseEntity.ok(groupResponse);
} }
/** /**
...@@ -184,11 +188,10 @@ public class GroupController { ...@@ -184,11 +188,10 @@ public class GroupController {
* @return a ResponseEntity object containing an HTTP status code and the newly created UserGroupAsso object, * @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 * or a ResponseEntity object with an HTTP status code indicating that the request was not successful
*/ */
@PostMapping("/connection/{username}/{linkCode}") @PostMapping("/connection")
public ResponseEntity<?> addConnection(@PathVariable("username") String username, public ResponseEntity<?> addConnection(@RequestBody GroupConnectionRequest groupConnectionRequest){
@PathVariable("linkCode") String linkCode){ return groupService.getGroupByLinkCode(groupConnectionRequest.linkCode())
return groupService.getGroupByLinkCode(linkCode) .flatMap(group -> userService.getUserFromUsername(groupConnectionRequest.username())
.flatMap(group -> userService.getUserFromUsername(username)
.flatMap(user -> { .flatMap(user -> {
UserGroupAsso userGroupAsso = UserGroupAsso.builder() UserGroupAsso userGroupAsso = UserGroupAsso.builder()
.group(group) .group(group)
......
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