Skip to content

Commit

Permalink
Merge pull request #218 from TeamDilly/develop
Browse files Browse the repository at this point in the history
v1.2.4
  • Loading branch information
leeeeeyeon authored May 4, 2024
2 parents d785059 + b97f86d commit fd616fb
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 21 deletions.
16 changes: 16 additions & 0 deletions packy-api/src/main/java/com/dilly/gift/api/GiftBoxController.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ public DataResponseDto<GiftBoxResponse> openGiftBox(
return DataResponseDto.from(giftBoxService.openGiftBox(giftBoxId));
}

@Operation(summary = "(WEB) 선물박스 열기", description = """
선물이 없을 경우 응답에서 gift 객체가 제외됩니다. <br>
선물박스를 보낸 뒤 7일이 지나면 선물박스를 열 수 없습니다. <br>
""")
@ApiErrorCodeExamples({
ErrorCode.GIFTBOX_NOT_FOUND,
ErrorCode.GIFTBOX_ALREADY_DELETED,
ErrorCode.GIFTBOX_URL_EXPIRED
})
@GetMapping("/web/{giftBoxUuid}")
public DataResponseDto<GiftBoxResponse> openGiftBoxForWeb(
@PathVariable("giftBoxUuid") String giftBoxUuid
) {
return DataResponseDto.from(giftBoxService.openGiftBoxForWeb(giftBoxUuid));
}

@Operation(summary = "주고받은 선물박스 조회")
@Parameter(in = ParameterIn.QUERY,
description = "한 페이지에 보여줄 선물박스 개수. 기본값은 6개",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,20 @@ public GiftBoxResponse openGiftBox(Long giftBoxId) {

checkIfGiftBoxOpenable(member, giftBox);

BoxResponse boxResponse = BoxResponse.from(giftBox.getBox());
EnvelopeResponse envelopeResponse = EnvelopeResponse.from(
giftBox.getLetter().getEnvelope());
List<PhotoResponse> photos = photoReader.findAllByGiftBox(giftBox).stream()
.map(PhotoResponse::from)
.sorted(Comparator.comparingInt(PhotoResponse::sequence))
.toList();
List<StickerResponse> stickers = giftBoxStickerReader.findAllByGiftBox(giftBox).stream()
.map(StickerResponse::from)
.sorted(Comparator.comparingInt(StickerResponse::location))
.toList();
return toGiftBoxResponse(giftBox);
}

GiftResponse giftResponse = null;
if (giftBox.getGift() != null) {
giftResponse = GiftResponse.from(giftBox.getGift());
}
public GiftBoxResponse openGiftBoxForWeb(String giftBoxUuid) {
GiftBox giftBox = giftBoxReader.findByUuid(giftBoxUuid);

return GiftBoxResponse.of(giftBox, boxResponse, envelopeResponse, photos, stickers,
giftResponse);
LocalDateTime sentDate = giftBox.getUpdatedAt();
LocalDateTime now = LocalDateTime.now();

if (now.minusDays(7).isAfter(sentDate)) {
throw new UnsupportedException(ErrorCode.GIFTBOX_URL_EXPIRED);
}

return toGiftBoxResponse(giftBox);
}

// TODO: 성능 개선 필요
Expand Down Expand Up @@ -376,4 +371,26 @@ public MainGiftBoxResponse getMainGiftBox() {

return mainGiftBoxResponse;
}

public GiftBoxResponse toGiftBoxResponse(GiftBox giftBox) {
BoxResponse boxResponse = BoxResponse.from(giftBox.getBox());
EnvelopeResponse envelopeResponse = EnvelopeResponse.from(
giftBox.getLetter().getEnvelope());
List<PhotoResponse> photos = photoReader.findAllByGiftBox(giftBox).stream()
.map(PhotoResponse::from)
.sorted(Comparator.comparingInt(PhotoResponse::sequence))
.toList();
List<StickerResponse> stickers = giftBoxStickerReader.findAllByGiftBox(giftBox).stream()
.map(StickerResponse::from)
.sorted(Comparator.comparingInt(StickerResponse::location))
.toList();

GiftResponse giftResponse = null;
if (giftBox.getGift() != null) {
giftResponse = GiftResponse.from(giftBox.getGift());
}

return GiftBoxResponse.of(giftBox, boxResponse, envelopeResponse, photos, stickers,
giftResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
"/api/v1/admin/design/profiles",
"/api/v1/auth/sign-up",
"/api/v1/auth/sign-in/**",
"/api/v1/auth/reissue"
"/api/v1/auth/reissue",
"/api/v1/giftboxes/web/**"
).permitAll()
.anyRequest().authenticated()
)
Expand Down
4 changes: 1 addition & 3 deletions packy-api/src/main/java/com/dilly/jwt/TokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class TokenProvider {
private static final String AUTHORITIES_KEY = "auth";
private static final String BEARER_TYPE = "Bearer";
private static final long ACCESS_TOKEN_EXPIRE_TIME = 1000L * 60 * 30; // 30분
private static final long REFRESH_TOKEN_EXPIRE_TIME = 1000L * 60 * 60 * 24 * 7; // 7일

private final Key key;

Expand All @@ -44,7 +43,6 @@ public TokenProvider(@Value("${jwt.secret}") String secretKey) {
public JwtResponse generateJwt(Member member) {
long now = (new Date()).getTime();
Date accessTokenExpiresIn = new Date(now + (ACCESS_TOKEN_EXPIRE_TIME));
Date refreshTokenExpiresIn = new Date(now + (REFRESH_TOKEN_EXPIRE_TIME));

// Access Token 생성
String accessToken = Jwts.builder()
Expand All @@ -58,7 +56,7 @@ public JwtResponse generateJwt(Member member) {

// Refresh Token 생성
String refreshToken = Jwts.builder()
.setExpiration(refreshTokenExpiresIn)
.setSubject(member.getId().toString())
.signWith(key, SignatureAlgorithm.HS512)
.compact();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public enum ErrorCode {
GIFTBOX_ALREADY_OPENDED(HttpStatus.CONFLICT, "이미 열린 선물입니다."),
GIFTBOX_ACCESS_DENIED(HttpStatus.FORBIDDEN, "선물박스에 접근할 수 없습니다."),
GIFTBOX_ALREADY_DELETED(HttpStatus.NOT_FOUND, "이미 삭제된 선물박스입니다."),
GIFTBOX_URL_EXPIRED(HttpStatus.BAD_REQUEST, "URL이 만료되었습니다."),

// Version
FAILED_TO_EXTRACT_VERSION(HttpStatus.BAD_REQUEST, "사용자 버전을 추출하는데 실패했습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public GiftBox findById(Long giftBoxId) {
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.GIFTBOX_NOT_FOUND));
}

public GiftBox findByUuid(String uuid) {
return giftBoxRepository.findByUuid(uuid)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.GIFTBOX_NOT_FOUND));
}

public GiftBox findByLetter(Letter letter) {
return giftBoxRepository.findByLetter(letter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import com.dilly.gift.domain.letter.Letter;
import com.dilly.member.domain.Member;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface GiftBoxRepository extends JpaRepository<GiftBox, Long> {

GiftBox findTopByOrderByIdDesc();

Optional<GiftBox> findByUuid(String uuid);

GiftBox findByLetter(Letter letter);

List<GiftBox> findTop6BySenderAndDeliverStatusAndSenderDeletedOrderByCreatedAtDesc(
Expand Down

0 comments on commit fd616fb

Please sign in to comment.