Skip to content

Commit

Permalink
feat: 최신 리스트 10개 조회 API 구현 (#82)
Browse files Browse the repository at this point in the history
* feat: 최신 리스트 10개 조회 API 구현 (#73)

* refactor: 코드 컨벤션 및 리펙토링 적용 (#73)
  • Loading branch information
pparkjs authored Feb 8, 2024
1 parent 8b8e3c4 commit 41fbe2d
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.listywave.list.application.dto.response;

import com.listywave.list.application.domain.Item;
import com.listywave.list.application.domain.Label;
import com.listywave.list.application.domain.Lists;
import java.util.Comparator;
import java.util.List;
import lombok.Builder;

@Builder
public record ListRecentResponse(
List<ListResponse> lists
) {
public static ListRecentResponse of(List<Lists> lists) {
return ListRecentResponse.builder()
.lists(ListResponse.toList(lists))
.build();
}
}

@Builder
record ListResponse(
Long id,
String category,
String backgroundColor,
Long ownerId,
String ownerNickname,
String ownerProfileImage,
List<LabelsResponse> labels,
String title,
String description,
List<ItemsResponse> items
) {

public static List<ListResponse> toList(List<Lists> lists) {
return lists.stream()
.map(ListResponse::of)
.toList();
}

public static ListResponse of(Lists lists) {
return ListResponse.builder()
.id(lists.getId())
.category(lists.getCategory().getKorNameValue())
.backgroundColor(lists.getBackgroundColor())
.ownerId(lists.getUser().getId())
.ownerNickname(lists.getUser().getNickname())
.ownerProfileImage(lists.getUser().getProfileImageUrl())
.labels(LabelsResponse.toList(lists.getLabels()))
.title(lists.getTitle())
.description(lists.getDescription())
.items(ItemsResponse.toList(lists.getItems()))
.build();
}
}

@Builder
record LabelsResponse(
Long id,
String name
) {

public static List<LabelsResponse> toList(List<Label> labels) {
return labels.stream()
.map(LabelsResponse::of)
.toList();
}

public static LabelsResponse of(Label label) {
return LabelsResponse.builder()
.id(label.getId())
.name(label.getLabelName())
.build();
}
}

@Builder
record ItemsResponse(
Long id,
int ranking,
String title,
String imageUrl
) {

public static List<ItemsResponse> toList(List<Item> items) {
return items.stream()
.sorted(Comparator.comparing(Item::getRanking))
.map(ItemsResponse::of)
.limit(3)
.toList();
}

public static ItemsResponse of(Item item) {
return ItemsResponse.builder()
.id(item.getId())
.ranking(item.getRanking())
.title(item.getTitle())
.imageUrl(item.getImageUrl())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.listywave.list.application.service;

import com.listywave.auth.application.domain.JwtManager;
import com.listywave.collaborator.domain.Collaborator;
import com.listywave.collaborator.domain.repository.CollaboratorRepository;
import com.listywave.common.exception.CustomException;
Expand All @@ -10,10 +11,13 @@
import com.listywave.list.application.dto.ListCreateCommand;
import com.listywave.list.application.dto.response.ListCreateResponse;
import com.listywave.list.application.dto.response.ListDetailResponse;
import com.listywave.list.application.dto.response.ListRecentResponse;
import com.listywave.list.application.dto.response.ListTrandingResponse;
import com.listywave.list.presentation.dto.request.ItemCreateRequest;
import com.listywave.list.repository.list.ListRepository;
import com.listywave.user.application.domain.Follow;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.follow.FollowRepository;
import com.listywave.user.repository.user.UserRepository;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -27,9 +31,11 @@
public class ListService {

private final UserUtil userUtil;
private final JwtManager jwtManager;
private final ListRepository listRepository;
private final UserRepository userRepository;
private final CollaboratorRepository collaboratorRepository;
private final FollowRepository followRepository;

public ListCreateResponse listCreate(
ListCreateCommand listCreateCommand,
Expand Down Expand Up @@ -132,4 +138,25 @@ private String getImageUrlTopRankItem(List<Item> items) {
.orElse("");
}

@Transactional(readOnly = true)
public ListRecentResponse getRecentLists(String accessToken) {
if (isSignedIn(accessToken)) {
Long loginUserId = jwtManager.read(accessToken);
User user = userRepository.getById(loginUserId);
List<Follow> follows = followRepository.getAllByFollowerUser(user);

List<User> followingUsers = follows.stream()
.map(Follow::getFollowingUser)
.toList();
List<Lists> recentListsByFollowing = listRepository.getRecentListsByFollowing(followingUsers);
return ListRecentResponse.of(recentListsByFollowing);
}
List<Lists> recentLists = listRepository.getRecentLists();
return ListRecentResponse.of(recentLists);

}

private boolean isSignedIn(String accessToken) {
return !accessToken.isBlank();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.listywave.list.application.dto.ListCreateCommand;
import com.listywave.list.application.dto.response.ListCreateResponse;
import com.listywave.list.application.dto.response.ListDetailResponse;
import com.listywave.list.application.dto.response.ListRecentResponse;
import com.listywave.list.application.dto.response.ListTrandingResponse;
import com.listywave.list.application.service.ListService;
import com.listywave.list.presentation.dto.request.ListCreateRequest;
Expand Down Expand Up @@ -51,4 +52,12 @@ ResponseEntity<List<ListTrandingResponse>> getTrandingList() {
List<ListTrandingResponse> trandingList = listService.getTrandingList();
return ResponseEntity.ok().body(trandingList);
}

@GetMapping()
ResponseEntity<ListRecentResponse> getRecentLists(
@RequestHeader(value = "Authorization", defaultValue = "") String accessToken
) {
ListRecentResponse recentLists = listService.getRecentLists(accessToken);
return ResponseEntity.ok(recentLists);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.listywave.list.repository.list.custom;

import com.listywave.list.application.domain.Lists;
import com.listywave.user.application.domain.User;
import java.util.List;

public interface CustomListRepository {

List<Lists> findTrandingLists();

List<Lists> getRecentLists();

List<Lists> getRecentListsByFollowing(List<User> followingUsers);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.listywave.list.repository.list.custom.impl;

import static com.listywave.list.application.domain.QItem.item;
import static com.listywave.list.application.domain.QLabel.label;
import static com.listywave.list.application.domain.QLists.lists;
import static com.listywave.user.application.domain.QUser.user;

import com.listywave.list.application.domain.Item;
import com.listywave.list.application.domain.Lists;
import com.listywave.list.repository.list.custom.CustomListRepository;
import com.listywave.user.application.domain.User;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
Expand All @@ -18,18 +20,43 @@ public class CustomListRepositoryImpl implements CustomListRepository {

@Override
public List<Lists> findTrandingLists() {
List<Lists> fetch = queryFactory
return queryFactory
.selectFrom(lists)
.leftJoin(lists.items, item)
.distinct()
.limit(10)
.orderBy(lists.collectCount.desc(), lists.viewCount.desc())
.fetch();
}

fetch.forEach(
list -> list.getItems()
.sort(Comparator.comparing(Item::getRanking))
);
return fetch;
@Override
public List<Lists> getRecentLists() {
return queryFactory
.selectFrom(lists)
.join(lists.user, user).fetchJoin()
.leftJoin(lists.labels, label)
.leftJoin(lists.items, item)
.distinct()
.limit(10)
.orderBy(lists.updatedDate.desc())
.fetch();
}

@Override
public List<Lists> getRecentListsByFollowing(List<User> followingUsers) {
return queryFactory
.selectFrom(lists)
.join(lists.user, user).fetchJoin()
.leftJoin(lists.labels, label)
.leftJoin(lists.items, item)
.where(lists.user.id.in(
followingUsers.stream()
.map(User::getId)
.collect(Collectors.toList())
))
.distinct()
.limit(10)
.orderBy(lists.updatedDate.desc())
.fetch();
}
}

0 comments on commit 41fbe2d

Please sign in to comment.