2024-12-21 14:30:26 +08:00
|
|
|
package example.controller;
|
|
|
|
|
2024-12-21 17:03:33 +08:00
|
|
|
import example.model.Computer;
|
|
|
|
import example.model.User;
|
|
|
|
import example.service.ICartService;
|
2024-12-21 14:30:26 +08:00
|
|
|
import example.service.IOrdersService;
|
2024-12-21 17:03:33 +08:00
|
|
|
import example.service.impl.CartServiceImpl;
|
2024-12-21 14:30:26 +08:00
|
|
|
import example.service.impl.OrdersServiceImpl;
|
|
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.IOException;
|
2024-12-21 17:03:33 +08:00
|
|
|
import java.util.List;
|
2024-12-21 14:30:26 +08:00
|
|
|
|
|
|
|
@WebServlet("/orders")
|
|
|
|
public class OrdersServlet extends HttpServlet {
|
|
|
|
|
|
|
|
IOrdersService ordersService=new OrdersServiceImpl();
|
|
|
|
|
2024-12-21 17:03:33 +08:00
|
|
|
ICartService cartService=new CartServiceImpl();
|
|
|
|
|
2024-12-21 14:30:26 +08:00
|
|
|
@Override
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
String action = req.getParameter("action");
|
|
|
|
switch (action) {
|
|
|
|
case "all":
|
|
|
|
ordersService.allOrder(req, resp);
|
|
|
|
break;
|
2024-12-21 17:09:41 +08:00
|
|
|
case "my":
|
|
|
|
ordersService.myOrder(req, resp);
|
|
|
|
break;
|
2024-12-21 16:41:21 +08:00
|
|
|
case "add":
|
|
|
|
try {
|
|
|
|
ordersService.checkOrder(req, resp);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
break;
|
2024-12-21 14:30:26 +08:00
|
|
|
case "delete":
|
|
|
|
// ordersService.delete(req, resp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-12-21 17:03:33 +08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
req.setCharacterEncoding("utf-8");
|
|
|
|
double price = Double.parseDouble(req.getParameter("price"));
|
|
|
|
String address = req.getParameter("address");
|
|
|
|
String remark = req.getParameter("remark");
|
|
|
|
User user = (User) req.getSession().getAttribute("user");
|
|
|
|
int flag=ordersService.addOrder(user.getId(),price, address, remark);
|
|
|
|
// 移出购物车所有信息
|
|
|
|
try {
|
|
|
|
List<Computer> computers = cartService.myCart(user.getId());
|
|
|
|
for (Computer computer : computers) {
|
|
|
|
cartService.removeFromCart(user.getId(), computer.getId());
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
resp.sendRedirect("/");
|
|
|
|
}
|
2024-12-21 14:30:26 +08:00
|
|
|
}
|