Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 최신 리스트 10개 조회 API 구현 #82

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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();
}

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

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

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

public static List<LabelsResponse> toList(List<Label> labels) {
return labels.stream()
.map(LabelsResponse::of)
.toList();
}
pparkjs marked this conversation as resolved.
Show resolved Hide resolved
}

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

public static ItemsResponse of(Item item) {
return ItemsResponse.builder()
.id(item.getId())
.ranking(item.getRanking())
.title(item.getTitle())
.imageUrl(item.getImageUrl())
.build();
}

public static List<ItemsResponse> toList(List<Item> items) {
return items.stream()
.sorted(Comparator.comparing(Item::getRanking))
.map(ItemsResponse::of)
.limit(3)
.toList();
}
}
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,26 @@ private String getImageUrlTopRankItem(List<Item> items) {
.orElse("");
}

@Transactional(readOnly = true)
public ListRecentResponse getRecentLists(String accessToken) {

pparkjs marked this conversation as resolved.
Show resolved Hide resolved
if (isSignedIn(accessToken)) {
List<Lists> recentLists = listRepository.getRecentLists();
return ListRecentResponse.of(recentLists);
} else {
pparkjs marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
pparkjs marked this conversation as resolved.
Show resolved Hide resolved

private boolean isSignedIn(String accessToken) {
pparkjs marked this conversation as resolved.
Show resolved Hide resolved
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,17 @@
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.common.BaseEntity;
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 @@ -25,11 +28,39 @@ public List<Lists> findTrandingLists() {
.limit(10)
.orderBy(lists.collectCount.desc(), lists.viewCount.desc())
.fetch();
return fetch;
}

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

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