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

added get group endpoint

parent 48f1575a
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ import org.springframework.security.core.Authentication; ...@@ -19,6 +19,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* Controller for groups API, providing endpoints for group management * Controller for groups API, providing endpoints for group management
...@@ -324,4 +325,14 @@ public class GroupController { ...@@ -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 { ...@@ -23,12 +23,12 @@ public class FridgeProductAsso {
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name = "fridge_id") @JoinColumn(name = "fridge_id")
@JsonIgnoreProperties({"products"}) @JsonIgnore
private Fridge fridgeId; private Fridge fridgeId;
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name = "ean") @JoinColumn(name = "ean")
@JsonIgnoreProperties({"fridges"}) @JsonIgnore
private Product ean; private Product ean;
@Column(name = "purchase_date") @Column(name = "purchase_date")
......
package ntnu.idatt2016.v233.SmartMat.entity.group; package ntnu.idatt2016.v233.SmartMat.entity.group;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
...@@ -26,13 +27,13 @@ public class UserGroupAsso { ...@@ -26,13 +27,13 @@ public class UserGroupAsso {
@ManyToOne @ManyToOne
@MapsId("username") @MapsId("username")
@JoinColumn(name = "username") @JoinColumn(name = "username")
@JsonIgnoreProperties("group") @JsonIgnore
private User user; private User user;
@ManyToOne @ManyToOne
@MapsId("group_id") @MapsId("group_id")
@JoinColumn(name = "group_id") @JoinColumn(name = "group_id")
@JsonIgnoreProperties({"group", "user"}) @JsonIgnoreProperties({"user", "fridge", "shoppingList"})
private Group group; private Group group;
@Column(name = "primary_group") @Column(name = "primary_group")
......
...@@ -73,4 +73,12 @@ public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, Us ...@@ -73,4 +73,12 @@ public interface UserGroupAssoRepository extends JpaRepository<UserGroupAsso, Us
* @return * @return
*/ */
String findAuthorityByUser_UsernameAndGroup_GroupId(String username, long groupId); 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 { ...@@ -219,4 +219,14 @@ public class GroupService {
Optional<UserGroupAsso> userGroupAsso = getUserGroupAsso(username, groupId); Optional<UserGroupAsso> userGroupAsso = getUserGroupAsso(username, groupId);
return userGroupAsso.map(UserGroupAsso::getGroupAuthority).orElseThrow(() -> new IllegalArgumentException("User is not associated with group")); 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.
Finish editing this message first!
Please register or to comment