-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCloseAuctionServlet.java
More file actions
102 lines (82 loc) · 3.28 KB
/
CloseAuctionServlet.java
File metadata and controls
102 lines (82 loc) · 3.28 KB
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
package com.seller;
import java.io.IOException;
import java.sql.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/closeAuction")
public class CloseAuctionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
int auctionId = Integer.parseInt(request.getParameter("auctionId"));
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/login_app", "root", "Pallavi@1000");
PreparedStatement aStmt = conn.prepareStatement(
"SELECT minPrice, status FROM T_Auction_isAuctioned WHERE auction_id = ?"
);
aStmt.setInt(1, auctionId);
ResultSet aRs = aStmt.executeQuery();
if (!aRs.next()) {
response.getWriter().println("Auction not found.");
return;
}
double minPrice = aRs.getDouble("minPrice");
String status = aRs.getString("status");
if (!"active".equalsIgnoreCase(status)) {
response.sendRedirect("sellerAuctionList.jsp");
return;
}
PreparedStatement bStmt = conn.prepareStatement(
"SELECT user_id, bid_amount " +
"FROM T_BidsOn_Bid_ItemBid " +
"WHERE auction_id = ? " +
"ORDER BY bid_amount DESC LIMIT 1"
);
bStmt.setInt(1, auctionId);
ResultSet bRs = bStmt.executeQuery();
Integer winnerId = null;
Double highestBid = null;
if (bRs.next()) {
winnerId = bRs.getInt("user_id");
highestBid = bRs.getDouble("bid_amount");
}
String finalStatus;
if (winnerId == null) {
finalStatus = "expired";
} else if (highestBid < minPrice) {
finalStatus = "failed";
winnerId = null;
} else {
finalStatus = "sold";
}
PreparedStatement update = conn.prepareStatement(
"UPDATE T_Auction_isAuctioned " +
"SET status = ?, winner_id = ? " +
"WHERE auction_id = ?"
);
update.setString(1, finalStatus);
if (winnerId == null) {
update.setNull(2, Types.INTEGER);
} else {
update.setInt(2, winnerId);
}
update.setInt(3, auctionId);
update.executeUpdate();
if ("sold".equals(finalStatus) && winnerId != null) {
PreparedStatement notify = conn.prepareStatement(
"INSERT INTO alerts (user_id, message) VALUES (?, ?)"
);
notify.setInt(1, winnerId);
notify.setString(2,
"Congratulations! You won auction #" + auctionId +
" with a bid of $" + highestBid + "!");
notify.executeUpdate();
}
response.sendRedirect("sellerAuctionList.jsp");
} catch (Exception e) {
e.printStackTrace(response.getWriter());
}
}
}