diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/config/CorsConfig.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/config/CorsConfig.java index 89269aecb78c262a773fd5450721eb6d46b861e7..8c238f9a2cf906d2cecd9626de76b690a14c3590 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/config/CorsConfig.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/config/CorsConfig.java @@ -35,7 +35,7 @@ public class CorsConfig { public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins(Arrays.asList("https://localhost", - "https://10.24.21.135", + "https://10.24.38.136", domainProperty.domain() ).toArray(String[]::new)) .allowedMethods(Arrays.asList( 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 90e7b988dba61214b5413a4efdfd1b319a88ea36..356e8000e76d093054efb2235c197fd28cd7bd18 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 @@ -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.entity.group.Group; 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.user.UserService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; /** @@ -57,30 +61,51 @@ public class GroupController { * Creates a new group * * @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}") - public ResponseEntity<Group> createGroup(@RequestBody Group group, - @PathVariable("username") String username) { - - if(group.getGroupName().equals("") || - userService.getUserFromUsername(username).isEmpty() || - groupService.getGroupById(group.getGroupId()).isPresent()) { - return ResponseEntity.badRequest().build(); + @PostMapping("/group") + public ResponseEntity<?> createGroup(@RequestBody GroupRequest groupRequest) { + if (groupRequest.groupName() == null || groupRequest.groupName().trim().length() < 3) { + return ResponseEntity.badRequest().body("Group name must be at least 3 characters long."); } + 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); - group1.addUser(UserGroupAsso.builder() - .groupAuthority("ADMIN") - .group(group1) - .user(userService.getUserFromUsername(username).get()) + User user = optionalUser.get(); + + UserGroupId userGroupId = UserGroupId.builder() + .username(user.getUsername()) + .groupId(createdGroup.getGroupId()) + .build(); + + createdGroup.addUser(UserGroupAsso.builder() + .id(userGroupId) + .primaryGroup(false) + .groupAuthority("ADMIN") + .group(createdGroup) + .user(user) .build()); - return groupService.updateGroup(group1).map(ResponseEntity::ok) - .orElseGet(() -> ResponseEntity.badRequest().build()); + GroupResponse groupResponse = new GroupResponse(createdGroup.getGroupId(), createdGroup.getLinkCode()); + + groupService.updateGroup(createdGroup); + + return ResponseEntity.ok(groupResponse); } + /** * Gets the level of a group * @@ -184,24 +209,49 @@ public class GroupController { * @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 groupService.getGroupByLinkCode(linkCode) - .flatMap(group -> userService.getUserFromUsername(username) - .flatMap(user -> { - UserGroupAsso userGroupAsso = UserGroupAsso.builder() - .group(group) - .user(user) - .build(); - user.addGroup(userGroupAsso); - userService.updateUser(user); - return Optional.of(userGroupAsso); - })) - .map(ResponseEntity::ok) - .orElseGet(() -> ResponseEntity.notFound().build()); + @PostMapping("/connection") + public ResponseEntity<?> addConnection(@RequestBody GroupConnectionRequest groupConnectionRequest) { + Optional<Group> optionalGroup = groupService.getGroupByLinkCode(groupConnectionRequest.linkCode()); + + if (optionalGroup.isEmpty()) { + return ResponseEntity.badRequest().body("Invalid link code."); + } + + Optional<User> optionalUser = userService.getUserFromUsername(groupConnectionRequest.username()); + + if (optionalUser.isEmpty()) { + return ResponseEntity.badRequest().body("Invalid username."); + } + + Group group = optionalGroup.get(); + 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. * diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/UserGroupAssoRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/UserGroupAssoRepository.java index 12cbc62829ca7b76e60f1dfe8d0be6919c87fa0e..1580965a7e73b07091c4abad6843da268bb83555 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/UserGroupAssoRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/group/UserGroupAssoRepository.java @@ -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 */ Optional<UserGroupAsso> findFirstByUserAndPrimaryGroup(User user, boolean primaryGroup); + + /** + * Finds UserGroupAsso by UserGroupId + */ + Optional<UserGroupAsso> findByUserGroupId(UserGroupId userGroupId); } 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 980d4f8ee82f0134a1c78434bc23f3e0dabafe2b..3542c83ea4941ce2a12c3e355e1df04be6bb8683 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 @@ -4,9 +4,11 @@ import lombok.AllArgsConstructor; import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList; import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge; 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.group.FridgeRepository; import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository; +import ntnu.idatt2016.v233.SmartMat.repository.group.UserGroupAssoRepository; import ntnu.idatt2016.v233.SmartMat.util.GroupUtil; import org.springframework.stereotype.Service; @@ -26,6 +28,8 @@ public class GroupService { private final GroupRepository groupRepository; + private final UserGroupAssoRepository userGroupAssoRepository; + private final FridgeRepository fridgeRepository; private ShoppingListRepository shoppingListRepository; @@ -160,4 +164,19 @@ public class GroupService { public Optional<Group> getGroupByLinkCode(String 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(); + } }