Skip to content

Commit

Permalink
feat(NoticeQueryService): 공지 조회시 댓글 수도 함께 반환하도록 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
zbqmgldjfh committed Feb 18, 2025
1 parent 2d96123 commit d1d6cc6
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public record NoticeRangeLookupResponse(
String url,
String subject,
String category,
Boolean important
Boolean important,
Long commentCount
){
public static NoticeRangeLookupResponse from(NoticeRangeLookupResult result) {
return new NoticeRangeLookupResponse(
Expand All @@ -19,7 +20,8 @@ public static NoticeRangeLookupResponse from(NoticeRangeLookupResult result) {
result.url(),
result.subject(),
result.category(),
result.important()
result.important(),
result.commentCount()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,7 @@ public List<NoticeDto> findNotYetEmbeddingNotice(CategoryName categoryName, Loca

@Override
public Optional<NoticeDto> findNoticeById(Long id) {
Optional<Notice> noticeOptional = noticeRepository.findById(id);

return noticeOptional.map(notice -> new NoticeDto(
notice.getId(),
notice.getArticleId(),
notice.getPostedDate(),
notice.getUrl(),
notice.getSubject(),
notice.getCategoryName(),
notice.isImportant()
));
return this.noticeRepository.findNoticeReadModelByArticleId(id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

interface NoticeQueryRepository {

Expand Down Expand Up @@ -41,4 +42,6 @@ interface NoticeQueryRepository {
void updateNoticeEmbeddingStatus(List<String> articleIds, CategoryName categoryName);

List<NoticeDto> findNotYetEmbeddingNoticeByDate(CategoryName categoryName, LocalDateTime date);

Optional<NoticeDto> findNoticeReadModelByArticleId(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@
import com.kustacks.kuring.notice.application.port.out.dto.QNoticeSearchDto;
import com.kustacks.kuring.notice.domain.CategoryName;
import com.kustacks.kuring.notice.domain.DepartmentName;
import com.kustacks.kuring.notice.domain.QComment;
import com.kustacks.kuring.user.application.port.out.dto.BookmarkDto;
import com.kustacks.kuring.user.application.port.out.dto.QBookmarkDto;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.ConstantImpl;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberTemplate;
import com.querydsl.core.types.dsl.StringTemplate;
import com.querydsl.jpa.JPQLQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static com.kustacks.kuring.notice.domain.QComment.comment;
import static com.kustacks.kuring.notice.domain.QDepartmentNotice.departmentNotice;
import static com.kustacks.kuring.notice.domain.QNotice.notice;
import static com.querydsl.jpa.JPAExpressions.select;

@RequiredArgsConstructor
class NoticeQueryRepositoryImpl implements NoticeQueryRepository {
Expand All @@ -41,6 +46,12 @@ public List<NoticeDto> findNoticesByCategoryWithOffset(CategoryName categoryName
ConstantImpl.create(DATE_TIME_TEMPLATE)
);

QComment comment = QComment.comment;

JPQLQuery<Long> commentCount = select(comment.count())
.from(comment)
.where(comment.noticeId.eq(notice.id));

return queryFactory
.select(
new QNoticeDto(
Expand All @@ -50,7 +61,9 @@ public List<NoticeDto> findNoticesByCategoryWithOffset(CategoryName categoryName
notice.url.value,
notice.subject,
notice.categoryName.stringValue().toLowerCase(),
notice.important)
notice.important,
commentCount
)
).from(notice)
.where(notice.categoryName.eq(categoryName))
.offset(pageable.getOffset())
Expand Down Expand Up @@ -90,6 +103,10 @@ public List<NoticeDto> findNotYetEmbeddingNoticeByDate(CategoryName categoryName
ConstantImpl.create(DATE_TIME_TEMPLATE)
);

JPQLQuery<Long> commentCount = select(comment.count())
.from(comment)
.where(comment.noticeId.eq(notice.id));

return queryFactory.select(
new QNoticeDto(
notice.id,
Expand All @@ -98,7 +115,8 @@ public List<NoticeDto> findNotYetEmbeddingNoticeByDate(CategoryName categoryName
notice.url.value,
notice.subject,
notice.categoryName.stringValue().toLowerCase(),
notice.important
notice.important,
commentCount
)
).from(notice)
.where(notice.categoryName.eq(categoryName)
Expand All @@ -107,6 +125,37 @@ public List<NoticeDto> findNotYetEmbeddingNoticeByDate(CategoryName categoryName
.fetch();
}

@Transactional(readOnly = true)
@Override
public Optional<NoticeDto> findNoticeReadModelByArticleId(Long id) {
StringTemplate postedDate = Expressions.stringTemplate(
DATE_FORMAT_TEMPLATE,
notice.noticeDateTime.postedDate,
ConstantImpl.create(DATE_TIME_TEMPLATE)
);

JPQLQuery<Long> commentCount = select(comment.count())
.from(comment)
.where(comment.noticeId.eq(notice.id));

NoticeDto noticeDto = queryFactory.select(
new QNoticeDto(
notice.id,
notice.articleId,
postedDate,
notice.url.value,
notice.subject,
notice.categoryName.stringValue().toLowerCase(),
notice.important,
commentCount
)
).from(notice)
.where(notice.id.eq(id))
.fetchOne();

return Optional.ofNullable(noticeDto);
}

@Transactional(readOnly = true)
@Override
public List<String> findNormalArticleIdsByCategory(CategoryName categoryName) {
Expand Down Expand Up @@ -189,15 +238,22 @@ public List<NoticeDto> findImportantNoticesByDepartment(DepartmentName departmen
ConstantImpl.create(DATE_TIME_TEMPLATE)
);

JPQLQuery<Long> commentCount = select(comment.count())
.from(comment)
.where(comment.noticeId.eq(departmentNotice.id));

return queryFactory
.select(new QNoticeDto(
departmentNotice.id,
departmentNotice.articleId,
postedDate,
departmentNotice.url.value,
departmentNotice.subject,
departmentNotice.categoryName.stringValue().toLowerCase(),
departmentNotice.important))
departmentNotice.id,
departmentNotice.articleId,
postedDate,
departmentNotice.url.value,
departmentNotice.subject,
departmentNotice.categoryName.stringValue().toLowerCase(),
departmentNotice.important,
commentCount
)
)
.from(departmentNotice)
.where(departmentNotice.departmentName.eq(departmentName)
.and(departmentNotice.important.isTrue()))
Expand All @@ -214,15 +270,22 @@ public List<NoticeDto> findNormalNoticesByDepartmentWithOffset(DepartmentName de
ConstantImpl.create(DATE_TIME_TEMPLATE)
);

JPQLQuery<Long> commentCount = select(comment.count())
.from(comment)
.where(comment.noticeId.eq(departmentNotice.id));

return queryFactory
.select(new QNoticeDto(
departmentNotice.id,
departmentNotice.articleId,
postedDate,
departmentNotice.url.value,
departmentNotice.subject,
departmentNotice.categoryName.stringValue().toLowerCase(),
departmentNotice.important))
departmentNotice.id,
departmentNotice.articleId,
postedDate,
departmentNotice.url.value,
departmentNotice.subject,
departmentNotice.categoryName.stringValue().toLowerCase(),
departmentNotice.important,
commentCount
)
)
.from(departmentNotice)
.where(departmentNotice.departmentName.eq(departmentName)
.and(departmentNotice.important.isFalse()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public record NoticeRangeLookupResult(
String url,
String subject,
String category,
Boolean important
Boolean important,
Long commentCount
){
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ public class NoticeDto {

private Boolean important;

private Long commentCount;

@QueryProjection
public NoticeDto(Long id, String articleId, String postedDate, String url, String subject, String category, Boolean important) {
public NoticeDto(
Long id, String articleId, String postedDate, String url, String subject,
String category, Boolean important, Long commentCount
) {
Assert.notNull(id, "id must not be null");
Assert.notNull(articleId, "articleId must not be null");
Assert.notNull(postedDate, "postedDate must not be null");
Expand All @@ -44,5 +49,6 @@ public NoticeDto(Long id, String articleId, String postedDate, String url, Strin
this.subject = subject;
this.category = category;
this.important = important;
this.commentCount = commentCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,16 @@ private String convertShortNameIntoLongName(String typeShortName) {
.orElseThrow(() -> new NotFoundException(ErrorCode.API_NOTICE_NOT_EXIST_CATEGORY));
}

public static NoticeRangeLookupResult convertPortResult(NoticeDto dto) {
private static NoticeRangeLookupResult convertPortResult(NoticeDto dto) {
return new NoticeRangeLookupResult(
dto.getId(),
dto.getArticleId(),
dto.getPostedDate(),
dto.getUrl(),
dto.getSubject(),
dto.getCategory(),
dto.getImportant()
dto.getImportant(),
dto.getCommentCount()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void scrapForEmbedding() throws IOException {
);

NoticeDto noticeDto = new NoticeDto(1L, "1", "2024-01-01 00:00:00",
"http://example.com", "제목", "category", true);
"http://example.com", "제목", "category", true, 2L);

when(normalJsoupClient.get(anyString(), anyInt())).thenReturn(doc);

Expand Down

0 comments on commit d1d6cc6

Please sign in to comment.