false
This commit is contained in:
@@ -1,25 +1,56 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDao {
|
||||
|
||||
public int save(Order order) {
|
||||
String sql = "INSERT INTO orders (user_id, total_price) VALUES (?, ?) RETURNING id";
|
||||
|
||||
// TODO:
|
||||
// Insert order and return generated id
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
|
||||
pstmt.setInt(1, order.getUserId());
|
||||
pstmt.setDouble(2, order.getTotalPrice());
|
||||
|
||||
try (ResultSet rs = pstmt.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error saving order: " + e.getMessage());
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
List<Order> orders = new ArrayList<>();
|
||||
String sql = "SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC";
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement pstmt = conn.prepareStatement(sql)){
|
||||
|
||||
return null;
|
||||
pstmt.setInt(1, userId);
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
|
||||
while (rs.next()){
|
||||
Order order = new Order(
|
||||
rs.getInt("id"),
|
||||
rs.getInt("user_id"),
|
||||
rs.getTimestamp("created_at").toLocalDateTime(),
|
||||
rs.getDouble("total_price")
|
||||
);
|
||||
orders.add(order);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Error fetching order history: " + e.getMessage());
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user