Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

부산대 BE 이영준 6주차 Step3 #443

Open
wants to merge 5 commits into
base: 20jcode
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@
2. 서비스 필요 DTO 추가
3. 서비스 필요 컨트롤러 추가

4. 카카오 메세지 전송 서비스 추가
4. 카카오 메세지 전송 서비스 추가

## step3

1. 사용자 포인트 엔티티 추가
2. 포인트 서비스 추가
3. 관리자 포인트 충전 추가
14 changes: 14 additions & 0 deletions src/main/java/gift/admin/controller/AdminProductController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}

}
41 changes: 41 additions & 0 deletions src/main/java/gift/admin/dto/LeafMemberDTO.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions src/main/java/gift/member/dto/MemberRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class MemberRequest {
@NotBlank(message = "비밀번호를 입력해주세요")
private String password;
private MemberRole role;
private Integer point;

public MemberRequest() {
}
Expand All @@ -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;
}
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/gift/member/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ public class Member {
@OneToMany(mappedBy = "member")
private List<Wish> 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() {
Expand Down Expand Up @@ -78,4 +83,19 @@ public List<Wish> 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;
}

}
2 changes: 1 addition & 1 deletion src/main/java/gift/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/gift/order/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/test/java/gift/service/WishListServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand Down