-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathOrderController.java
200 lines (182 loc) · 6.77 KB
/
OrderController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package me.anant.PMS.controller;
import java.awt.Dialog.ModalExclusionType;
import java.security.Principal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import me.anant.PMS.exceptions.OrderNotFoundException;
import me.anant.PMS.exceptions.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import me.anant.PMS.model.Order;
import me.anant.PMS.model.OrderProduct;
import me.anant.PMS.model.Product;
import me.anant.PMS.model.User;
import me.anant.PMS.service.EmailService;
import me.anant.PMS.service.OrderService;
import me.anant.PMS.service.ProductService;
import me.anant.PMS.service.UserService;
/**
* This Order Controller has rest endpoints which are responsible to perform following functionalities :
* <ul>
* <li>Place an order</li>
* <li>View Order</li>
* <li>View All Orders - Used By Admin</li>
* <li>View Order Details Of a particular Customer</li>
* <li>Change Order Status</li>
* <li>Cancel an Order</li>
* </ul>
*
*/
@Controller
public class OrderController {
@Autowired
ProductService productService;
@Autowired
UserService userService;
@Autowired
OrderService orderService;
@Autowired
EmailService emailService;
/**
* This GET api is responsible to view the home screen for Place Orders
* @return ModelAndView
*/
@GetMapping("/customer/order_place")
public ModelAndView customerHome() {
List<Product> pList = productService.get();
ModelAndView modelAndView = new ModelAndView("customer/home");
modelAndView.addObject("pList", pList);
return modelAndView;
}
/**
* This POST api is responsible to place an Order
* @param request
* @param principal
* @return ModelAndView
*/
@PostMapping("/customer/order_place")
public ModelAndView orderPlace(HttpServletRequest request, Principal principal) throws ProductNotFoundException{
String[] pIds = request.getParameterValues("productId");
Set<OrderProduct> opList = new HashSet<>();
for(String pId: pIds) {
long pid = Long.parseLong(pId);
Product product = productService.findById(pid);
int buyqty = Integer.parseInt(request.getParameter(pId));
opList.add(new OrderProduct(product, buyqty));
productService.deductQty(pid, buyqty);
}
User user = userService.findByEmail(principal.getName());
orderService.save(new Order(user, "PROCESSING", opList));
String message = "Hello,<br><br>Your order has been placed successfuly. Following is the detail of your order.<br><br>"
+ "<table>" +
"<tr>" +
"<th>Name</th>" +
"<th>Price</th>" +
"<th>Qty</th>" +
"<th>Amount</th>" +
"</tr>";
float sum = 0;
for (OrderProduct op : opList)
{
sum = sum + op.getProduct().getProductPrice() * op.getBuyqty();
message = message + "<tr>" +
"<td>"+op.getProduct().getProductName()+"</td>" +
"<td>Rs. "+op.getProduct().getProductPrice()+"</td>" +
"<td>"+op.getBuyqty()+"</td>" +
"<td>Rs. "+op.getProduct().getProductPrice() * op.getBuyqty()+"</td>" +
"</tr>";
}
message = message + "<tr><td colspan=\"3\"><center><b>Total Amount</b></center></td><td>Rs. "+sum+"</td></tr>" +
"</table>";
emailService.send(principal.getName(), "Order Placed successfully", message);
ModelAndView modelAndView = new ModelAndView("customer/order_place");
modelAndView.addObject("opList", opList);
return modelAndView;
}
/**
* This Get api is responsible to View Customer's Order.
*
* @param principal
* @return ModelAndView
*/
@GetMapping("customer/order/list")
public ModelAndView viewMyOrder(Principal principal) {
User user = userService.findByEmail(principal.getName());
ModelAndView modelAndView = new ModelAndView("customer/order/list");
modelAndView.addObject("orderList", user.getOrders());
return modelAndView;
}
/**
* This GET api is responsible to List All Orders.
* @return ModelAnndView
*/
@GetMapping("admin/order/list")
public ModelAndView viewAllOrder() {
List<Order> oList = orderService.get();
ModelAndView modelAndView = new ModelAndView("admin/order/list");
modelAndView.addObject("oList", oList);
return modelAndView;
}
/**
* This GET api is responsible to view the details of an Order.
* This feature is used by Admin Module.
* @param id
* @return ModelAndView
*/
@GetMapping("admin/order/detail")
public ModelAndView orderDetailAdmin(@RequestParam("id") long id) throws ProductNotFoundException {
ModelAndView modelAndView = new ModelAndView("admin/order/detail");
modelAndView.addObject("order", orderService.findById(id).get());
return modelAndView;
}
/**
* This GET api is responsible to view the details of an order by passing Order Id.
* This api is used by Customer Module.
* @param id
* @return ModelAndView
*/
@GetMapping("customer/order/detail")
public ModelAndView orderDetailCustomer(@RequestParam("id") long id) throws ProductNotFoundException{
ModelAndView modelAndView = new ModelAndView("customer/order/detail");
modelAndView.addObject("order", orderService.findById(id).get());
return modelAndView;
}
/**
* This GET api is responsible to cancel an Order.
* @param id
* @param redirectAttributes
* @return String redirect path
*/
@GetMapping("customer/order/cancel")
public String orderCancel(@RequestParam("id") long id, final RedirectAttributes redirectAttributes) throws ProductNotFoundException {
if(orderService.cancelOrder(id)) {
redirectAttributes.addFlashAttribute("msg", "Order cancelled successfully");
redirectAttributes.addFlashAttribute("class", "alert-success");
} else {
redirectAttributes.addFlashAttribute("msg", "Order not cancelled, since you can cencel PROCESSING or CONFIRMED order with 24 hours only.");
redirectAttributes.addFlashAttribute("class", "alert-danger");
}
return "redirect:/customer/order/list";
}
/**
* This POST api changeStatus is responsible to change the status of an Order.
* @param id
* @param status
* @param redirectAttributes
* @return String redirect path
*/
@PostMapping("admin/order/status")
public String changeStatus(@RequestParam("id") long id, @RequestParam("status") String status, final RedirectAttributes redirectAttributes) throws OrderNotFoundException, ProductNotFoundException {
orderService.changeStatus(id, status);
redirectAttributes.addFlashAttribute("msg", "Status of order changed.");
redirectAttributes.addFlashAttribute("class", "alert-success");
return "redirect:/admin/order/list";
}
}