Skip to content
Snippets Groups Projects
Commit feab9783 authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge branch 'feature/addEndpointForInvitePersonToGroup' into 'main'

All related to add person to group and change main group was added

See merge request idatt2106-v23-03/backend!123
parents 6afe71f9 75b0266f
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,8 @@ package ntnu.idatt2016.v233.SmartMat.controller.group;
import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
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.*;
......@@ -18,6 +20,8 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/api/groups")
public class GroupController {
private final GroupService groupService;
private final UserService userService;
private final UserGroupAssoService userGroupAssoService;
/**
* Gets a group by its name
......@@ -49,15 +53,18 @@ public class GroupController {
* @param group the group to create
* @return a ResponseEntity containing the created group if it was created successfully, or a 400 if it wasn't
*/
@PostMapping("/group")
public ResponseEntity<Group> createGroup(@RequestBody Group group) {
@PostMapping("/group/{username}")
public ResponseEntity<Group> createGroup(@RequestBody Group group,
@PathVariable("username") String username) {
if(groupService.getGroupById(group.getGroupId()).isPresent()) {
return ResponseEntity.badRequest().build();
}
if(group.getGroupName().equals("")) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(groupService.createGroup(group));
Group group1 = groupService.createGroup(group);
userGroupAssoService.addPersonToGroup(username,group1.getLinkCode(), "ADMIN");
return ResponseEntity.ok(group1);
}
/**
......
......@@ -32,10 +32,34 @@ public class UserGroupAssoController {
return userGroupAssoService.getInformationByGroupId(groupId).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Handles the HTTP PUT request to change the primary group of a user.
*
* @param username the username of the user whose primary group is to be changed
* @param newId the ID of the new primary group
* @param oldId the ID of the old primary group
* @return a ResponseEntity object containing an HTTP status code and the updated UserGroupAsso object,
* or a ResponseEntity object with an HTTP status code indicating that the request was not successful
*/
@PutMapping("/markNewPrimary/{username}/{oldId}/{newId}")
public ResponseEntity<?> markNewPrimaryGroup(@PathVariable("username") String username,
@PathVariable("newId") long newId,
@PathVariable("oldId") long oldId){
return userGroupAssoService.changePrimaryGroup(oldId,newId,username).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* 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
* @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());
}
}
......@@ -50,4 +50,7 @@ public interface GroupRepository extends JpaRepository<Group, Long> {
@Query(value = "SELECT link_code FROM groups", nativeQuery = true)
List<String> findAllLinkCode();
Optional<Group> findByLinkCode(String linkCode);
}
......@@ -31,5 +31,23 @@ public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, Us
*/
List<UserGroupAsso> findAllByGroup(Group group);
/**
* Finds all UserGroupAsso objects associated with the specified group and user.
*
* @param group the group for which to find the user-group associations
* @param user the user for which to find the user-group associations
* @return an optional object containing a list of UserGroupAsso objects associated with the specified group and user,
* or an empty optional if no such user-group associations exist in the database
*/
Optional<UserGroupAsso> findAllByGroupAndUser(Group group, User user);
/**
* Finds the first UserGroupAsso object associated with the specified user and primary group status.
*
* @param user the user for which to find the user-group association
* @param primaryGroup the primary group status for which to find the user-group association
* @return an optional object containing the first UserGroupAsso object associated with the specified user and primary group status,
* or an empty optional if no such user-group associations exist in the database
*/
Optional<UserGroupAsso> findFirstByUserAndPrimaryGroup(User user, boolean primaryGroup);
}
......@@ -68,18 +68,32 @@ public class UserGroupAssoService {
Optional<Group> newGroup = groupRepository.findByGroupId(newId);
Optional<User> user = userRepository.findByUsername(username);
if (oldGroup.isEmpty()) return Optional.empty();
if (newGroup.isEmpty()) return Optional.empty();
if (user.isEmpty()) return Optional.empty();
UserGroupAsso userGroupAsso = userGroupAssoRepository.findAllByGroupAndUser(oldGroup.get(),user.get()).get();
UserGroupAsso userGroupAsso;
if (oldGroup.isPresent()){
userGroupAsso = userGroupAssoRepository.findAllByGroupAndUser(oldGroup.get(),user.get()).get();
userGroupAsso.setPrimaryGroup(false);
userGroupAssoRepository.save(userGroupAsso);
}
userGroupAsso = userGroupAssoRepository.findAllByGroupAndUser(newGroup.get(),user.get()).get();
userGroupAsso.setPrimaryGroup(true);
userGroupAssoRepository.save(userGroupAsso);
return Optional.of(userGroupAsso);
}
public Optional<Object> addPersonToGroup(String username, String linkCode, String authority){
Optional<Group> group = groupRepository.findByLinkCode(linkCode);
Optional<User> user = userRepository.findByUsername(username);
if(group.isEmpty()) return Optional.empty();
if(user.isEmpty()) return Optional.empty();
if(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).isPresent()) return Optional.of("User is already in this group!");
save(user.get(),group.get(),authority);
changePrimaryGroup(userGroupAssoRepository.findFirstByUserAndPrimaryGroup(user.get(),true).get().getGroup().getGroupId(), group.get().getGroupId(),username);
return Optional.of(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).get());
}
}
package ntnu.idatt2016.v233.SmartMat.controller.group;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
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.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
......@@ -10,6 +13,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetailsService;
import java.sql.Date;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
......@@ -20,12 +24,21 @@ class GroupControllerTest {
@Mock
private GroupService groupService;
@Mock
private UserService userService;
@Mock
private UserGroupAssoService userGroupAssoService;
@Mock
private UserDetailsService userDetailsService;
private User user;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
user = User.builder().username("olavPro").firstName("Olav").lastName("Gamer").email("OlavGamer@gamil.com").dateOfBirth(Date.valueOf("1999-09-09")).build();
}
@Test
......@@ -35,7 +48,7 @@ class GroupControllerTest {
when(groupService.getGroupByName("testGroup")).thenReturn(Optional.of(group));
GroupController controller = new GroupController(groupService);
GroupController controller = new GroupController(groupService,userService,userGroupAssoService);
ResponseEntity<Group> response = controller.getGroupByName("testGroup");
......@@ -50,7 +63,7 @@ class GroupControllerTest {
when(groupService.getGroupById(1L)).thenReturn(Optional.of(group));
GroupController controller = new GroupController(groupService);
GroupController controller = new GroupController(groupService,userService,userGroupAssoService);
ResponseEntity<Group> response = controller.getGroupById(1L);
......@@ -66,9 +79,9 @@ class GroupControllerTest {
when(groupService.createGroup(group)).thenReturn(group);
GroupController controller = new GroupController(groupService);
GroupController controller = new GroupController(groupService,userService,userGroupAssoService);
ResponseEntity<Group> response = controller.createGroup(group);
ResponseEntity<Group> response = controller.createGroup(group,"olavPro");
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(group, response.getBody());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment