Skip to content

Commit

Permalink
Feat: 적립금, 주문 생성 api 구현
Browse files Browse the repository at this point in the history
적립금, 주문 생성 api 구현
  • Loading branch information
hejin8307 authored May 8, 2024
2 parents e471724 + d913e7d commit 89c5bab
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/hanaro/starbucks/config/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Authentication getAuthentication(String token) {

// http 요청에서 "TOKEN" 헤더 값을 읽어오는 메소드
public String resolveToken(HttpServletRequest request) {
return request.getHeader("TOKEN");
return request.getHeader("Authorization");
}

public boolean validateToken(String token){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void deleteUser(@PathVariable int userIdx){

@GetMapping("/points")
public PointResDto getUserPoint(@RequestHeader MultiValueMap<String, String> headers){
return memberService.getUserPoint(headers.getFirst("token"));
String originalToken = headers.getFirst("authorization");
return memberService.getUserPoint(originalToken.substring(7, originalToken.length()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public void deleteOrder(@PathVariable int orderIdx) {

@PostMapping("")
public void createOrder(@RequestHeader MultiValueMap<String, String> headers, @RequestBody List<OrderReqDto> dtos) {
if(headers.containsKey("token") ){
orderService.createOrder(headers.getFirst("token"), dtos);
String originalToken = headers.getFirst("authorization");
if(headers.containsKey("authorization") ){
String token = originalToken.substring(7, originalToken.length());
orderService.createOrder(token, dtos);
}else{
orderService.createOrder("null", dtos);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/hanaro/starbucks/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public void update(MemberUpdateReqDto dto) {
this.userJoinDate = LocalDate.now();
}

public void update(int userPoint) {
this.userPoint = userPoint;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
//계정의 권한 목록을 리턴함.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/hanaro/starbucks/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public void updateUser(int userIdx, MemberUpdateReqDto user){
memberRepository.save(member);
}

public void updateUserPoint(int userIdx, int userPoint) {
Optional<Member> optional = memberRepository.findById(userIdx);
if(optional.isEmpty()){
throw new IllegalArgumentException("존재하지 않는 회원입니다.");
}
Member member = optional.get();
member.update(member.getUserPoint() + userPoint);
memberRepository.save(member);
}

public void deleteUser(int userIdx){
memberRepository.deleteById(userIdx);
}
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/hanaro/starbucks/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ private Orders findOrderById(int orderIdx) {
public void createOrder(String token, List<OrderReqDto> dtos){
Member member;
Orders order;
int userIdx = -1;

if(token.equals("null")){
System.out.println("~~~~~~~~~~~~~~~~~~``");
order = Orders.builder()
.orderId(UUID.randomUUID().toString())
.orderStatus("주문완료")
Expand All @@ -100,14 +101,16 @@ public void createOrder(String token, List<OrderReqDto> dtos){
String userId = jwtUtil.getAuthentication(token).getName();
System.out.println("User ID: " + userId);
member = memberService.getUserById(userId);
userIdx = member.getUserIdx();
order = Orders.builder()
.user(member)
.orderId(UUID.randomUUID().toString())
.orderStatus("주문완료")
.build();

}

Orders savedOrder = orderRepository.save(order);
System.out.println("~~~"+order.getOrderIdx());

List<OrderDetail> orderDetails = dtos.stream()
.map(dto -> {
Expand All @@ -125,12 +128,11 @@ public void createOrder(String token, List<OrderReqDto> dtos){

})
.collect(Collectors.toList());
System.out.println("~~~~~~~~"+orderDetails.size());
for(OrderDetail orderDetail : orderDetails){
System.out.println(orderDetail.getOrders());
System.out.println(orderDetail.getMenu());
System.out.println(orderDetail.getMenuTemperature());
System.out.println(orderDetail.getOrderDetailIdx());

if(userIdx != -1) {
int tot = calculateTotalPrice(orderDetails);
int point = (int) Math.floor(tot / 100.0);
memberService.updateUserPoint(userIdx, point);
}

orderDetailRepository.saveAll(orderDetails);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/hanaro/starbucks/util/Constant.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.hanaro.starbucks.util;

public class Constant {
public static final String GRANDE_SIZE="grande";
public static final String VENTI_SIZE="venti";
public static final String GRANDE_SIZE="Grande";
public static final String VENTI_SIZE="Venti";
public static final int GRANDE = 500;
public static final int VENTI = 1000;

Expand Down

0 comments on commit 89c5bab

Please sign in to comment.