2024-12-21 13:54:25 +08:00
|
|
|
package example.controller;
|
|
|
|
|
|
|
|
import example.model.Computer;
|
2024-12-21 16:04:38 +08:00
|
|
|
import example.model.User;
|
2024-12-21 13:54:25 +08:00
|
|
|
import example.service.ICartService;
|
|
|
|
import example.service.impl.CartServiceImpl;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@WebServlet("/cart")
|
|
|
|
public class CartServlet extends HttpServlet {
|
|
|
|
|
|
|
|
ICartService cartService = new CartServiceImpl();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
|
String action = req.getParameter("action");
|
|
|
|
switch (action){
|
|
|
|
case "list":
|
|
|
|
try {
|
|
|
|
toCart(req, resp);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "add":
|
2024-12-21 16:04:38 +08:00
|
|
|
try {
|
|
|
|
addCart(req,resp);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2024-12-21 13:54:25 +08:00
|
|
|
break;
|
|
|
|
case "remove":
|
2024-12-21 16:41:21 +08:00
|
|
|
try {
|
|
|
|
removeFromCart(req, resp);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
2024-12-21 13:54:25 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
req.getRequestDispatcher("/WEB-INF/jsp/cart.jsp").forward(req,resp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-21 16:41:21 +08:00
|
|
|
private void removeFromCart(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
|
|
|
User user = (User) req.getSession().getAttribute("user");
|
|
|
|
String id = req.getParameter("id");
|
|
|
|
cartService.removeFromCart(Integer.parseInt(id),user.getId());
|
|
|
|
try {
|
|
|
|
toCart(req, resp);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-21 16:04:38 +08:00
|
|
|
private void addCart(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
|
|
|
String id = req.getParameter("id");
|
|
|
|
User user = (User) req.getSession().getAttribute("user");
|
|
|
|
cartService.addCart(Integer.parseInt(id),user.getId());
|
|
|
|
req.setAttribute("msg","加购成功");
|
|
|
|
req.getRequestDispatcher("/computerList").forward(req,resp);
|
|
|
|
}
|
|
|
|
|
2024-12-21 13:54:25 +08:00
|
|
|
private void toCart(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
2024-12-21 16:04:38 +08:00
|
|
|
User user = (User) req.getSession().getAttribute("user");
|
|
|
|
List<Computer> computers = cartService.myCart(user.getId());
|
2024-12-21 13:54:25 +08:00
|
|
|
req.setAttribute("computers",computers);
|
|
|
|
req.getRequestDispatcher("cart.jsp").forward(req,resp);
|
|
|
|
}
|
|
|
|
}
|