Skip to content

Commit

Permalink
Yv3 #1/order and fund (#20)
Browse files Browse the repository at this point in the history
* feat[v2#2]: Item funding quantity 구현 중

* chore[v2#2]: MemberCreateRequest 이름 수정

* feat[v2#2]: funding 구현 중

* order, item관계 설정

* item, order 관계 설정
  • Loading branch information
yujin9747 authored Jan 27, 2023
1 parent 50e19ca commit 94c250c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public ResponseEntity<Orders> createOrder(@RequestBody OrderCreationRequest requ
return ResponseEntity.ok(orderService.saveOrder(request.getAddress(), request.getSize(), request.getCount(), request.getPhoneNumber(), request.getMemberId(), request.getItemId()));
}

/**
* 주문 승인
* */
@PostMapping("/order")
public ResponseEntity<Orders> approveOrder(@RequestBody OrderCreationRequest request){
return ResponseEntity.ok(orderService.approveOrder(request.getItemId()));
}

/**
* 펀딩 전체 조회
* */
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/noriton/team9/domain/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@Entity
Expand All @@ -23,15 +26,21 @@ public class Item {

private int stockQuantity;

private int count;

@OneToMany(mappedBy = "item")
private List<Orders> fundingList = new ArrayList<>();
public void removeStockQuantity(int count) {
int resStock = this.stockQuantity - count;
if(resStock < 0){
throw new IllegalStateException("펀딩 한계치를 초과했습니다.");
}
this.count += count;
this.stockQuantity -= count;
}

public void addStockQuantity(int count) {
this.count -= count;
this.stockQuantity += count;
}
}
32 changes: 30 additions & 2 deletions src/main/java/com/noriton/team9/domain/Orders.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.List;

@Entity
@Getter
Expand All @@ -26,7 +27,7 @@ public class Orders {

private String size;

@OneToOne
@ManyToOne
@JoinColumn(name = "item_id")
private Item item;
private LocalDateTime orderDate;
Expand All @@ -42,20 +43,47 @@ public class Orders {
private String fundStatus;

//==생성 메서드==//
// 펀딩 생성
public static Orders createOrder(int count, String address, String size, String phoneNumber, Member member, Item item, String fundStatus){
Orders order = new Orders();
order.setCount(count);
order.setAddress(address);
order.setSize(size);
order.setPhoneNumber(phoneNumber);
order.setMember(member);
order.setItem(item);
order.setItemRelation(item);
order.setFundStatus(fundStatus);
order.setOrderDate(LocalDateTime.now());
order.setTotalPrice(count * item.getPrice());
return order;
}

private void setItemRelation(Item item) {
this.item = item;
item.getFundingList().add(this);
}

public static Orders approveOrder(Item item, String fundStatus) {
Orders order = new Orders();
order.setFundStatus(fundStatus); // approved
order.setOrderDate(LocalDateTime.now());
int totalCount=0, totalPrice=0;
List<Orders> fundingList = item.getFundingList();
for(int i=0; i<fundingList.size(); i++){
totalCount += fundingList.get(i).count;
totalPrice += fundingList.get(i).totalPrice;
fundingList.get(i).setFundStatus(fundStatus);
}
order.setCount(totalCount);
order.setTotalPrice(totalPrice);
order.setApprovedItem(item);
}

// approved된 order는 Item의 fundingList에 하위로 들어가지 않는다. 단방향 관계.
private void setApprovedItem(Item item) {
this.item = item;
}

public void setMember(Member member){
this.member = member;
member.getOrdersList().add(this);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/noriton/team9/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public Item createItem(ItemCreationRequest item) {
}
Item itemToCreate = new Item();
BeanUtils.copyProperties(item, itemToCreate);
itemToCreate.setCount(0);
itemToCreate.setSample(sample.get());
return itemRepository.save(itemToCreate);
}
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/noriton/team9/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,30 @@ public void deleteOrder(Long orderId){
}

/**
* 펀딩 조회 - 상품별
* 펀딩 조회 - 상품별 : 관리자 페이지에서 조회
* */
public List<Orders> findOrdersByItem(Long itemId) {
return orderRepository.findByItem(itemId);
}

/**
* 펀딩 조회 - 멤버별 : 마이페이지에서 내가 펀딩한 상품 보기
* */
// public List<Orders> findOrdersByMember(Long memberId) { return orderRepository.findByMember(memberId);}


/**
* 주문 승인
* */
@Transactional
public Orders approveOrder(Long itemId) {
Optional<Item> optionalItem = itemRepository.findById(itemId);
Item item = optionalItem.get();

Orders order = Orders.approveOrder(item, "APPROVED");
return orderRepository.save(order);
}

/**
* 주문 조회 by id
* */
Expand Down

0 comments on commit 94c250c

Please sign in to comment.