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: add item city and state filtering add #509

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -15,6 +15,7 @@
import java.util.Date;
import java.util.List;

import static com.depromeet.area.village.QVillageArea.villageArea;
import static com.depromeet.item.QItem.item;
import static com.depromeet.item.QItemLike.itemLike;
import static com.depromeet.item.QItemLocation.itemLocation;
Expand Down Expand Up @@ -94,4 +95,92 @@ public List<UserItemPointDao> findUserLikedItemsPoints(Long userId) {
.fetch();
}

@Override
public List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor, ItemOrderType itemOrderType, String state) {
DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = itemLike.createdAt;
QItemLike innerItemLike = new QItemLike("innerItemLike");

var query = queryFactory.select(
Projections.constructor(
UserItemLikeDao.class,
createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
item.id,
item.content,
itemLike.createdAt,
itemLocation.name,
song.name.as("songName"),
album.name.as("albumName"),
artist.name.as("artistName"),
albumCover.albumThumbnail.as("albumThumbnail"),
select(innerItemLike.id.count()).from(innerItemLike).where(innerItemLike.item.id.eq(item.id)),
item.user.id,
item.user.nickname
)
).from(itemLike)
.join(item).on(item.id.eq(itemLike.item.id))
.join(itemLocation).on(item.id.eq(itemLocation.item.id))
.join(villageArea).on(itemLocation.villageArea.id.eq(villageArea.id))
.join(song).on(item.song.id.eq(song.id))
.join(album).on(song.album.id.eq(album.id))
.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))
.where(villageArea.cityArea.stateArea.stateName.eq(state));


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

return query.fetch();
}



@Override
public List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, ItemOrderType itemOrderType, String city) {
DateExpression<Date> currentWeekExpr = currentDate();
DateTimePath<LocalDateTime> createdAtExpr = itemLike.createdAt;
QItemLike innerItemLike = new QItemLike("innerItemLike");

var query = queryFactory.select(
Projections.constructor(
UserItemLikeDao.class,
createdAtExpr.week().subtract(currentWeekExpr.week()).abs().as("weekAgo"),
item.id,
item.content,
itemLike.createdAt,
itemLocation.name,
song.name.as("songName"),
album.name.as("albumName"),
artist.name.as("artistName"),
albumCover.albumThumbnail.as("albumThumbnail"),
select(innerItemLike.id.count()).from(innerItemLike).where(innerItemLike.item.id.eq(item.id)),
item.user.id,
item.user.nickname
)
).from(itemLike)
.join(item).on(item.id.eq(itemLike.item.id))
.join(itemLocation).on(item.id.eq(itemLocation.item.id))
.join(villageArea).on(itemLocation.villageArea.id.eq(villageArea.id))
.join(song).on(item.song.id.eq(song.id))
.join(album).on(song.album.id.eq(album.id))
.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))
.where(villageArea.cityArea.cityName.eq(city));


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

return query.fetch();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
public interface QueryDslItemLikeRepository {
List<UserItemLikeDao> findByUserId(Long userId, Long lastCursor, ItemOrderType itemOrderType);
List<UserItemPointDao> findUserLikedItemsPoints(Long userId);
List<UserItemLikeDao> findByUserIdAndState(Long userId, Long lastCursor, ItemOrderType itemOrderType, String state);
List<UserItemLikeDao> findByUserIdAndCity(Long userId, Long lastCursor, ItemOrderType itemOrderType, String city);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ public void unlikeItem(User user, Long itemId) {
}

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

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

List<ItemGroupByDateResponseDto> itemGroupByDateResponseDto = itemLikeDaoList
.stream()
Expand All @@ -79,6 +79,19 @@ public PaginationResponseDto<?,?> getLikedItemsByUser(User user, long lastCursor
return new PaginationResponseDto<>(itemGroupByDateResponseDto, meta);
}


private List<UserItemLikeDao> getLikedItemListByUser(User user, long lastCursor, ItemOrderType itemOrderType, String state, String city){
if (state == null) {
return itemLikeRepository.findByUserId(user.getId(), lastCursor, itemOrderType);
}
else if (city == null) {
return itemLikeRepository.findByUserIdAndState(user.getId(), lastCursor, itemOrderType, state);
}
else {
return itemLikeRepository.findByUserIdAndCity(user.getId(), lastCursor, itemOrderType, city);
}
}

private ItemGroupResponseDto userItemLikeDaotoItemGroupResponseDto(UserItemLikeDao userItemLikeDao) {
return ItemGroupResponseDto
.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ public ResponseEntity<UserPoiResponseDto> getUserDropItemsPoints(
public ResponseEntity<PaginationResponseDto<?, ?>> getUserLikedItems(
@ReqUser User user,
@RequestParam(defaultValue = "9223372036854775000") long lastCursor,
@RequestParam(value = "state", required = false) String state,
@RequestParam(value = "city", required = false) String city,
@RequestParam(defaultValue = "RECENT", required = false) ItemOrderType orderType
) {
var response = userItemService.getLikedItems(user, lastCursor, orderType);
var response = userItemService.getLikedItems(user, lastCursor, orderType, state, city);
return ResponseDto.ok(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ private ItemGroupResponseDto itemDaotoItemGroupResponseDto(User user, ItemDao it


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

@Transactional(readOnly = true)
Expand Down
Loading