-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* v1.0.1: Github Actions 릴리즈 버저닝 자동화 스크립트 추가 및 잘못된 (#143) * chore: Github Actions 릴리즈 버저닝 자동화 스크립트 추가 (#140) * fix: Github Actions 잘못된 Indent 수정 (#142) * feat: 편지 모아보기 API 구현
- Loading branch information
1 parent
d2efa0e
commit 14b5b9d
Showing
8 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packy-api/src/main/java/com/dilly/gift/dto/response/LetterResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.dilly.gift.dto.response; | ||
|
||
import com.dilly.gift.domain.Letter; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Builder; | ||
|
||
@Builder | ||
public record LetterResponse( | ||
@Schema(example = "1") | ||
Long id, | ||
@Schema(example = "이 편지는 영국에서 시작되어...") | ||
String letterContent, | ||
EnvelopeResponse envelope | ||
) { | ||
|
||
public static LetterResponse from(Letter letter) { | ||
return LetterResponse.builder() | ||
.id(letter.getId()) | ||
.letterContent(letter.getContent()) | ||
.envelope(EnvelopeResponse.of(letter.getEnvelope())) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packy-domain/src/main/java/com/dilly/gift/adaptor/LetterReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.dilly.gift.adaptor; | ||
|
||
import com.dilly.exception.ErrorCode; | ||
import com.dilly.exception.entitynotfound.EntityNotFoundException; | ||
import com.dilly.gift.dao.LetterRepository; | ||
import com.dilly.gift.dao.querydsl.LetterQueryRepository; | ||
import com.dilly.gift.domain.Letter; | ||
import com.dilly.member.domain.Member; | ||
import java.time.LocalDateTime; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class LetterReader { | ||
|
||
private final LetterRepository letterRepository; | ||
private final LetterQueryRepository letterQueryRepository; | ||
|
||
public Letter findById(Long id) { | ||
return letterRepository.findById(id) | ||
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.LETTER_NOT_FOUND)); | ||
} | ||
|
||
public Slice<Letter> searchBySlice(Member member, LocalDateTime lastLetterDate, Pageable pageable) { | ||
return letterQueryRepository.searchBySlice(member, lastLetterDate, pageable); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packy-domain/src/main/java/com/dilly/gift/dao/GiftBoxRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package com.dilly.gift.dao; | ||
|
||
import com.dilly.gift.domain.GiftBox; | ||
import com.dilly.gift.domain.Letter; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface GiftBoxRepository extends JpaRepository<GiftBox, Long> { | ||
|
||
GiftBox findTopByOrderByIdDesc(); | ||
|
||
GiftBox findByLetter(Letter letter); | ||
} |
59 changes: 59 additions & 0 deletions
59
packy-domain/src/main/java/com/dilly/gift/dao/querydsl/LetterQueryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.dilly.gift.dao.querydsl; | ||
|
||
import static com.dilly.gift.domain.QGiftBox.giftBox; | ||
import static com.dilly.gift.domain.QLetter.letter; | ||
import static com.dilly.gift.domain.QReceiver.receiver; | ||
|
||
import com.dilly.gift.domain.Letter; | ||
import com.dilly.member.domain.Member; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.domain.SliceImpl; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class LetterQueryRepository { | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
public Slice<Letter> searchBySlice(Member member, LocalDateTime lastLetterDate, Pageable pageable) { | ||
List<Letter> results = jpaQueryFactory.select(letter) | ||
.from(receiver) | ||
.join(receiver.giftBox, giftBox) | ||
.join(giftBox.letter, letter) | ||
.where( | ||
ltLetterDate(lastLetterDate), | ||
receiver.member.eq(member) | ||
) | ||
.orderBy(receiver.createdAt.desc()) | ||
.limit(pageable.getPageSize() + 1L) | ||
.fetch(); | ||
|
||
return checkLastPage(pageable, results); | ||
} | ||
|
||
private BooleanExpression ltLetterDate(LocalDateTime lastLetterDate) { | ||
if (lastLetterDate == null) { | ||
return null; | ||
} | ||
|
||
return receiver.createdAt.lt(lastLetterDate); | ||
} | ||
|
||
private Slice<Letter> checkLastPage(Pageable pageable, List<Letter> results) { | ||
boolean hasNext = false; | ||
|
||
if (results.size() > pageable.getPageSize()) { | ||
results.remove(results.size() - 1); | ||
hasNext = true; | ||
} | ||
|
||
return new SliceImpl<>(results, pageable, hasNext); | ||
} | ||
} |