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

Merge branch 'bugfix/groupcontroller-endpoint-refactor' into 'main'

Bugfix/groupcontroller endpoint refactor

Closes #208 and #204

See merge request idatt2106-v23-03/backend!154
parents 4c0911fc ed0e4e35
No related branches found
No related tags found
No related merge requests found
...@@ -35,7 +35,7 @@ public class CorsConfig { ...@@ -35,7 +35,7 @@ public class CorsConfig {
public void addCorsMappings(CorsRegistry registry) { public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") registry.addMapping("/**")
.allowedOrigins(Arrays.asList("https://localhost", .allowedOrigins(Arrays.asList("https://localhost",
"https://10.24.21.135", "https://10.24.38.136",
domainProperty.domain() domainProperty.domain()
).toArray(String[]::new)) ).toArray(String[]::new))
.allowedMethods(Arrays.asList( .allowedMethods(Arrays.asList(
......
...@@ -6,12 +6,16 @@ import ntnu.idatt2016.v233.SmartMat.dto.request.group.GroupRequest; ...@@ -6,12 +6,16 @@ import ntnu.idatt2016.v233.SmartMat.dto.request.group.GroupRequest;
import ntnu.idatt2016.v233.SmartMat.dto.response.group.GroupResponse; import ntnu.idatt2016.v233.SmartMat.dto.response.group.GroupResponse;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group; import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso; import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupId;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import ntnu.idatt2016.v233.SmartMat.service.group.GroupService; import ntnu.idatt2016.v233.SmartMat.service.group.GroupService;
import ntnu.idatt2016.v233.SmartMat.service.user.UserService; import ntnu.idatt2016.v233.SmartMat.service.user.UserService;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
/** /**
...@@ -57,30 +61,51 @@ public class GroupController { ...@@ -57,30 +61,51 @@ public class GroupController {
* Creates a new group * Creates a new group
* *
* @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 the group name is invalid or already exists, or if the username is invalid
*/ */
@PostMapping("/{username}") @PostMapping("/group")
public ResponseEntity<Group> createGroup(@RequestBody Group group, public ResponseEntity<?> createGroup(@RequestBody GroupRequest groupRequest) {
@PathVariable("username") String username) { if (groupRequest.groupName() == null || groupRequest.groupName().trim().length() < 3) {
return ResponseEntity.badRequest().body("Group name must be at least 3 characters long.");
if(group.getGroupName().equals("") ||
userService.getUserFromUsername(username).isEmpty() ||
groupService.getGroupById(group.getGroupId()).isPresent()) {
return ResponseEntity.badRequest().build();
} }
if (groupService.getGroupByName(groupRequest.groupName()).isPresent()) {
return ResponseEntity.badRequest().body("Group name already exists.");
}
Optional<User> optionalUser = userService.getUserFromUsername(groupRequest.username());
if (optionalUser.isEmpty()) {
return ResponseEntity.badRequest().body("Invalid username.");
}
Group group = new Group();
group.setGroupName(groupRequest.groupName());
Group createdGroup = groupService.createGroup(group);
Group group1 = groupService.createGroup(group); User user = optionalUser.get();
group1.addUser(UserGroupAsso.builder()
.groupAuthority("ADMIN") UserGroupId userGroupId = UserGroupId.builder()
.group(group1) .username(user.getUsername())
.user(userService.getUserFromUsername(username).get()) .groupId(createdGroup.getGroupId())
.build();
createdGroup.addUser(UserGroupAsso.builder()
.id(userGroupId)
.primaryGroup(false)
.groupAuthority("ADMIN")
.group(createdGroup)
.user(user)
.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);
} }
/** /**
* Gets the level of a group * Gets the level of a group
* *
...@@ -184,24 +209,49 @@ public class GroupController { ...@@ -184,24 +209,49 @@ 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){ Optional<Group> optionalGroup = groupService.getGroupByLinkCode(groupConnectionRequest.linkCode());
return groupService.getGroupByLinkCode(linkCode)
.flatMap(group -> userService.getUserFromUsername(username) if (optionalGroup.isEmpty()) {
.flatMap(user -> { return ResponseEntity.badRequest().body("Invalid link code.");
UserGroupAsso userGroupAsso = UserGroupAsso.builder() }
.group(group)
.user(user) Optional<User> optionalUser = userService.getUserFromUsername(groupConnectionRequest.username());
.build();
user.addGroup(userGroupAsso); if (optionalUser.isEmpty()) {
userService.updateUser(user); return ResponseEntity.badRequest().body("Invalid username.");
return Optional.of(userGroupAsso); }
}))
.map(ResponseEntity::ok) Group group = optionalGroup.get();
.orElseGet(() -> ResponseEntity.notFound().build()); User user = optionalUser.get();
if (groupService.isUserAssociatedWithGroup(user.getUsername(), group.getGroupId())) {
return ResponseEntity.badRequest().body("User is already associated with the group.");
}
UserGroupId userGroupId = UserGroupId.builder()
.username(user.getUsername())
.groupId(group.getGroupId())
.build();
UserGroupAsso userGroupAsso = UserGroupAsso.builder()
.id(userGroupId)
.group(group)
.user(user)
.build();
user.addGroup(userGroupAsso);
userService.updateUser(user);
Map<String, Object> responseBody = new HashMap<>();
responseBody.put("groupId", group.getGroupId());
responseBody.put("username", user.getUsername());
return ResponseEntity.ok(responseBody);
} }
/** /**
* Changes the authority level of a user in a group. * Changes the authority level of a user in a group.
* *
......
...@@ -50,4 +50,9 @@ public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, Us ...@@ -50,4 +50,9 @@ public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, Us
* or an empty optional if no such user-group associations exist in the database * or an empty optional if no such user-group associations exist in the database
*/ */
Optional<UserGroupAsso> findFirstByUserAndPrimaryGroup(User user, boolean primaryGroup); Optional<UserGroupAsso> findFirstByUserAndPrimaryGroup(User user, boolean primaryGroup);
/**
* Finds UserGroupAsso by UserGroupId
*/
Optional<UserGroupAsso> findByUserGroupId(UserGroupId userGroupId);
} }
...@@ -4,9 +4,11 @@ import lombok.AllArgsConstructor; ...@@ -4,9 +4,11 @@ import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList; import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group; import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupId;
import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListRepository; import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListRepository;
import ntnu.idatt2016.v233.SmartMat.repository.group.FridgeRepository; import ntnu.idatt2016.v233.SmartMat.repository.group.FridgeRepository;
import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository; import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository;
import ntnu.idatt2016.v233.SmartMat.repository.group.UserGroupAssoRepository;
import ntnu.idatt2016.v233.SmartMat.util.GroupUtil; import ntnu.idatt2016.v233.SmartMat.util.GroupUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -26,6 +28,8 @@ public class GroupService { ...@@ -26,6 +28,8 @@ public class GroupService {
private final GroupRepository groupRepository; private final GroupRepository groupRepository;
private final UserGroupAssoRepository userGroupAssoRepository;
private final FridgeRepository fridgeRepository; private final FridgeRepository fridgeRepository;
private ShoppingListRepository shoppingListRepository; private ShoppingListRepository shoppingListRepository;
...@@ -160,4 +164,19 @@ public class GroupService { ...@@ -160,4 +164,19 @@ public class GroupService {
public Optional<Group> getGroupByLinkCode(String linkCode) { public Optional<Group> getGroupByLinkCode(String linkCode) {
return groupRepository.findByLinkCode(linkCode); return groupRepository.findByLinkCode(linkCode);
} }
/**
* Checks if a user is associated with a group
* @param username the username of the user
* @param groupId the id of the group
* @return true if the user is associated with the group, false otherwise
*/
public boolean isUserAssociatedWithGroup(String username, Long groupId) {
UserGroupId userGroupId = UserGroupId.builder()
.username(username)
.groupId(groupId)
.build();
return userGroupAssoRepository.findById(userGroupId).isPresent();
}
} }
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