diff --git a/src/main/java/com/nice/petudio/api/controller/gift/GiftController.java b/src/main/java/com/nice/petudio/api/controller/gift/GiftController.java new file mode 100644 index 0000000..2ffea4c --- /dev/null +++ b/src/main/java/com/nice/petudio/api/controller/gift/GiftController.java @@ -0,0 +1,26 @@ +package com.nice.petudio.api.controller.gift; + +import com.nice.petudio.api.controller.gift.dto.GiftGenerateResponse; +import com.nice.petudio.api.controller.gift.service.GiftCodeGenerator; +import com.nice.petudio.api.controller.gift.service.GiftCommandService; +import com.nice.petudio.api.controller.gift.service.GiftQueryService; +import com.nice.petudio.api.dto.ApiResponse; +import com.nice.petudio.common.auth.admin.Admin; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1") +@RequiredArgsConstructor +public class GiftController { + private final GiftCommandService giftCommandService; + private final GiftQueryService giftQueryService; + + @Admin + @PostMapping("/gift/generate") + public ApiResponse generateGift() { + return ApiResponse.success(giftCommandService.generateGift()); + } +} diff --git a/src/main/java/com/nice/petudio/api/controller/gift/dto/GiftGenerateResponse.java b/src/main/java/com/nice/petudio/api/controller/gift/dto/GiftGenerateResponse.java new file mode 100644 index 0000000..e398953 --- /dev/null +++ b/src/main/java/com/nice/petudio/api/controller/gift/dto/GiftGenerateResponse.java @@ -0,0 +1,7 @@ +package com.nice.petudio.api.controller.gift.dto; + +public record GiftGenerateResponse(String giftCode) { + public static GiftGenerateResponse from(String giftCode) { + return new GiftGenerateResponse(giftCode); + } +} diff --git a/src/main/java/com/nice/petudio/api/controller/gift/service/GiftCodeGenerator.java b/src/main/java/com/nice/petudio/api/controller/gift/service/GiftCodeGenerator.java new file mode 100644 index 0000000..c661716 --- /dev/null +++ b/src/main/java/com/nice/petudio/api/controller/gift/service/GiftCodeGenerator.java @@ -0,0 +1,19 @@ +package com.nice.petudio.api.controller.gift.service; + +import java.util.UUID; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class GiftCodeGenerator { + + public String generate() { + String giftCode = UUID.randomUUID().toString(); + giftCode = giftCode.toUpperCase(); + giftCode = giftCode.replace("-", ""); + + log.info(String.format("[GIFT CODE 생성] %s", giftCode)); + return giftCode; + } +} diff --git a/src/main/java/com/nice/petudio/api/controller/gift/service/GiftCommandService.java b/src/main/java/com/nice/petudio/api/controller/gift/service/GiftCommandService.java new file mode 100644 index 0000000..c7dabaf --- /dev/null +++ b/src/main/java/com/nice/petudio/api/controller/gift/service/GiftCommandService.java @@ -0,0 +1,24 @@ +package com.nice.petudio.api.controller.gift.service; + +import com.nice.petudio.api.controller.gift.dto.GiftGenerateResponse; +import com.nice.petudio.domain.gift.Gift; +import com.nice.petudio.domain.gift.repository.GiftRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +@RequiredArgsConstructor +public class GiftCommandService { + private final GiftRepository giftRepository; + private final GiftCodeGenerator giftCodeGenerator; + + public GiftGenerateResponse generateGift() { + String giftCode = giftCodeGenerator.generate(); + Gift gift = Gift.newInstance(giftCode); + + giftRepository.save(gift); + return GiftGenerateResponse.from(giftCode); + } +} diff --git a/src/main/java/com/nice/petudio/api/controller/gift/service/GiftQueryService.java b/src/main/java/com/nice/petudio/api/controller/gift/service/GiftQueryService.java new file mode 100644 index 0000000..377e28b --- /dev/null +++ b/src/main/java/com/nice/petudio/api/controller/gift/service/GiftQueryService.java @@ -0,0 +1,11 @@ +package com.nice.petudio.api.controller.gift.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class GiftQueryService { +} diff --git a/src/main/java/com/nice/petudio/domain/gift/Gift.java b/src/main/java/com/nice/petudio/domain/gift/Gift.java index 7f4e850..df4efbd 100644 --- a/src/main/java/com/nice/petudio/domain/gift/Gift.java +++ b/src/main/java/com/nice/petudio/domain/gift/Gift.java @@ -10,11 +10,13 @@ import jakarta.persistence.Table; import lombok.AccessLevel; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @Entity @Table(name = "gifts") +@Builder(access = AccessLevel.PRIVATE) @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor(access = AccessLevel.PROTECTED) @@ -33,4 +35,11 @@ public class Gift extends BaseEntity { @Column(name = "is_used", nullable = false) private Boolean isUsed; + + public static Gift newInstance(String code) { + return Gift.builder() + .code(code) + .isUsed(false) + .build(); + } } diff --git a/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepository.java b/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepository.java index 957bf12..391ef33 100644 --- a/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepository.java +++ b/src/main/java/com/nice/petudio/domain/gift/repository/GiftRepository.java @@ -1,7 +1,8 @@ package com.nice.petudio.domain.gift.repository; import com.nice.petudio.domain.concept.Concept; +import com.nice.petudio.domain.gift.Gift; import org.springframework.data.jpa.repository.JpaRepository; -public interface GiftRepository extends GiftRepositoryCustom, JpaRepository { +public interface GiftRepository extends GiftRepositoryCustom, JpaRepository { } diff --git a/src/main/resources/sql/schema.sql b/src/main/resources/sql/schema.sql index a5ae4f6..f0388ee 100644 --- a/src/main/resources/sql/schema.sql +++ b/src/main/resources/sql/schema.sql @@ -96,7 +96,7 @@ CREATE TABLE `gifts` ( `gift_id` bigint AUTO_INCREMENT PRIMARY KEY, `member_id` bigint NULL, - `gift_number` varchar(100) NOT NULL, + `gift_code` varchar(100) NOT NULL, `is_used` boolean NOT NULL, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `modified_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP