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

Merge branch 'feature/addPersonToGroup' into 'main'

Endpoint for get all person of a group was added

See merge request idatt2106-v23-03/backend!111
parents 374b24a6 fe0a9599
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.entity.group.UserGroupAsso;
import ntnu.idatt2016.v233.SmartMat.service.group.UserGroupAssoService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* The UserGroupAssoController class is a REST controller that handles HTTP requests related to the user-group association.
* It provides endpoints for getting information about users in a group.
*/
@AllArgsConstructor
@RestController
@RequestMapping("/api/userGroup")
public class UserGroupAssoController {
private final UserGroupAssoService userGroupAssoService;
/**
* Returns a response entity containing a list of UserGroupAsso objects related to the given groupId.
* If no user-group associations are found for the given groupId, a not-found response entity is returned.
*
* @param groupId the ID of the group to retrieve user-group associations for
* @return a response entity containing a list of UserGroupAsso objects related to the given groupId, or a not-found response entity if no associations are found
*/
@GetMapping("/information/{groupId}")
public ResponseEntity<List<UserGroupAsso>> getInformationByGroupId(@PathVariable("groupId") long groupId){
return userGroupAssoService.getInformationByGroupId(groupId).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
}
......@@ -38,4 +38,7 @@ public class UserGroupAsso {
@Column(name = "primary_group")
private Boolean primaryGroup;
@Column(name ="group_authority")
private String groupAuthority;
}
package ntnu.idatt2016.v233.SmartMat.repository.group;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupId;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Optional;
public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, UserGroupId> {
/**
* Creates a new user-group association with the specified user and group IDs and assigns the user as the primary group administrator with ADMIN authority.
*
* @param username the username of the user to add to the group
* @param groupId the ID of the group to add the user to
* @return an Optional containing the Group object for the newly created user-group association, or an empty Optional if the operation failed
*/
@Query(value = "insert into user_group ('username', 'group_id', 'primary_group','group_authority') values (:username , :groupId , TRUE , 'ADMIN')",nativeQuery = true)
Optional<Group> createConnectionAfterCreateGroup(String username, long groupId);
/**
* Retrieves a list of UserGroupAsso objects for the specified Group object.
*
* @param group the Group object to retrieve information for
* @return a List of UserGroupAsso objects for the specified Group object
*/
List<UserGroupAsso> findAllByGroup(Group group);
}
......@@ -121,5 +121,4 @@ public class GroupService {
}
return Optional.empty();
}
}
......@@ -6,14 +6,19 @@ import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupId;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository;
import ntnu.idatt2016.v233.SmartMat.repository.group.UserGroupAssoRepository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
@AllArgsConstructor
public class UserGroupAssoService {
private UserGroupAssoRepository userGroupAssoRepository;
private GroupRepository groupRepository;
public void save(User user, Group group, boolean primaryGroup) {
UserGroupAsso userGroupTable1 = new UserGroupAsso();
......@@ -31,4 +36,19 @@ public class UserGroupAssoService {
group.addUser(userGroupTable1);
}
/**
* Retrieves a list of UserGroupAsso objects for the specified group ID.
*
* @param id the ID of the group to retrieve information for
* @return an Optional containing a list of UserGroupAsso objects for the specified group ID, or an empty Optional if no information is found
*/
public Optional<List<UserGroupAsso>> getInformationByGroupId(long id){
if (groupRepository.findByGroupId(id).isPresent()){
List<UserGroupAsso> list = userGroupAssoRepository.findAllByGroup(groupRepository.findByGroupId(id).get());
if(!list.isEmpty()) return Optional.of(list);
}
return Optional.empty();
}
}
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