Skip to content

Commit

Permalink
feat: #83 반려동물의 가장 최근 기록 리스트 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Feb 1, 2024
1 parent 3dcc9e6 commit 2e73481
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/kcy/fitapet/domain/memo/api/MemoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public ResponseEntity<?> getMemosByPet(
@PathVariable("pet_id") Long petId,
@PageableDefault(size = 5, page = 0, sort = "memo.createdAt", direction = Sort.Direction.DESC) Pageable pageable
) {
return null;
MemoInfoDto.PageResponse res = memoManageService.findMemosByPetId(petId, pageable);
return ResponseEntity.ok(SuccessResponse.from(res));
}

@Operation(summary = "메모 단건 조회")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
public interface MemoQueryDslRepository {
Optional<MemoInfoDto.MemoInfo> findMemoAndMemoImageUrlsById(Long memoId);
Slice<MemoInfoDto.MemoInfo> findMemosInMemoCategory(Long memoCategoryId, Pageable pageable, String target);
Slice<MemoInfoDto.MemoInfo> findMemosByPetId(Long petId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,22 @@
import com.kcy.fitapet.domain.memo.dto.MemoInfoDto;
import com.kcy.fitapet.global.common.util.querydsl.QueryDslUtil;
import com.kcy.fitapet.global.common.util.querydsl.RepositorySliceHelper;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Order;
import com.querydsl.core.ResultTransformer;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.querydsl.core.group.GroupBy.groupBy;
import static com.querydsl.core.types.Projections.list;
import static com.querydsl.core.types.dsl.Expressions.constant;
import static com.querydsl.core.types.dsl.Expressions.constantAs;

@Repository
@RequiredArgsConstructor
Expand Down Expand Up @@ -78,10 +69,11 @@ public Optional<MemoInfoDto.MemoInfo> findMemoAndMemoImageUrlsById(Long memoId)
* FROM (
* SELECT m.id
* FROM memo_category c
* INNER JOIN memo m ON m.category_id = c.id
* WHERE c.id = 4
* LEFT JOIN memo m ON m.category_id = c.id
* WHERE c.id = ?
* AND MATCH(m.title, m.content) AGAINST('병원*' IN BOOLEAN MODE)
* LIMIT 4
* ORDER BY m.created_at DESC
* LIMIT ?, ?
* ) id
* )
* ORDER BY m.created_at DESC
Expand All @@ -101,30 +93,55 @@ public Slice<MemoInfoDto.MemoInfo> findMemosInMemoCategory(Long memoCategoryId,
.where(memoCategory.id.eq(memoCategoryId)
.and(QueryDslUtil.matchAgainst(memo.title, memo.content, target))
)
.orderBy(QueryDslUtil.getOrderSpecifier(pageable.getSort()).toArray(OrderSpecifier[]::new))
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1)
))
.orderBy(QueryDslUtil.getOrderSpecifier(pageable.getSort()).toArray(OrderSpecifier[]::new))
.transform(
groupBy(memo.id).list(
Projections.constructor(
MemoInfoDto.MemoInfo.class,
memo.id,
queryFactory.select(memoCategory.categoryName).from(memoCategory).where(memoCategory.id.eq(memoCategoryId)),
QueryDslUtil.left(memo.title, Expressions.constant(19)),
QueryDslUtil.left(memo.content, Expressions.constant(16)),
memo.createdAt,
list(
Projections.constructor(
MemoInfoDto.MemoImageInfo.class,
memoImage.id,
memoImage.imgUrl
).skipNulls()
).skipNulls()
)
)
);
.orderBy(QueryDslUtil.getOrderSpecifier(pageable.getSort()).toArray(OrderSpecifier[]::new))
.transform(createMemoInfoDtoResultTransformer());

return RepositorySliceHelper.toSlice(results, pageable);
}

@Override
public Slice<MemoInfoDto.MemoInfo> findMemosByPetId(Long petId, Pageable pageable) {
List<MemoInfoDto.MemoInfo> results = queryFactory
.from(memoCategory)
.leftJoin(memo).on(memo.memoCategory.id.eq(memoCategory.id))
.leftJoin(memoImage).on(memoImage.memo.id.eq(memo.id))
.where(memo.id.in(
queryFactory
.select(memo.id)
.from(memoCategory)
.leftJoin(memo).on(memo.memoCategory.id.eq(memoCategory.id))
.where(memoCategory.pet.id.eq(petId))
.orderBy(QueryDslUtil.getOrderSpecifier(pageable.getSort()).toArray(OrderSpecifier[]::new))
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1)
))
.orderBy(QueryDslUtil.getOrderSpecifier(pageable.getSort()).toArray(OrderSpecifier[]::new))
.transform(createMemoInfoDtoResultTransformer());

return RepositorySliceHelper.toSlice(results, pageable);
}

private ResultTransformer<List<MemoInfoDto.MemoInfo>> createMemoInfoDtoResultTransformer() {
return groupBy(memo.id).list(
Projections.constructor(
MemoInfoDto.MemoInfo.class,
memo.id,
queryFactory.select(memoCategory.categoryName).from(memoCategory).where(memoCategory.id.eq(memo.memoCategory.id)),
QueryDslUtil.left(memo.title, Expressions.constant(19)),
QueryDslUtil.left(memo.content, Expressions.constant(16)),
memo.createdAt,
list(
Projections.constructor(
MemoInfoDto.MemoImageInfo.class,
memoImage.id,
memoImage.imgUrl
).skipNulls()
).skipNulls()
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public MemoInfoDto.MemoInfo findMemoAndMemoImageUrlsById(Long memoId) {
public MemoInfoDto.PageResponse findMemosInMemoCategory(Long memoCategoryId, Pageable pageable, String target) {
return memoSearchService.findMemosInMemoCategory(memoCategoryId, pageable, target);
}

@Transactional(readOnly = true)
public MemoInfoDto.PageResponse findMemosByPetId(Long petId, Pageable pageable) {
return memoSearchService.findMemosByPetId(petId, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ public MemoInfoDto.PageResponse findMemosInMemoCategory(Long memoCategoryId, Pag

return MemoInfoDto.PageResponse.from(page);
}

@Transactional(readOnly = true)
public MemoInfoDto.PageResponse findMemosByPetId(Long petId, Pageable pageable) {
Slice<MemoInfoDto.MemoInfo> page = memoRepository.findMemosByPetId(petId, pageable);

return MemoInfoDto.PageResponse.from(page);
}
}

0 comments on commit 2e73481

Please sign in to comment.