Skip to content

Commit

Permalink
상품주문, 목록검색, 주문취소
Browse files Browse the repository at this point in the history
  • Loading branch information
kimgwanghoon committed Jun 10, 2021
1 parent 579c230 commit 3ca038d
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/main/java/com/bookstore/shop/controller/Itemcontroller.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,10 @@ public String updateItemForm(@PathVariable("itemId") Long itemId, Model model){
}

@PostMapping("/items/{itemId}/edit")
public String updateItem(@ModelAttribute("form") BookForm form){
public String updateItem(@PathVariable Long itemId,@ModelAttribute("form") BookForm form){

Book book = new Book();
book.setId(form.getId());
book.setName(form.getName());
book.setPrice(form.getPrice());
book.setStockQuantity(form.getStockQuantity());
book.setAuthor(form.getAuthor());
book.setIsbn(form.getIsbn());
itemService.updateItem(itemId, form.getName(),form.getPrice(),form.getStockQuantity());

itemService.saveItem(book);
return "redirect:/items";
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/bookstore/shop/controller/OrderController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.bookstore.shop.controller;

import com.bookstore.shop.domain.Member;
import com.bookstore.shop.domain.Order;
import com.bookstore.shop.domain.item.Item;
import com.bookstore.shop.repository.OrderSearch;
import com.bookstore.shop.service.ItemService;
import com.bookstore.shop.service.MemberService;
import com.bookstore.shop.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequiredArgsConstructor
public class OrderController {

private final OrderService orderService;
private final MemberService memberService;
private final ItemService itemService;

@GetMapping("/order")
public String createForm(Model model){
List<Member> members = memberService.findMembers();
List<Item> items = itemService.findItems();

model.addAttribute("members", members);
model.addAttribute("items", items);

return "order/orderForm";
}

@PostMapping("/order")
public String order(@RequestParam("memberId") Long memberId, @RequestParam("itemId") Long itemId, @RequestParam("count") int count){
orderService.order(memberId,itemId,count);
return "redirect:/orders";
}

@GetMapping("/orders")
public String orderList(@ModelAttribute("orderSearch")OrderSearch orderSearch, Model model){
List<Order> orders = orderService.findOrders(orderSearch);
model.addAttribute("orders", orders);

return "order/orderList";
}

@PostMapping("/orders/{orderId}/cancel")
public String cancelOrder(@PathVariable("orderId") Long orderId){
orderService.cancelOrder(orderId);
System.out.println("dddddddddddddd");
return "redirect:/orders";
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/bookstore/shop/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public void saveItem(Item item){
itemRepository.save(item);
}

@Transactional
public void updateItem(Long itemId, String name, int price, int stockQuantity){

Item findId = itemRepository.findId(itemId);
findId.setPrice(price);
findId.setName(name);
findId.setStockQuantity(stockQuantity);

}

public Item findId(Long itemId){
return itemRepository.findId(itemId);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/bookstore/shop/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import com.bookstore.shop.repository.ItemRepository;
import com.bookstore.shop.repository.MemberRepository;
import com.bookstore.shop.repository.OrderRepository;
import com.bookstore.shop.repository.OrderSearch;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand Down Expand Up @@ -48,5 +51,10 @@ public void cancelOrder(Long orderId){
order.cancel();
}

//검색
public List<Order> findOrders(OrderSearch orderSearch){
return orderRepository.findAll(orderSearch);
}


}
36 changes: 36 additions & 0 deletions src/main/resources/templates/order/orderForm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="fragments/header :: header" />
<body>
<div class="container">
<div th:replace="fragments/bodyHeader :: bodyHeader"/>
<form role="form" action="/order" method="post">
<div class="form-group">
<label for="member">주문회원</label>
<select name="memberId" id="member" class="form-control">
<option value="">회원선택</option>
<option th:each="member : ${members}"
th:value="${member.id}"
th:text="${member.name}" />
</select>
</div>
<div class="form-group">
<label for="item">상품명</label>
<select name="itemId" id="item" class="form-control">
<option value="">상품선택</option>
<option th:each="item : ${items}"
th:value="${item.id}"
th:text="${item.name}" />
</select>
</div>
<div class="form-group">
<label for="count">주문수량</label>
<input type="number" name="count" class="form-control" id="count"placeholder="주문 수량을 입력하세요">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<br/>
<div th:replace="fragments/footer :: footer" />
</div>
</body>
</html>
66 changes: 66 additions & 0 deletions src/main/resources/templates/order/orderList.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="fragments/header :: header"/>
<body>
<div class="container">
<div th:replace="fragments/bodyHeader :: bodyHeader"/>
<div>
<div>
<form th:object="${orderSearch}" class="form-inline">
<div class="form-group mb-2">
<input type="text" th:field="*{memberName}" class="formcontrol" placeholder="회원명"/>
</div>
<div class="form-group mx-sm-1 mb-2">
<select th:field="*{orderStatus}" class="form-control">
<option value="">주문상태</option>
<option th:each="status : ${T(com.bookstore.shop.domain.status.OrderStatus).values()}"
th:value="${status}"
th:text="${status}">option
</option>
</select>
</div>
<button type="submit" class="btn btn-primary mb-2">검색</button>
</form>
</div>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>회원명</th>
<th>대표상품 이름</th>
<th>대표상품 주문가격</th> <th>대표상품 주문수량</th>
<th>상태</th>
<th>일시</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="item : ${orders}">
<td th:text="${item.id}"></td>
<td th:text="${item.member.name}"></td>
<td th:text="${item.orderItems[0].item.name}"></td>
<td th:text="${item.orderItems[0].orderPrice}"></td>
<td th:text="${item.orderItems[0].count}"></td>
<td th:text="${item.status}"></td>
<td th:text="${item.orderDate}"></td>
<td>
<a th:if="${item.status.name() == 'ORDER'}" href="#"
th:href="'javascript:cancel('+${item.id}+')'"
class="btn btn-danger">CANCEL</a>
</td>
</tr>
</tbody>
</table>
</div>
<div th:replace="fragments/footer :: footer"/>
</div>
</body>
<script>
function cancel(id) {
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "/orders/" + id + "/cancel");
document.body.appendChild(form);
form.submit();
}</script>
</html>

0 comments on commit 3ca038d

Please sign in to comment.