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

Merge branch 'bugfix/user-group-asso-changes' into 'main'

Bugfix/user group asso changes

See merge request idatt2106-v23-03/backend!118
parents 64334d5d af58877b
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,10 @@ public class UserGroupAssoController {
return userGroupAssoService.getInformationByGroupId(groupId).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}
@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());
}
}
......@@ -2,9 +2,7 @@ package ntnu.idatt2016.v233.SmartMat.entity.group;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;
import ntnu.idatt2016.v233.SmartMat.entity.user.User;
/**
......@@ -17,7 +15,7 @@ import ntnu.idatt2016.v233.SmartMat.entity.user.User;
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Getter @Setter
@Table(name = "user_group")
public class UserGroupAsso {
......@@ -33,7 +31,7 @@ public class UserGroupAsso {
@ManyToOne
@MapsId("group_id")
@JoinColumn(name = "group_id")
@JsonIgnoreProperties("user")
@JsonIgnoreProperties({"group", "user"})
private Group group;
@Column(name = "primary_group")
......
......@@ -3,6 +3,7 @@ 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 ntnu.idatt2016.v233.SmartMat.entity.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
......@@ -30,4 +31,5 @@ public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, Us
*/
List<UserGroupAsso> findAllByGroup(Group group);
Optional<UserGroupAsso> findAllByGroupAndUser(Group group, User user);
}
......@@ -22,11 +22,12 @@ public class UserGroupAssoService {
private GroupRepository groupRepository;
private final UserRepository userRepository;
public void save(User user, Group group, boolean primaryGroup) {
public void save(User user, Group group, String authority) {
UserGroupAsso userGroupTable1 = new UserGroupAsso();
userGroupTable1.setGroup(group);
userGroupTable1.setUser(user);
userGroupTable1.setPrimaryGroup(primaryGroup);
userGroupTable1.setPrimaryGroup(true);
userGroupTable1.setGroupAuthority(authority);
userGroupTable1.setId(UserGroupId.builder()
.groupId(group.getGroupId())
.username(user.getUsername())
......@@ -56,4 +57,29 @@ 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 Optional<Object> changePrimaryGroup(long oldId, long newId, String username){
Optional<Group> oldGroup = groupRepository.findByGroupId(oldId);
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.setPrimaryGroup(false);
userGroupAssoRepository.save(userGroupAsso);
userGroupAsso = userGroupAssoRepository.findAllByGroupAndUser(newGroup.get(),user.get()).get();
userGroupAsso.setPrimaryGroup(true);
userGroupAssoRepository.save(userGroupAsso);
return Optional.of(userGroupAsso);
}
}
......@@ -47,6 +47,7 @@ public class UserGroupAssoServiceTest {
userGroupAsso.setUser(user);
userGroupAsso.setGroup(group);
userGroupAsso.setPrimaryGroup(true);
userGroupAsso.setGroupAuthority("ADMIN");
userGroupAsso.setId(UserGroupId.builder()
.groupId(group.getGroupId())
.username(user.getUsername())
......@@ -55,9 +56,9 @@ public class UserGroupAssoServiceTest {
@Test
public void testSave() {
userGroupAssoService.save(user, group, true);
userGroupAssoService.save(user, group, "ADMIN");
verify(userGroupAssoRepository, times(1)).save(userGroupAsso);
verify(userGroupAssoRepository, times(1)).save(any(userGroupAsso.getClass()));
verify(userRepository, times(1)).save(user);
verify(groupRepository, times(1)).save(group);
}
......
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