Skip to content

Commit 591b045

Browse files
committed
내 그룹 목록 조회 api
1 parent fb6753a commit 591b045

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

polling-app-server/src/main/java/com/example/polls/controller/GroupController.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
import org.springframework.http.HttpStatus;
1212
import org.springframework.http.ResponseEntity;
1313
import org.springframework.security.core.annotation.AuthenticationPrincipal;
14-
import org.springframework.web.bind.annotation.PostMapping;
15-
import org.springframework.web.bind.annotation.RequestBody;
16-
import org.springframework.web.bind.annotation.RequestMapping;
17-
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.bind.annotation.*;
15+
16+
import java.util.List;
1817

1918
@RestController
2019
@RequestMapping("/api/groups")
@@ -45,4 +44,11 @@ public ResponseEntity<GroupSummaryResponse> createGroup(
4544
return ResponseEntity.status(HttpStatus.CREATED).body(response);
4645
}
4746

47+
@GetMapping("/my")
48+
public ResponseEntity<List<GroupSummaryResponse>> getMyGroups(@AuthenticationPrincipal UserPrincipal userPrincipal) {
49+
Long useId = userPrincipal.getId();
50+
List<GroupSummaryResponse> groups = groupService.getGroupsForUser(useId);
51+
return ResponseEntity.ok(groups);
52+
}
53+
4854
}

polling-app-server/src/main/java/com/example/polls/payload/Response/GroupSummaryResponse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.example.polls.model.Group;
44

5+
6+
57
public class GroupSummaryResponse {
68
private Long id;
79
private String name;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.example.polls.repository;
22

3+
import com.example.polls.model.Group;
34
import com.example.polls.model.GroupMember;
45
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.query.Param;
8+
9+
import java.util.List;
510

611
public interface GroupMemberRepository extends JpaRepository<GroupMember, Long> {
12+
@Query("SELECT gm.group FROM GroupMember gm WHERE gm.user.id = :userId")
13+
List<Group> findGroupsByUserId(@Param("userId") Long userId);
714
}

polling-app-server/src/main/java/com/example/polls/repository/GroupRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.example.polls.model.Group;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
57
import org.springframework.stereotype.Repository;
68

79
import java.util.List;
@@ -12,4 +14,7 @@ public interface GroupRepository extends JpaRepository<Group, Long> {
1214
Optional<Group> findByJoinCode(String joinCode); //코드로 그룹조회
1315
List<Group> findAllByMembers_user_Id(Long userId);
1416
boolean existsByJoinCode(String joinCode); //중복방지
17+
18+
@Query("SELECT g FROM Group g Join g.members gm WHERE gm.user.id = :userId")
19+
List<Group> findAllByUserId(@Param("userId") Long userId);
1520
}

polling-app-server/src/main/java/com/example/polls/service/GroupService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import org.springframework.stereotype.Service;
1414

1515
import javax.transaction.Transactional;
16+
import java.util.Collection;
17+
import java.util.List;
1618
import java.util.UUID;
19+
import java.util.stream.Collectors;
1720

1821
@Service
1922
@RequiredArgsConstructor
@@ -71,4 +74,13 @@ private String generateUniqueJoinCode() {
7174
}while(groupRepository.existsByJoinCode(code));
7275
return code;
7376
}
77+
78+
79+
//내 그룹 조회
80+
public List<GroupSummaryResponse> getGroupsForUser(Long userId) {
81+
List<Group> groups = groupMemberRepository.findGroupsByUserId(userId);
82+
return groups.stream()
83+
.map(GroupSummaryResponse::new)
84+
.collect(Collectors.toList());
85+
}
7486
}

0 commit comments

Comments
 (0)