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

Method to change the main group was added

parent fe0a9599
No related branches found
No related tags found
No related merge requests found
......@@ -5,10 +5,7 @@ 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 org.springframework.web.bind.annotation.*;
import java.util.List;
......@@ -35,4 +32,21 @@ public class UserGroupAssoController {
return userGroupAssoService.getInformationByGroupId(groupId).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
/**
* Updates the user's primary group in the database.
*
* @param username the username of the user whose primary group will be updated
* @param newId the ID of the new primary group to set
* @return a ResponseEntity with a 200 OK status code
*/
@PutMapping("/markNewPrimary/{username}/{newId}")
public ResponseEntity<?> markNewPrimaryGroup(@PathVariable("username") String username,
@PathVariable("newId") long newId){
try {
userGroupAssoService.changePrimaryGroup(newId,username);
}finally {
return ResponseEntity.ok().build();
}
}
}
......@@ -4,30 +4,38 @@ 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.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Optional;
public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, UserGroupId> {
/**
* 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);
/**
* 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.
* Unmarks the old primary group for the given user by setting the 'primary_group' column to FALSE in the 'user_group' table.
*
* @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
* @param username the username of the user
*/
@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);
@Query(value = "UPDATE user_group SET primary_group = FALSE where username = :username", nativeQuery = true)
void unmarkOldPrimaryGroup(@Param("username") String username);
/**
* Retrieves a list of UserGroupAsso objects for the specified Group object.
* Marks the given group as the new primary group for the given user by setting the 'primary_group' column to TRUE in the 'user_group' table.
*
* @param group the Group object to retrieve information for
* @return a List of UserGroupAsso objects for the specified Group object
* @param username the username of the user
* @param groupId the ID of the group to mark as primary
*/
List<UserGroupAsso> findAllByGroup(Group group);
@Query(value = "UPDATE user_group SET primary_group = TRUE where username = :username and group_id = :groupId ", nativeQuery = true)
void markNewPrimaryGroup(@Param("username") String username,@Param("groupId") long groupId);
}
......@@ -20,11 +20,12 @@ public class UserGroupAssoService {
private UserGroupAssoRepository userGroupAssoRepository;
private GroupRepository groupRepository;
public void save(User user, Group group, boolean primaryGroup) {
public void save(User user, Group group, String userLevel) {
UserGroupAsso userGroupTable1 = new UserGroupAsso();
userGroupTable1.setGroup(group);
userGroupTable1.setUser(user);
userGroupTable1.setPrimaryGroup(primaryGroup);
userGroupTable1.setPrimaryGroup(true);
userGroupTable1.setGroupAuthority(userLevel);
userGroupTable1.setId(UserGroupId.builder()
.groupId(group.getGroupId())
.username(user.getUsername())
......@@ -51,4 +52,19 @@ public class UserGroupAssoService {
return Optional.empty();
}
/**
* Changes the primary group of a user by unmarking the current primary group and marking a new primary group.
*
* @param newId The ID of the new primary group.
* @param username The username of the user whose primary group is being changed.
*/
public void changePrimaryGroup(long newId, String username){
try {
userGroupAssoRepository.unmarkOldPrimaryGroup(username);
}finally {
userGroupAssoRepository.markNewPrimaryGroup(username,newId);
}
}
}
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