Skip to content
Snippets Groups Projects
Commit c50b440f authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

Merge branch 'feature/243-add-get-group-by-username-endpoint' into 'main'

Resolve "Add get group by username endpoint"

Closes #243

See merge request idatt2106-v23-03/backend!182
parents 860843e5 84d7a41a
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.stream.Collectors;
/**
* Controller for groups API, providing endpoints for group management
......@@ -324,4 +325,14 @@ public class GroupController {
}
}
/**
* Handles the HTTP DELETE request to remove a user from a group.
* @param auth the authentication object containing the username of the user
* @return a ResponseEntity object containing the list of groups the user is associated with and an HTTP status code of 200,
*/
@GetMapping("/")
public ResponseEntity<List<UserGroupAsso>> getAllGroupsByUser(Authentication auth) {
return ResponseEntity.ok(groupService.getUserGroupAssoByUserName(auth.getName()));
}
}
......@@ -23,12 +23,12 @@ public class FridgeProductAsso {
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name = "fridge_id")
@JsonIgnoreProperties({"products"})
@JsonIgnore
private Fridge fridgeId;
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name = "ean")
@JsonIgnoreProperties({"fridges"})
@JsonIgnore
private Product ean;
@Column(name = "purchase_date")
......
package ntnu.idatt2016.v233.SmartMat.entity.group;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.*;
......@@ -26,13 +27,13 @@ public class UserGroupAsso {
@ManyToOne
@MapsId("username")
@JoinColumn(name = "username")
@JsonIgnoreProperties("group")
@JsonIgnore
private User user;
@ManyToOne
@MapsId("group_id")
@JoinColumn(name = "group_id")
@JsonIgnoreProperties({"group", "user"})
@JsonIgnoreProperties({"user", "fridge", "shoppingList"})
private Group group;
@Column(name = "primary_group")
......
......@@ -73,4 +73,12 @@ public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, Us
* @return
*/
String findAuthorityByUser_UsernameAndGroup_GroupId(String username, long groupId);
/**
* Finds all groups a user is a member of
* @param username the username of the user
* @return a list of all groups the user is a member of
*/
List<UserGroupAsso> findAllByUserUsername(String username);
}
......@@ -219,4 +219,14 @@ public class GroupService {
Optional<UserGroupAsso> userGroupAsso = getUserGroupAsso(username, groupId);
return userGroupAsso.map(UserGroupAsso::getGroupAuthority).orElseThrow(() -> new IllegalArgumentException("User is not associated with group"));
}
/**
* Gets all user group associations for a user
* @param username the username of the user
* @return a list of all user group associations for the user
*/
public List<UserGroupAsso> getUserGroupAssoByUserName(String username) {
return userGroupAssoRepository.findAllByUserUsername(username);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment