diff --git a/README.md b/README.md index 7ebbff580..e60301fa9 100644 --- a/README.md +++ b/README.md @@ -17,4 +17,10 @@ 2. 서비스 필요 DTO 추가 3. 서비스 필요 컨트롤러 추가 -4. 카카오 메세지 전송 서비스 추가 \ No newline at end of file +4. 카카오 메세지 전송 서비스 추가 + +## step3 + +1. 사용자 포인트 엔티티 추가 +2. 포인트 서비스 추가 +3. 관리자 포인트 충전 추가 diff --git a/src/main/java/gift/admin/controller/AdminProductController.java b/src/main/java/gift/admin/controller/AdminProductController.java index c9cffbb5b..31c1b72e3 100644 --- a/src/main/java/gift/admin/controller/AdminProductController.java +++ b/src/main/java/gift/admin/controller/AdminProductController.java @@ -1,7 +1,11 @@ package gift.admin.controller; +import gift.admin.dto.LeafMemberDTO; import gift.admin.dto.LeafProductDTO; +import gift.auth.argumentResolver.KakaoMember; +import gift.member.dto.MemberRequest; import gift.product.dto.ProductRequest; +import gift.product.entity.Product; import gift.product.service.ProductService; import jakarta.validation.Valid; import org.springframework.stereotype.Controller; @@ -69,10 +73,20 @@ public String deletePageSubmit(@PathVariable Long id) { return "redirect:/admin/products"; } + @PostMapping("/member") + public String addPoint(@PathVariable Long id, @KakaoMember String token,@ModelAttribute("leafMemberDTO") LeafMemberDTO leafMemberDTO) { + MemberRequest memberRequest = toRestRequest(leafMemberDTO); + return "redirect:/admin/products"; + } + private ProductRequest toRestRequest(LeafProductDTO leafProductDTO) { return new ProductRequest(leafProductDTO.getId(), leafProductDTO.getName(), leafProductDTO.getPrice(), leafProductDTO.getImageUrl(), leafProductDTO.getCategoryId(), leafProductDTO.getGiftOptionName(), leafProductDTO.getGiftOptionQuantity()); } + private MemberRequest toRestRequest(LeafMemberDTO leafMemberDTO) { + return new MemberRequest(leafMemberDTO.getId(), leafMemberDTO.getEmail(), leafMemberDTO.getPoint()); + } + } diff --git a/src/main/java/gift/admin/dto/LeafMemberDTO.java b/src/main/java/gift/admin/dto/LeafMemberDTO.java new file mode 100644 index 000000000..48d638938 --- /dev/null +++ b/src/main/java/gift/admin/dto/LeafMemberDTO.java @@ -0,0 +1,41 @@ +package gift.admin.dto; + +public class LeafMemberDTO { + + private Long id; + private String email; + private Integer point; + + public LeafMemberDTO() { + } + + public LeafMemberDTO(Long id, String email, Integer point) { + this.id = id; + this.email = email; + this.point = point; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Integer getPoint() { + return point; + } + + public void setPoint(Integer point) { + this.point = point; + } +} diff --git a/src/main/java/gift/member/dto/MemberRequest.java b/src/main/java/gift/member/dto/MemberRequest.java index 7a0fcaab1..58f627304 100644 --- a/src/main/java/gift/member/dto/MemberRequest.java +++ b/src/main/java/gift/member/dto/MemberRequest.java @@ -11,6 +11,7 @@ public class MemberRequest { @NotBlank(message = "비밀번호를 입력해주세요") private String password; private MemberRole role; + private Integer point; public MemberRequest() { } @@ -31,6 +32,12 @@ public MemberRequest(Long id, String email, String password, MemberRole role) { this.role = role; } + public MemberRequest(Long id, String email, Integer point) { + this.id = id; + this.email = email; + this.point = point; + } + public Long getId() { return id; } diff --git a/src/main/java/gift/member/entity/Member.java b/src/main/java/gift/member/entity/Member.java index 853aba950..491bface8 100644 --- a/src/main/java/gift/member/entity/Member.java +++ b/src/main/java/gift/member/entity/Member.java @@ -36,11 +36,16 @@ public class Member { @OneToMany(mappedBy = "member") private List wishList = new ArrayList<>(); - public Member(Long id, String email, String password, MemberRole role) { + @Column(name="MEMBER_POINT") + private Integer point; + + public Member(Long id, String email, String password, MemberRole role, Integer point) { this.id = id; this.email = email; this.password = password; this.role = role; + this.point = point; + } public Member() { @@ -78,4 +83,19 @@ public List getWishList() { return wishList; } + public Integer getPoint() { + return point; + } + + public void usePoint(Integer point) { + this.point -= point; + if(this.point < 0){ + this.point = 0; + } + } + + public void useAllPoint(){ + this.point = 0; + } + } diff --git a/src/main/java/gift/member/service/MemberService.java b/src/main/java/gift/member/service/MemberService.java index fc30c151d..0a72e53fd 100644 --- a/src/main/java/gift/member/service/MemberService.java +++ b/src/main/java/gift/member/service/MemberService.java @@ -30,7 +30,7 @@ public void register(MemberRequest memberRequest) { throw new GiftUnauthorizedException("사용할 수 없는 이메일입니다."); } Member member = new Member(null, memberRequest.getEmail(), memberRequest.getPassword(), - memberRequest.getRole()); + memberRequest.getRole(), 0); jpaMemberRepository.save(member); } diff --git a/src/main/java/gift/order/service/OrderService.java b/src/main/java/gift/order/service/OrderService.java index 2805f8537..90e8362e9 100644 --- a/src/main/java/gift/order/service/OrderService.java +++ b/src/main/java/gift/order/service/OrderService.java @@ -52,5 +52,18 @@ public void create(OrderRequest orderRequest) { jpaWishRepository.deleteByMemberIdAndProductId(member.getId(), product.getId()); } + usePoint(product.getPrice()*orderRequest.getQuantity(),member); + + } + + private int usePoint(int price, Member member){ + int memberPoint = member.getPoint(); + + if(memberPoint < price){ + member.useAllPoint(); + return price - memberPoint; + } + member.usePoint(price); + return 0; } } diff --git a/src/test/java/gift/service/WishListServiceTest.java b/src/test/java/gift/service/WishListServiceTest.java index 7abef69fc..62ec2cebf 100644 --- a/src/test/java/gift/service/WishListServiceTest.java +++ b/src/test/java/gift/service/WishListServiceTest.java @@ -28,7 +28,7 @@ class WishListServiceTest { @DisplayName("새로운 상품 목록 추가") void addProduct(){ //given - Member member = new Member(null,"hah@ha","1234", MemberRole.COMMON_MEMBER); + Member member = new Member(null,"hah@ha","1234", MemberRole.COMMON_MEMBER,0); Product product = new Product(null,"tuna",4000, "test"); member = jpaMemberRepository.save(member); @@ -49,7 +49,7 @@ void addProduct(){ @DisplayName("새로운 상품 여러개 추가") void addMultiProduct(){ //given - Member member = new Member(null,"hah@ha","1234", MemberRole.COMMON_MEMBER); + Member member = new Member(null,"hah@ha","1234", MemberRole.COMMON_MEMBER,0); Product product = new Product(null,"tuna",4000, "test"); Product product2 = new Product(null,"tuna2",5000, "test2"); Product product3 = new Product(null,"tuna3",6000, "test3");