add<下单操作>

master
liyansheng 2024-12-21 17:03:33 +08:00
parent 1a225dff58
commit 68304201e5
7 changed files with 54 additions and 9 deletions

View File

@ -1,6 +1,10 @@
package example.controller;
import example.model.Computer;
import example.model.User;
import example.service.ICartService;
import example.service.IOrdersService;
import example.service.impl.CartServiceImpl;
import example.service.impl.OrdersServiceImpl;
import javax.servlet.ServletException;
@ -9,12 +13,15 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/orders")
public class OrdersServlet extends HttpServlet {
IOrdersService ordersService=new OrdersServiceImpl();
ICartService cartService=new CartServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action");
@ -35,4 +42,24 @@ public class OrdersServlet extends HttpServlet {
}
}
@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("/");
}
}

View File

@ -5,6 +5,7 @@ import example.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.time.LocalDateTime;
import java.util.List;
public class OrdersDao {
@ -26,4 +27,14 @@ public class OrdersDao {
OrdersDao ordersDao = new OrdersDao();
System.out.println(ordersDao.getAllOrders());
}
public int addOrder(int userId,double price, String address, String remark) {
String sql="insert into orders(user_id,order_date,total_price,address,remark) values(?,?,?,?,?)";
try {
return queryRunner.update(sql,userId, LocalDateTime.now(),price,address,remark);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}

View File

@ -9,5 +9,5 @@ public interface ICartService {
void addCart(int i, int id) throws Exception;
void removeFromCart(int i, int id) throws Exception;
void removeFromCart(int userId, int productId) throws Exception;
}

View File

@ -11,4 +11,6 @@ public interface IOrdersService {
void allOrder(HttpServletRequest req, HttpServletResponse resp);
void checkOrder(HttpServletRequest req, HttpServletResponse resp) throws Exception;
int addOrder(int userId,double price, String address, String remark);
}

View File

@ -21,7 +21,7 @@ public class CartServiceImpl implements ICartService {
}
@Override
public void removeFromCart(int i, int id) throws Exception {
cartDao.deleteCart(id, i);
public void removeFromCart(int userId, int productId) throws Exception {
cartDao.deleteCart(userId, productId);
}
}

View File

@ -46,4 +46,9 @@ public class OrdersServiceImpl implements IOrdersService {
req.getRequestDispatcher("checkOrder.jsp").forward(req,resp);
}
@Override
public int addOrder(int userId,double price, String address, String remark) {
return ordersDao.addOrder(userId,price,address,remark);
}
}

View File

@ -10,12 +10,12 @@
<body>
<h1>订单确认</h1>
<form action="/orders" method="post">
<label for="name">总金额: </label><br>
<input type="text" id="name" name="name" value="${order.totalPrice}" disabled><br><br>
<label for="price">地址: </label><br>
<input type="text" id="price" name="price" value="${order.address}" required><br><br>
<label for="stock">商品信息: </label><br>
<textarea id="stock" name="stock" rows="15" cols="30" disabled>${order.remark}</textarea><br><br>
<label for="price">总金额: </label><br>
<input type="text" id="price" name="price" value="${order.totalPrice}" readonly><br><br>
<label for="address">地址: </label><br>
<input type="text" id="address" name="address" value="${order.address}" required><br><br>
<label for="remark">商品信息: </label><br>
<textarea id="remark" name="remark" rows="15" cols="30" readonly>${order.remark}</textarea><br><br>
<input type="submit" value="我已确认">
</form>
<br>