Skip to content

Commit

Permalink
fix : 카테고리별 읽음 현황 조회 쿼리에 case문 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
EunjiShin committed Nov 9, 2023
1 parent 33f8c5e commit 46a2248
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.querydsl.core.Tuple;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.core.types.dsl.NumberExpression;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.pickly.service.common.utils.page.PageRequest;
import org.pickly.service.common.utils.timezone.TimezoneHandler;
import org.pickly.service.domain.bookmark.entity.Bookmark;
Expand All @@ -24,6 +27,7 @@
import static org.pickly.service.domain.category.entity.QCategory.category;
import static org.pickly.service.domain.notification.entity.QNotification.notification;

@Slf4j
@Repository
@RequiredArgsConstructor
public class BookmarkQueryRepositoryImpl implements BookmarkQueryRepository {
Expand Down Expand Up @@ -100,6 +104,7 @@ public List<Bookmark> findBookmarkByCategoryId(PageRequest pageRequest, Long cat
return queryFactory
.selectFrom(bookmark)
.where(
ltCursorId(cursorId),
eqCategoryId(categoryId),
notDeleted()
)
Expand Down Expand Up @@ -138,7 +143,7 @@ public Map<Category, BookmarkReadStatus> findCategoryReadStatus(final Long membe
.select(
category,
bookmark.id.count(),
bookmark.readByUser.count()
sumReadTrue()
)
.from(category)
.leftJoin(bookmark).on(category.id.eq(bookmark.category.id))
Expand All @@ -154,14 +159,22 @@ public Map<Category, BookmarkReadStatus> findCategoryReadStatus(final Long membe
for (Tuple result : results) {
Category currentCategory = result.get(category);
Long totalBookmarks = result.get(bookmark.id.count());
Long readBookmarks = result.get(bookmark.readByUser.count());
Long readBookmarks = result.get(sumReadTrue());

BookmarkReadStatus status = new BookmarkReadStatus(totalBookmarks, readBookmarks);
categoryBookmarkCounts.put(currentCategory, status);
}
return categoryBookmarkCounts;
}

private NumberExpression<Long> sumReadTrue() {
return new CaseBuilder()
.when(bookmark.readByUser.isTrue())
.then(1L)
.otherwise(0L)
.sum();
}

private BooleanExpression eqMemberId(final Long memberId) {
if (memberId == null) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions pickly-service/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ spring:
mode: never
# postgreSQL local 설정
datasource:
url: jdbc:postgresql://localhost:5450/pickly
username: postgres
password: postgres
url: jdbc:postgresql://${DEV_DB_HOST}:${DEV_DB_PORT}/${DB_DBNAME}
username: ${DB_USERNAME}
password: ${DEV_DB_PASSWORD}

# 로깅 관련
logging:
Expand Down

0 comments on commit 46a2248

Please sign in to comment.