-
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.
- Loading branch information
Showing
11 changed files
with
100 additions
and
15 deletions.
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
23 changes: 21 additions & 2 deletions
23
src/main/java/com/nice/petudio/api/controller/gift/service/GiftCommandService.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,24 +1,43 @@ | ||
package com.nice.petudio.api.controller.gift.service; | ||
|
||
import com.nice.petudio.api.controller.gift.dto.GiftGenerateResponse; | ||
import com.nice.petudio.common.exception.error.ErrorCode; | ||
import com.nice.petudio.common.exception.model.ValidationException; | ||
import com.nice.petudio.domain.gift.Gift; | ||
import com.nice.petudio.domain.gift.repository.GiftRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Slf4j | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class GiftCommandService { | ||
private final GiftRepository giftRepository; | ||
private final GiftCodeGenerator giftCodeGenerator; | ||
|
||
public GiftGenerateResponse generateGift() { | ||
public GiftGenerateResponse generateGift(final Long memberId) { | ||
String giftCode = giftCodeGenerator.generate(); | ||
Gift gift = Gift.newInstance(giftCode); | ||
Gift gift = Gift.newInstance(memberId, giftCode); | ||
|
||
giftRepository.save(gift); | ||
return GiftGenerateResponse.from(giftCode); | ||
} | ||
|
||
public void useGift(final Long memberId, final String giftCode) { | ||
Gift gift = GiftServiceUtils.findByGiftId(giftRepository, giftCode); | ||
validateGiftIsNotUsed(gift); | ||
|
||
gift.use(memberId); | ||
log.info(String.format("[기프트 사용] 기프트 (GIFT_ID: %d)가 회원 (MEMBER_ID: %d)에 의해 사용되었습니다.", gift.getId(), memberId)); | ||
} | ||
|
||
private void validateGiftIsNotUsed(Gift gift) { | ||
if(gift.isUsed()) { | ||
throw new ValidationException(ErrorCode.ALREADY_USED_GIFT_EXCEPTION, | ||
String.format("이미 사용된 기프트 (GIFT_ID: %d) 입니다.", gift.getId())); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/nice/petudio/api/controller/gift/service/GiftServiceUtils.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,15 @@ | ||
package com.nice.petudio.api.controller.gift.service; | ||
|
||
import com.nice.petudio.common.exception.error.ErrorCode; | ||
import com.nice.petudio.common.exception.model.NotFoundException; | ||
import com.nice.petudio.domain.gift.Gift; | ||
import com.nice.petudio.domain.gift.repository.GiftRepository; | ||
|
||
public class GiftServiceUtils { | ||
|
||
public static Gift findByGiftId(GiftRepository giftRepository, final String giftCode) { | ||
return giftRepository.findByGiftCode(giftCode) | ||
.orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_GIFT_EXCEPTION, | ||
String.format("해당하는 GiftCode (%s)를 가진 Gift가 존재하지 않습니다.", giftCode))); | ||
} | ||
} |
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
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
2 changes: 1 addition & 1 deletion
2
src/main/java/com/nice/petudio/domain/gift/repository/GiftRepository.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
4 changes: 4 additions & 0 deletions
4
src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryCustom.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,4 +1,8 @@ | ||
package com.nice.petudio.domain.gift.repository; | ||
|
||
import com.nice.petudio.domain.gift.Gift; | ||
import java.util.Optional; | ||
|
||
public interface GiftRepositoryCustom { | ||
Optional<Gift> findByGiftCode(String giftCode); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/nice/petudio/domain/gift/repository/GiftRepositoryImpl.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,21 @@ | ||
package com.nice.petudio.domain.gift.repository; | ||
|
||
import static com.nice.petudio.domain.gift.QGift.gift; | ||
|
||
import com.nice.petudio.domain.gift.Gift; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class GiftRepositoryImpl implements GiftRepositoryCustom { | ||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Optional<Gift> findByGiftCode(String giftCode) { | ||
return Optional.ofNullable(queryFactory | ||
.selectFrom(gift) | ||
.where(gift.code.eq(giftCode)) | ||
.fetchOne()); | ||
} | ||
} |
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