Skip to content

Commit

Permalink
✨ feat(api): add user drop item order (#496)
Browse files Browse the repository at this point in the history
* ✨ Feat: add user drop item order

* ✨ Feat: add user like recent, oldest
  • Loading branch information
seonghun-dev authored Jul 23, 2024
1 parent d846e11 commit 2981630
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 24 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
@@ -1,6 +1,7 @@
package com.depromeet.domains.item.repository;

import com.depromeet.domains.item.dao.ItemDao;
import com.depromeet.domains.user.dto.request.ItemOrderType;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.DateExpression;
import com.querydsl.core.types.dsl.DateTimePath;
Expand Down Expand Up @@ -29,12 +30,12 @@ public class ItemRepositoryImpl implements QueryDslItemRepository {
private final JPAQueryFactory queryFactory;

@Override
public List<ItemDao> findByUserId(Long userId, long lastCursor) {
public List<ItemDao> findByUserId(Long userId, long lastCursor, ItemOrderType orderType) {

DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = item.createdAt;

return queryFactory.select(
var query = queryFactory.select(
Projections.constructor(
ItemDao.class,
createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
Expand All @@ -56,8 +57,14 @@ public List<ItemDao> findByUserId(Long userId, long lastCursor) {
.join(albumCover).on(item.albumCover.id.eq(albumCover.id))
.leftJoin(itemLike).on(item.id.eq(itemLike.item.id))
.where(item.user.id.eq(userId))
.groupBy(item.id, item.content, item.createdAt, itemLocation.name, song.name, album.name, artist.name, albumCover.albumThumbnail)
.orderBy(item.createdAt.desc())
.fetch();
.groupBy(item.id, item.content, item.createdAt, itemLocation.name, song.name, album.name, artist.name, albumCover.albumThumbnail);


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

return query.fetch();
}
}
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
@@ -1,9 +1,10 @@
package com.depromeet.domains.item.repository;

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

import java.util.List;

public interface QueryDslItemRepository {
List<ItemDao> findByUserId(Long userId, long lastCursor);
List<ItemDao> findByUserId(Long userId, long lastCursor, ItemOrderType orderType);
}
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 @@ -2,6 +2,7 @@

import com.depromeet.common.dto.PaginationResponseDto;
import com.depromeet.common.dto.ResponseDto;
import com.depromeet.domains.user.dto.request.ItemOrderType;
import com.depromeet.domains.user.dto.response.UserPoiResponseDto;
import com.depromeet.domains.user.service.UserItemService;
import com.depromeet.security.annotation.ReqUser;
Expand Down Expand Up @@ -29,9 +30,10 @@ public class UserItemController {
@GetMapping("/drop")
public ResponseEntity<PaginationResponseDto<?, ?>> getUserDropItems(
@ReqUser User user,
@RequestParam(defaultValue = "9223372036854775000") long lastCursor
@RequestParam(defaultValue = "9223372036854775000") long lastCursor,
@RequestParam(defaultValue = "RECENT", required = false) ItemOrderType orderType
) {
var response = userItemService.getDropItems(user, lastCursor);
var response = userItemService.getDropItems(user, lastCursor, orderType);
return ResponseDto.ok(response);
}

Expand All @@ -50,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
@@ -0,0 +1,5 @@
package com.depromeet.domains.user.dto.request;

public enum ItemOrderType {
RECENT, OLDEST
}
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 @@ -11,13 +11,14 @@
import com.depromeet.domains.item.repository.ItemRepository;
import com.depromeet.domains.item.service.ItemLikeService;
import com.depromeet.domains.music.dto.response.MusicResponseDto;
import com.depromeet.domains.user.dto.request.ItemOrderType;
import com.depromeet.domains.user.dto.response.UserPoiResponseDto;
import com.depromeet.domains.user.dto.response.UserResponseDto;
import com.depromeet.user.User;
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 All @@ -32,8 +33,8 @@ public class UserItemService {
private final ItemLikeService itemLikeService;

@Transactional(readOnly = true)
public PaginationResponseDto<?, ?> getDropItems(User user, long nextCursor) {
List<ItemDao> itemList = itemRepository.findByUserId(user.getId(), nextCursor);
public PaginationResponseDto<?, ?> getDropItems(User user, long nextCursor, ItemOrderType orderType) {
List<ItemDao> itemList = itemRepository.findByUserId(user.getId(), nextCursor, orderType);
List<ItemGroupByDateResponseDto> itemGroupByDateResponseDto = itemList
.stream()
.map(ItemDao::getWeekAgo)
Expand Down Expand Up @@ -86,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 2981630

Please sign in to comment.