Skip to content
Snippets Groups Projects
Commit 1df22139 authored by Anders Austlid's avatar Anders Austlid
Browse files

Some changes to user group asso classes

parent e33877b1
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());
}
}
......@@ -33,7 +33,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 userAuthority) {
UserGroupAsso userGroupTable1 = new UserGroupAsso();
userGroupTable1.setGroup(group);
userGroupTable1.setUser(user);
userGroupTable1.setPrimaryGroup(primaryGroup);
userGroupTable1.setPrimaryGroup(true);
userGroupTable1.setGroupAuthority(userAuthority);
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);
}
}
......@@ -55,7 +55,7 @@ public class UserGroupAssoServiceTest {
@Test
public void testSave() {
userGroupAssoService.save(user, group, true);
userGroupAssoService.save(user, group, "ADMIN");
verify(userGroupAssoRepository, times(1)).save(userGroupAsso);
verify(userRepository, times(1)).save(user);
......
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