Skip to content

Commit 52731ec

Browse files
Merge pull request #70 from SeoyeonPark1223/feature-67
#67 Feature: 검색, 컬렉션 기능에서 로그인 안 된 유저 처리 로직 추가
2 parents 0b801fe + 3368b1f commit 52731ec

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/main/java/com/memesphere/domain/collection/controller/CollectionRestController.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.memesphere.domain.collection.entity.Collection;
66
import com.memesphere.domain.collection.dto.response.CollectionPageResponse;
77
import com.memesphere.domain.collection.service.CollectionQueryService;
8+
import com.memesphere.global.apipayload.code.status.ErrorStatus;
9+
import com.memesphere.global.apipayload.exception.GeneralException;
810
import com.memesphere.global.jwt.CustomUserDetails;
911
import com.memesphere.global.jwt.TokenProvider;
1012
import com.memesphere.global.validation.annotation.CheckPage;
@@ -22,7 +24,6 @@
2224
@Tag(name="콜렉션", description = "콜렉션 관련 API")
2325
@RestController
2426
@RequiredArgsConstructor
25-
//@RequestMapping("/collection")
2627
public class CollectionRestController {
2728
private final CollectionQueryService collectionQueryService;
2829
private final CollectionCommandService collectionCommandService;
@@ -31,12 +32,14 @@ public class CollectionRestController {
3132
@GetMapping("/collection")
3233
@Operation(summary = "사용자의 밈코인 콜렉션 모음 조회 API")
3334
public ApiResponse<CollectionPageResponse> getCollectionList (
34-
// @AuthenticationPrincipal User user, // 현재 로그인한 사용자 (아직 구현 x)
35+
@AuthenticationPrincipal CustomUserDetails userDetails, // 현재 로그인한 사용자
3536
@CheckPage @RequestParam(name = "page") Integer page // 페이지 번호
3637
) {
3738
Integer pageNumber = page - 1;
38-
// Long userId = user.getId();
39-
Long userId = 1L;
39+
Long userId = (userDetails == null) ? null : userDetails.getUser().getId();
40+
41+
// 유저를 찾지 못하면(로그인을 안 했으면) 콜렉션 접근 못하도록 에러 처리
42+
if (userId == null) throw new GeneralException(ErrorStatus.USER_NOT_FOUND);
4043

4144
Page<Collection> collectionPage = collectionQueryService.getCollectionPage(userId, pageNumber);
4245
return ApiResponse.onSuccess(CollectionConverter.toCollectionPageDTO(collectionPage));

src/main/java/com/memesphere/domain/collection/service/CollectionQueryServiceImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import org.springframework.stereotype.Service;
1010
import org.springframework.transaction.annotation.Transactional;
1111

12+
import java.util.Collections;
1213
import java.util.List;
1314
import java.util.stream.Collectors;
1415

1516
@Service
1617
@RequiredArgsConstructor
1718
public class CollectionQueryServiceImpl implements CollectionQueryService {
18-
private final UserRepository userRepository;
1919
private final CollectionRepository collectionRepository;
2020

2121
@Transactional(readOnly = true)
@@ -28,6 +28,8 @@ public Page<Collection> getCollectionPage(Long userId, Integer pageNumber) {
2828
@Transactional(readOnly = true)
2929
@Override
3030
public List<Long> getUserCollectionIds(Long userId) {
31+
if (userId == null) return Collections.emptyList();
32+
3133
return collectionRepository.findAllByUserId(userId).stream()
3234
.map(Collection::getMemeCoinId)
3335
.collect(Collectors.toList());

src/main/java/com/memesphere/domain/search/controller/SearchRestController.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import com.memesphere.domain.collection.service.CollectionQueryService;
99
import com.memesphere.domain.search.service.SearchQueryService;
1010
import com.memesphere.domain.search.converter.SearchConverter;
11+
import com.memesphere.global.jwt.CustomUserDetails;
1112
import com.memesphere.global.validation.annotation.CheckPage;
1213
import io.swagger.v3.oas.annotations.Operation;
1314
import lombok.RequiredArgsConstructor;
1415
import org.springframework.data.domain.Page;
16+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
17+
import org.springframework.security.core.userdetails.UserDetails;
1518
import org.springframework.web.bind.annotation.GetMapping;
1619
import org.springframework.web.bind.annotation.RequestParam;
1720
import org.springframework.web.bind.annotation.RestController;
@@ -20,7 +23,6 @@
2023

2124
@RestController
2225
@RequiredArgsConstructor
23-
//@RequestMapping("/")
2426
public class SearchRestController {
2527
private final SearchQueryService searchQueryService;
2628
private final CollectionQueryService collectionQueryService;
@@ -31,12 +33,11 @@ public ApiResponse<SearchPageResponse> getSearchPage(
3133
@RequestParam(name = "searchWord") String searchWord, // 검색어
3234
@RequestParam(name = "viewType", defaultValue = "GRID") ViewType viewType, // 뷰 타입 (grid 또는 list)
3335
@RequestParam(name = "sortType", defaultValue = "PRICE_CHANGE") SortType sortType, // 정렬 기준 (MKTCap, 24h Volume, Price)
34-
@CheckPage @RequestParam(name = "page") Integer page // 페이지 번호
35-
// @AuthenticationPrincipal UserDetails user // 현재 로그인한 유저
36+
@CheckPage @RequestParam(name = "page") Integer page, // 페이지 번호
37+
@AuthenticationPrincipal CustomUserDetails userDetails // 현재 로그인한 유저
3638
) {
3739
Integer pageNumber = page - 1;
38-
// Long userId = user.getId();
39-
Long userId = 1L;
40+
Long userId = (userDetails == null) ? null : userDetails.getUser().getId();
4041

4142
Page<MemeCoin> searchPage = searchQueryService.getSearchPage(searchWord, viewType, sortType, pageNumber);
4243
List<Long> userCollectionIds = collectionQueryService.getUserCollectionIds(userId);

0 commit comments

Comments
 (0)