Skip to content

Commit

Permalink
feat: 탐색 페이지 트랜딩 리스트 조회 API 구현 (#74)
Browse files Browse the repository at this point in the history
* style: 패키지 구조 변경 (#74)

* feat: 탐색 페이지 트랜딩 리스트 조회 API 구현 (#74)

* refactor: 트랜딩에 ownerId 추가 및 정렬 기준 변경 (#74)

* style: 패키지 구조 변경 (#74)

* refactor: 비지니스로직 부분 service 단에서 처리하도록 변경 (#74)
  • Loading branch information
pparkjs authored Feb 7, 2024
1 parent 257b6e4 commit a9632a6
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.listywave.list.application.domain.Item;
import com.listywave.list.application.domain.Lists;
import com.listywave.list.repository.ItemRepository;
import com.listywave.list.repository.ListRepository;
import com.listywave.list.repository.list.ListRepository;
import com.listywave.user.application.domain.User;
import java.util.Arrays;
import java.util.Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -162,6 +163,10 @@ public static Lists createList(
return lists;
}

public void sortItems() {
this.getItems().sort(Comparator.comparing(Item::getRanking));
}

public String getCategoryName() {
return category.name();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.listywave.list.presentation.dto.response;
package com.listywave.list.application.dto.response;

import com.listywave.list.application.domain.CategoryType;

Expand All @@ -10,9 +10,9 @@ public record CategoryTypeResponse(

public static CategoryTypeResponse fromEnum(CategoryType categoryType) {
return new CategoryTypeResponse(
categoryType.getCodeValue(),
categoryType.name().toLowerCase(),
categoryType.getKorNameValue()
categoryType.getCodeValue(),
categoryType.name().toLowerCase(),
categoryType.getKorNameValue()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.listywave.list.presentation.dto.response;
package com.listywave.list.application.dto.response;

public record ListCreateResponse(Long listId){
public record ListCreateResponse(Long listId) {

public static ListCreateResponse of(Long id) {
return new ListCreateResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.listywave.list.application.dto.response;

import com.listywave.list.application.domain.Lists;
import lombok.Builder;

@Builder
public record ListTrandingResponse(
Long id,
Long ownerId,
String title,
String description,
String itemImageUrl,
String backgroundColor
) {
public static ListTrandingResponse of(Lists list, String imageUrlTopRankItem) {
return ListTrandingResponse.builder()
.id(list.getId())
.ownerId(list.getUser().getId())
.title(list.getTitle())
.description(list.getDescription())
.itemImageUrl(imageUrlTopRankItem)
.backgroundColor(list.getBackgroundColor())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.listywave.list.application.dto.response.CommentCreateResponse;
import com.listywave.list.application.dto.response.CommentFindResponse;
import com.listywave.list.repository.CommentRepository;
import com.listywave.list.repository.ListRepository;
import com.listywave.list.repository.ReplyRepository;
import com.listywave.list.repository.list.ListRepository;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.user.UserRepository;
import jakarta.transaction.Transactional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
import com.listywave.common.exception.CustomException;
import com.listywave.common.exception.ErrorCode;
import com.listywave.common.util.UserUtil;
import com.listywave.list.application.domain.Item;
import com.listywave.list.application.domain.Lists;
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.ListTrandingResponse;
import com.listywave.list.presentation.dto.request.ItemCreateRequest;
import com.listywave.list.presentation.dto.response.ListCreateResponse;
import com.listywave.list.repository.ListRepository;
import com.listywave.list.repository.list.ListRepository;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.user.UserRepository;
import jakarta.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
Expand Down Expand Up @@ -63,6 +65,7 @@ public ListCreateResponse listCreate(
return ListCreateResponse.of(list.getId());
}


private List<User> findExistingCollaborators(List<Long> collaboratorIds) {
List<User> existingCollaborators = userRepository.findAllById(collaboratorIds);

Expand Down Expand Up @@ -111,4 +114,22 @@ public ListDetailResponse getListDetail(Long listId, String accessToken) {
}
return ListDetailResponse.of(list, list.getUser(), true, collaborators);
}

@Transactional(readOnly = true)
public List<ListTrandingResponse> getTrandingList() {
List<Lists> lists = listRepository.findTrandingLists();
lists.forEach(Lists::sortItems); // <-- 추가 !
return lists.stream()
.map(list -> ListTrandingResponse.of(list, getImageUrlTopRankItem(list.getItems())))
.toList();
}

private String getImageUrlTopRankItem(List<Item> items) {
return items.stream()
.map(Item::getImageUrl)
.filter(url -> !url.isEmpty())
.findFirst()
.orElse("");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.listywave.list.application.dto.response.ReplyCreateResponse;
import com.listywave.list.application.vo.Content;
import com.listywave.list.repository.CommentRepository;
import com.listywave.list.repository.ListRepository;
import com.listywave.list.repository.ReplyRepository;
import com.listywave.list.repository.list.ListRepository;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.user.UserRepository;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.listywave.list.presentation.controller;

import com.listywave.list.application.domain.CategoryType;
import com.listywave.list.presentation.dto.response.CategoryTypeResponse;
import com.listywave.list.application.dto.response.CategoryTypeResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.listywave.list.presentation.controller;

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.ListTrandingResponse;
import com.listywave.list.application.service.ListService;
import com.listywave.list.presentation.dto.request.ListCreateRequest;
import com.listywave.list.presentation.dto.response.ListCreateResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -43,4 +45,10 @@ ResponseEntity<ListDetailResponse> getListDetail(
ListDetailResponse listDetailResponse = listService.getListDetail(listId, accessToken);
return ResponseEntity.ok(listDetailResponse);
}

@GetMapping("/explore")
ResponseEntity<List<ListTrandingResponse>> getTrandingList() {
List<ListTrandingResponse> trandingList = listService.getTrandingList();
return ResponseEntity.ok().body(trandingList);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.listywave.list.repository;
package com.listywave.list.repository.list;

import static com.listywave.common.exception.ErrorCode.NOT_FOUND;

import com.listywave.common.exception.CustomException;
import com.listywave.list.application.domain.Lists;
import com.listywave.list.repository.list.custom.CustomListRepository;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ListRepository extends JpaRepository<Lists, Long> {
public interface ListRepository extends JpaRepository<Lists, Long>, CustomListRepository {

default Lists getById(Long listId) {
return findById(listId).orElseThrow(() -> new CustomException(NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.listywave.list.repository.list.custom;

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

public interface CustomListRepository {

List<Lists> findTrandingLists();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.listywave.list.repository.list.custom.impl;

import static com.listywave.list.application.domain.QItem.item;
import static com.listywave.list.application.domain.QLists.lists;

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

@RequiredArgsConstructor
public class CustomListRepositoryImpl implements CustomListRepository {

private final JPAQueryFactory queryFactory;

@Override
public List<Lists> findTrandingLists() {
List<Lists> fetch = 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;
}
}

0 comments on commit a9632a6

Please sign in to comment.