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_유보민 5주차 과제 (2단계) #300

Open
wants to merge 43 commits into
base: rbm0524
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b8a0f64
Move from spring-gift-enhancement/step3
rbm0524 Jul 23, 2024
4d8103b
docs : 1단계 요구사항 작성
rbm0524 Jul 25, 2024
1c71190
image : 로그인 화면에 사용할 이미지 추가
rbm0524 Jul 25, 2024
a84b2ab
feat : 카카오 api 사용에 필요한 Dto 작성
rbm0524 Jul 25, 2024
9e6a03d
feat : 카카오 api 사용에 필요한 Dto 작성
rbm0524 Jul 25, 2024
e923af2
feat : 카카오 api를 통해 로그인 기능을 하는 Controller 작성
rbm0524 Jul 25, 2024
3578bd9
feat : 카카오 서버로부터 받은 토큰 검증하는 클래스 작성
rbm0524 Jul 25, 2024
e8c36ee
refactor : clientId 추가
rbm0524 Jul 25, 2024
684b2ea
refactor : 카카오 로그인 버튼 추가
rbm0524 Jul 25, 2024
c7fd8e0
refactor : 카카오 토큰 관련 로직 추가
rbm0524 Jul 25, 2024
265c315
refactor : 카카오id 저장할 attribute 추가
rbm0524 Jul 25, 2024
7f644c1
refactor : 카카오id 저장할 필드 추가
rbm0524 Jul 25, 2024
603d993
refactor : 카카오id로 Member 찾는 메서드 추가
rbm0524 Jul 25, 2024
8a36eae
refactor : 카카오id 관련 메서드 추가, Member에 kakaoId set 추가
rbm0524 Jul 25, 2024
fc6e7cd
refactor : Optional로 받도록 수정
rbm0524 Jul 25, 2024
1c96884
refactor : Qualifier 제거
rbm0524 Jul 25, 2024
1d483c9
docs : README.md 수정
rbm0524 Jul 25, 2024
d5c2937
feat : KakaoOAuthController 테스트 작성
rbm0524 Jul 25, 2024
f3789e5
refactor : OptionService 추가
rbm0524 Jul 26, 2024
ef7572f
refactor : OptionService 추가
rbm0524 Jul 26, 2024
e71c19d
feat : purchaseWishlist 추가
rbm0524 Jul 26, 2024
b260eae
refactor : subtractOption이 List를 받도록 수정
rbm0524 Jul 26, 2024
3e730a1
refactor : 한 줄 띄우기
rbm0524 Jul 26, 2024
a59b5a0
refactor : Option 보여주는 부분 추가
rbm0524 Jul 26, 2024
8bca880
refactor : 구매하기 기능 추가
rbm0524 Jul 26, 2024
c3f1bf8
refactor : Option 외래키 추가
rbm0524 Jul 26, 2024
2d345fa
refactor : 안쓰는 import 삭제
rbm0524 Jul 26, 2024
9da2bfa
refactor : optionId 추가
rbm0524 Jul 26, 2024
8901763
refactor : optionJpaRepository 추가
rbm0524 Jul 26, 2024
a369e3a
refactor : 테스트에 사용할 Option 추가
rbm0524 Jul 26, 2024
592c130
docs : README.md 2단계 요구사항 추가
rbm0524 Jul 26, 2024
0e4ac1d
feat : 카카오 나에게 메시지 보내 기능을 위한 클래스 작성
rbm0524 Jul 26, 2024
9bcad34
fix : 테스트 수정
rbm0524 Jul 27, 2024
73373cc
fix : 타입 수정
rbm0524 Jul 27, 2024
2db30a0
feat: getter, setter 추가
rbm0524 Jul 27, 2024
6678a21
refactor : userId로 되어있던 부분 memberId로 수정
rbm0524 Jul 27, 2024
7a315e1
feat : getter, setter 추가
rbm0524 Jul 27, 2024
eeded51
refactor : import 수정
rbm0524 Jul 27, 2024
3671d08
feat : 주문 이후 위시리스트 비우는 함수 추가
rbm0524 Jul 27, 2024
1f1b290
reafactor : memberId로 수정
rbm0524 Jul 27, 2024
4bb18a3
feat : memberId에 따라 위시리스트 비우는 기능 추가
rbm0524 Jul 27, 2024
8087c6e
feat : memberId로 수정
rbm0524 Jul 27, 2024
b948f7b
Merge branch 'rbm0524' into step2
rbm0524 Jul 27, 2024
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@
- 특히 시크릿 키는 GitHub나 클라이언트 코드 등 외부에서 볼 수 있는 곳에 추가하지 않는다.
6. 오류 처리
레퍼런스와 문제 해결을 참고하여 발생할 수 있는 다양한 오류를 처리한다.

# 2단계 - 주문하기

