Skip to content

Commit

Permalink
feat: 기프트 생성 기능 구현 #44
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed Feb 6, 2024
1 parent d685e60 commit 902cce2
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<GiftGenerateResponse> generateGift() {
return ApiResponse.success(giftCommandService.generateGift());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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 {
}
9 changes: 9 additions & 0 deletions src/main/java/com/nice/petudio/domain/gift/Gift.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Concept, Long> {
public interface GiftRepository extends GiftRepositoryCustom, JpaRepository<Gift, Long> {
}
2 changes: 1 addition & 1 deletion src/main/resources/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 902cce2

Please sign in to comment.