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

Merge branch 'bugfix/groupcontroller-endpoint-refactor' into 'main'

makeNewPrimaryGroup endpoint is fixed

See merge request idatt2106-v23-03/backend!155
parents ffca589b f7e9ec87
No related branches found
No related tags found
No related merge requests found
......@@ -92,7 +92,7 @@ public class GroupController {
createdGroup.addUser(UserGroupAsso.builder()
.id(userGroupId)
.primaryGroup(false)
.primaryGroup(true)
.groupAuthority("ADMIN")
.group(createdGroup)
.user(user)
......@@ -178,28 +178,46 @@ public class GroupController {
* 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 groupId the ID of the group
* @param newPrimaryGroupId the ID of the new 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}/{groupId}/{newId}")
@PutMapping("/markNewPrimary/{username}/{newPrimaryGroupId}")
public ResponseEntity<?> markNewPrimaryGroup(@PathVariable("username") String username,
@PathVariable("groupId") long groupId){
return userService.getUserFromUsername(username)
.flatMap(user ->{
user.getGroup().forEach(userGroupAsso -> {
if(userGroupAsso.getGroup().getGroupId() != groupId){
userGroupAsso.setPrimaryGroup(false);
}
if(userGroupAsso.getGroup().getGroupId() == groupId){
userGroupAsso.setPrimaryGroup(true);
}
});
return Optional.of(userService.updateUser(user));
}
)
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
@PathVariable("newPrimaryGroupId") long newPrimaryGroupId) {
Optional<User> optionalUser = userService.getUserFromUsername(username);
if (optionalUser.isEmpty()) {
return ResponseEntity.badRequest().body("Invalid username.");
}
User user = optionalUser.get();
Optional<UserGroupAsso> oldPrimaryOpt = groupService.findPrimaryUserGroupAssoForUser(username);
if (oldPrimaryOpt.isEmpty()) {
return ResponseEntity.badRequest().body("No primary group found for the user.");
}
UserGroupAsso oldPrimary = oldPrimaryOpt.get();
oldPrimary.setPrimaryGroup(false);
groupService.updateUserGroupAsso(oldPrimary);
Optional<UserGroupAsso> newPrimaryOpt = groupService.getUserGroupAsso(username, newPrimaryGroupId);
if (newPrimaryOpt.isEmpty()) {
return ResponseEntity.badRequest().body("Invalid new primary group ID.");
}
UserGroupAsso newPrimary = newPrimaryOpt.get();
newPrimary.setPrimaryGroup(true);
groupService.updateUserGroupAsso(newPrimary);
userService.updateUser(user);
Map<String, Object> responseBody = new HashMap<>();
responseBody.put("username", username);
responseBody.put("oldPrimaryGroupId", oldPrimary.getGroup().getGroupId());
responseBody.put("newPrimaryGroupId", newPrimary.getGroup().getGroupId());
return ResponseEntity.ok(responseBody);
}
/**
......
......@@ -53,6 +53,16 @@ public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, Us
/**
* Finds UserGroupAsso by UserGroupId
*
* @param userGroupId the UserGroupId to find by
* @return an optional containing the UserGroupAsso if it exists
*/
Optional<UserGroupAsso> findByUserGroupId(UserGroupId userGroupId);
/**
* Finds primary group for given user
* @param username the username of the user
* @return an optional containing the UserGroupAsso if it exists
*/
Optional<UserGroupAsso> findByUser_UsernameAndPrimaryGroupTrue(String username);
}
......@@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
import ntnu.idatt2016.v233.SmartMat.entity.group.Fridge;
import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupAsso;
import ntnu.idatt2016.v233.SmartMat.entity.group.UserGroupId;
import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListRepository;
import ntnu.idatt2016.v233.SmartMat.repository.group.FridgeRepository;
......@@ -179,4 +180,27 @@ public class GroupService {
return userGroupAssoRepository.findById(userGroupId).isPresent();
}
/**
* Updates a user group association
*
* @param userGroupAsso the user group association to update
* @return the updated user group association
*/
public UserGroupAsso updateUserGroupAsso(UserGroupAsso userGroupAsso) {
return userGroupAssoRepository.save(userGroupAsso);
}
public Optional<UserGroupAsso> findPrimaryUserGroupAssoForUser(String username) {
return userGroupAssoRepository.findByUser_UsernameAndPrimaryGroupTrue(username);
}
public Optional<UserGroupAsso> getUserGroupAsso(String username, Long groupId) {
UserGroupId userGroupId = UserGroupId.builder()
.username(username)
.groupId(groupId)
.build();
return userGroupAssoRepository.findById(userGroupId);
}
}
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