Skip to content

Commit

Permalink
✨ Feat: add user like recent, oldest
Browse files Browse the repository at this point in the history
  • Loading branch information
seonghun-dev committed Jul 20, 2024
1 parent a0992f4 commit 2620ab5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.depromeet.domains.item.dao.UserItemLikeDao;
import com.depromeet.domains.user.dao.UserItemPointDao;
import com.depromeet.domains.user.dto.request.ItemOrderType;
import com.depromeet.item.QItemLike;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.DateExpression;
Expand Down Expand Up @@ -32,13 +33,13 @@ public class ItemLikeRepositoryImpl implements QueryDslItemLikeRepository {
private final JPAQueryFactory queryFactory;


public List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor) {
public List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType) {

DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = itemLike.createdAt;
QItemLike innerItemLike = new QItemLike("innerItemLike");

return queryFactory.select(
var query = queryFactory.select(
Projections.constructor(
UserItemLikeDao.class,
createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
Expand All @@ -62,9 +63,15 @@ public List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor) {
.join(artist).on(album.artist.id.eq(artist.id))
.join(albumCover).on(item.albumCover.id.eq(albumCover.id))
.join(user).on(user.eq(item.user))
.where(itemLike.user.id.eq(userId))
.orderBy(itemLike.createdAt.desc())
.fetch();
.where(itemLike.user.id.eq(userId));


query = switch (itemOrderType) {
case RECENT -> query.orderBy(itemLike.createdAt.desc());
case OLDEST -> query.orderBy(itemLike.createdAt.asc());
};

return query.fetch();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.depromeet.domains.item.dao.UserItemLikeDao;
import com.depromeet.domains.user.dao.UserItemPointDao;
import com.depromeet.domains.user.dto.request.ItemOrderType;

import java.util.List;

public interface QueryDslItemLikeRepository {
List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor);
List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType);
List<UserItemPointDao> findUserLikedItemsPoints(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.depromeet.domains.item.error.ItemErrorCode;
import com.depromeet.domains.item.repository.ItemLikeRepository;
import com.depromeet.domains.music.dto.response.MusicResponseDto;
import com.depromeet.domains.user.dto.request.ItemOrderType;
import com.depromeet.domains.user.dto.response.UserResponseDto;
import com.depromeet.item.Item;
import com.depromeet.item.ItemLike;
Expand Down Expand Up @@ -56,9 +57,9 @@ public void unlikeItem(User user, Long itemId) {
}

@Transactional(readOnly = true)
public PaginationResponseDto<?,?> getLikedItemsByUser(User user, long lastCursor) {
public PaginationResponseDto<?,?> getLikedItemsByUser(User user, long lastCursor, ItemOrderType itemOrderType) {

List<UserItemLikeDao> itemLikeDaoList = itemLikeRepository.findByUserId(user.getId(),lastCursor);
List<UserItemLikeDao> itemLikeDaoList = itemLikeRepository.findByUserId(user.getId(),lastCursor, itemOrderType);

List<ItemGroupByDateResponseDto> itemGroupByDateResponseDto = itemLikeDaoList
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public ResponseEntity<UserPoiResponseDto> getUserDropItemsPoints(
@GetMapping("/like")
public ResponseEntity<PaginationResponseDto<?, ?>> getUserLikedItems(
@ReqUser User user,
@RequestParam(defaultValue = "9223372036854775000") long lastCursor
@RequestParam(defaultValue = "9223372036854775000") long lastCursor,
@RequestParam(defaultValue = "RECENT", required = false) ItemOrderType orderType
) {
var response = userItemService.getLikedItems(user, lastCursor);
var response = userItemService.getLikedItems(user, lastCursor, orderType);
return ResponseDto.ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.depromeet.domains.user.service;

import com.depromeet.common.dto.InfiniteScrollMetaResponseDto;

import com.depromeet.common.dto.PaginationResponseDto;
import com.depromeet.domains.item.dao.ItemDao;
import com.depromeet.domains.item.dto.response.ItemGroupByDateResponseDto;
import com.depromeet.domains.item.dto.response.ItemGroupResponseDto;
Expand All @@ -18,7 +18,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.depromeet.common.dto.PaginationResponseDto;

import java.util.List;

import static com.depromeet.util.WeekUtil.getWeeksAgo;
Expand Down Expand Up @@ -87,8 +87,8 @@ private ItemGroupResponseDto itemDaotoItemGroupResponseDto(User user, ItemDao it


@Transactional(readOnly = true)
public PaginationResponseDto<?, ?> getLikedItems(User user, long nextCursor) {
return itemLikeService.getLikedItemsByUser(user, nextCursor);
public PaginationResponseDto<?, ?> getLikedItems(User user, long nextCursor, ItemOrderType itemOrderType) {
return itemLikeService.getLikedItemsByUser(user, nextCursor, itemOrderType);
}

@Transactional(readOnly = true)
Expand Down

0 comments on commit 2620ab5

Please sign in to comment.