berryberrybin / board-project

0 stars 0 forks source link

[spring] 상단 고정 구현 #26

Open berryberrybin opened 1 year ago

berryberrybin commented 1 year ago

    @GetMapping("/shop/{shopId}/feed")
    public ResponseEntity<CommonResponse<Slice<FeedDetailInfo>>> retrieveFeedListByShopId(
        @PathVariable("userId") UUID userId,
        @PathVariable("shopId") UUID shopId,
        @SortDefault.SortDefaults({
            @SortDefault(sort = "fix", direction = Sort.Direction.DESC),
            @SortDefault(sort = "createdAt", direction = Sort.Direction.DESC)
        }) Pageable pageable
    ){

        FeedQuery.RetrieveFeedListByShopId query = FeedQuery.RetrieveFeedListByShopId.builder()
            .loginUserId(userId)
            .shopId(shopId)
            .pageable(pageable)
            .build();

        Slice<FeedDetailInfo> feedListInfo = feedQueryService.retrieveFeedListByShopId(query);

        return ResponseEntity.status(HttpStatus.OK).body(CommonResponse.success("해당 shopId의 피드 목록 조회 성공", feedListInfo));
    }

public void saveFeedFix(FeedCommand.SaveFeedFixCommand command){

        UUID loginUserId = command.getUserId();
        User loginUser = userRepository.findById(loginUserId)
            .orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND, "user.byId", List.of(loginUserId.toString())));

        UUID feedId = command.getFeedId();
        Feed feed = feedRepository.findById(feedId)
            .orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND, "feed.byId", List.of(feedId.toString())));

        if(feed.getAuthor().getId().equals(loginUserId)){ // 작성자만 고정시킬 수 있음 
            if(feed.getFix()== 0){
                feed.updateFeedFix(1); // 고정
            }else{
                feed.updateFeedFix(0); // 고정취소
            }
        }
        feedRepository.save(feed);
    }