## 요구사항
### 기능 요구사항
1. 카카오톡 메시지 API를 사용하여 주문하기 기능을 구현한다.
2. 주문할 때 수령인에게 보낼 메시지를 작성할 수 있다.
3. 상품 옵션과 해당 수량을 선택하여 주문하면 해당 상품 옵션의 수량이 차감된다.
4. 해당 상품이 위시 리스트에 있는 경우 위시 리스트에서 삭제한다.
5. 나에게 보내기를 읽고 주문 내역을 카카오톡 메시지로 전송한다.
6. 메시지는 메시지 템플릿의 기본 템플릿이나 사용자 정의 템플릿을 사용하여 자유롭게 작성한다.
45 changes: 41 additions & 4 deletions src/main/java/gift/Controller/OptionController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package gift.Controller;

import gift.Annotation.LoginMemberResolver;
import gift.Model.MemberDto;
import gift.Model.OptionDto;
import gift.Model.OrderRequestDto;
import gift.Service.KakaoTalkService;
import gift.Service.OptionService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
Expand All @@ -16,10 +20,12 @@
public class OptionController {

private final OptionService optionService;
private final KakaoTalkService kakaoTalkService;

@Autowired
public OptionController(OptionService optionService) {
public OptionController(OptionService optionService, KakaoTalkService kakaoTalkService) {
this.optionService = optionService;
this.kakaoTalkService = kakaoTalkService;
}

@GetMapping("/api/products/options/{productId}")
Expand Down Expand Up @@ -80,4 +86,35 @@ public ResponseEntity<?> deleteOption(@PathVariable long optionId) {
return ResponseEntity.badRequest().body("Error deleting option");
}
}

@PostMapping("/option/purchase")
public ResponseEntity<String> purchaseWishlist(@LoginMemberResolver MemberDto memberDto, @RequestBody List<OrderRequestDto> orderRequestDtoList, HttpServletRequest request) {
for (OrderRequestDto orderRequestDto : orderRequestDtoList) {
orderRequestDto.setMemberId(memberDto.getId());
}
optionService.subtractOption(orderRequestDtoList);

//카카오 토큰 가져오기
String token = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("accessToken".equals(cookie.getName())) {
token = cookie.getValue();
break;
}
}
}

//토큰으로 메시지 보내기
if (token != null) { //
try {
kakaoTalkService.sendMessageToMe(token, orderRequestDtoList);
} catch (Exception e) {
return ResponseEntity.badRequest().body("Error sending message");
}
}

return ResponseEntity.ok("Purchase successful");
}
}
9 changes: 8 additions & 1 deletion src/main/java/gift/Controller/WishlistController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import gift.Annotation.LoginMemberResolver;
import gift.Entity.Wishlist;
import gift.Model.MemberDto;
import gift.Model.ProductDto;
import gift.Model.WishlistDto;
import gift.Service.WishlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -63,4 +63,11 @@ public String removeWishlistItem(@LoginMemberResolver MemberDto memberDto, @Requ

return "redirect:/wishlist";
}

@PostMapping("/wishlist/clear")
public ResponseEntity<?> clearWishlist(@LoginMemberResolver MemberDto memberDto) {
wishlistService.clearWishlist(memberDto.getId());
return ResponseEntity.ok().build();
}

}
12 changes: 6 additions & 6 deletions src/main/java/gift/Entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Member {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long userId;
private long memberId;
private String email;
private String name;
private String password;
Expand All @@ -17,8 +17,8 @@ public class Member {
protected Member() {
}

public Member(long userId, String email, String name, String password, boolean isAdmin) {
this.userId = userId;
public Member(long memberId, String email, String name, String password, boolean isAdmin) {
this.memberId = memberId;
this.email = email;
this.name = name;
this.password = password;
Expand All @@ -27,11 +27,11 @@ public Member(long userId, String email, String name, String password, boolean i

// Getters and setters
public long getId() {
return userId;
return memberId;
}

public void setId(Long userId) {
this.userId = userId;
public void setId(Long memberId) {
this.memberId = memberId;
}

public String getName() {
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/gift/Entity/Wishlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class Wishlist {
private WishlistId id;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@MapsId("userId")
@JoinColumn(name = "userId", referencedColumnName = "userId")
@MapsId("memberId")
@JoinColumn(name = "memberId", referencedColumnName = "memberId")
private Member member;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
Expand All @@ -25,16 +25,21 @@ public class Wishlist {
private int count;
private int price;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@JoinColumn(name = "optionId", referencedColumnName = "id")
private Option option;

protected Wishlist() {
}

public Wishlist(WishlistId id, Member member, Product product, String productName, int count, int price) {
public Wishlist(WishlistId id, Member member, Product product, String productName, int count, int price, Option option) {
this.id = id;
this.member = member;
this.product = product;
this.productName = productName;
this.count = count;
this.price = price;
this.option = option;
}

public WishlistId getId() {
Expand All @@ -45,12 +50,12 @@ public void setId(WishlistId id) {
this.id = id;
}

public long getUserId() {
return id.getUserId();
public long getMemberId() {
return id.getMemberId();
}

public void setUserId(long userId) {
id.setUserId(userId);
public void setMemberId(long memberId) {
id.setMemberId(memberId);
}

public long getProductId() {
Expand Down Expand Up @@ -84,4 +89,8 @@ public int getPrice() {
public void setPrice(int price) {
this.price = price;
}

public Option getOption() {
return option;
}
}
14 changes: 7 additions & 7 deletions src/main/java/gift/Entity/WishlistId.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
@Embeddable
public class WishlistId implements Serializable {

private long userId;
private long memberId;
private long productId;

protected WishlistId() {
}

public WishlistId(long userId, long productId) {
this.userId = userId;
public WishlistId(long memberId, long productId) {
this.memberId = memberId;
this.productId = productId;
}

public long getUserId() {
return userId;
public long getMemberId() {
return memberId;
}

public void setUserId(long userId) {
this.userId = userId;
public void setMemberId(long memberId) {
this.memberId = memberId;
}

public long getProductId() {
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/gift/Mapper/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import gift.Entity.*;
import gift.Model.*;
import gift.Service.CategoryService;
import gift.Service.ProductService;
import gift.Service.MemberService;
import gift.Service.WishlistService;
import gift.Service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
Expand All @@ -19,13 +16,15 @@ public class Mapper {
private final MemberService memberService;
private final WishlistService wishlistService;
private final CategoryService categoryService;
private final OptionService optionService;

@Autowired
public Mapper(@Lazy ProductService productService, @Lazy MemberService memberService, @Lazy WishlistService wishListService, @Lazy CategoryService categoryService) {
public Mapper(@Lazy ProductService productService, @Lazy MemberService memberService, @Lazy WishlistService wishListService, @Lazy CategoryService categoryService, @Lazy OptionService optionService) {
this.productService = productService;
this.memberService = memberService;
this.wishlistService = wishListService;
this.categoryService = categoryService;
this.optionService = optionService;
}


Expand All @@ -37,13 +36,15 @@ public Wishlist wishlistDtoToEntity(WishlistDto wishlistDto) {
ProductDto productDto = productDtoOptional.get();
productDto.setCategoryId(productDtoOptional.get().getCategoryId());

OptionDto optionDto = optionService.getOptionById(wishlistDto.getOptionId());

WishlistId id = new WishlistId(memberDto.getId(), productDto.getId());
return new Wishlist(id, memberDtoToEntity(memberDto), productDtoToEntity(productDto), wishlistDto.getProductName(), wishlistDto.getCount(), wishlistDto.getPrice());
return new Wishlist(id, memberDtoToEntity(memberDto), productDtoToEntity(productDto), wishlistDto.getProductName(), wishlistDto.getCount(), wishlistDto.getPrice(), optionDtoToEntity(optionDto));

}

public WishlistDto wishlistToDto(Wishlist wishlist) {
return new WishlistDto(wishlist.getUserId(), wishlist.getProductId(), wishlist.getCount(), 0, wishlist.getProductName(), wishlist.getPrice());
return new WishlistDto(wishlist.getMemberId(), wishlist.getProductId(), wishlist.getCount(), 0, wishlist.getProductName(), wishlist.getPrice(), wishlist.getOption().getId());
}

public Category categoryDtoToEntity(CategoryDto categoryDto) {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/gift/Model/Link.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gift.Model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Link {

private String web_url;
private String mobile_web_url;

public Link(String web_url, String mobile_web_url) {
this.web_url = web_url;
this.mobile_web_url = mobile_web_url;
}


public String getWeb_url() {
return web_url;
}

public void setWeb_url(String web_url) {
this.web_url = web_url;
}

public String getMobile_web_url() {
return mobile_web_url;
}

public void setMobile_web_url(String mobile_web_url) {
this.mobile_web_url = mobile_web_url;
}
}
41 changes: 41 additions & 0 deletions src/main/java/gift/Model/TextObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gift.Model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class TextObject {

private String object_type;
private String text;
private Link link;

public TextObject(String object_type, String text, Link link) {
this.object_type = object_type;
this.text = text;
this.link = link;
}

public String getObject_type() {
return object_type;
}

public void setObject_type(String object_type) {
this.object_type = object_type;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public Link getLink() {
return link;
}

public void setLink(Link link) {
this.link = link;
}
}
12 changes: 11 additions & 1 deletion src/main/java/gift/Model/WishlistDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ public class WishlistDto {
private int quantity;//뺄 개수
private String productName;
private int price;
private long optionId;

public WishlistDto() {
}

public WishlistDto(long userId, long productId, int count, int quantity, String productName, int price) {
public WishlistDto(long userId, long productId, int count, int quantity, String productName, int price, long optionId) {
this.userId = userId;
this.productId = productId;
this.count = count;
this.quantity = quantity;
this.productName = productName;
this.price = price;
this.optionId = optionId;
}

public long getUserId() {
Expand Down Expand Up @@ -67,4 +69,12 @@ public int getQuantity() {
public void setQuantity(int quantity) {
this.quantity = quantity;
}

public long getOptionId() {
return optionId;
}

public void setOptionId(long optionId) {
this.optionId = optionId;
}
}
Loading