Complete OrderDetail and OrderDetailDao
This commit is contained in:
@@ -1,24 +1,74 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDetailDao {
|
||||
|
||||
public void save(OrderDetail detail) {
|
||||
|
||||
// TODO:
|
||||
// Insert order detail
|
||||
String sql = "INSERT INTO order_details (order_id, menu_item_id, quantity, price) VALUES (?, ?, ?, ?)";
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)
|
||||
) {
|
||||
|
||||
statement.setInt(1, detail.getOrderId());
|
||||
statement.setInt(2, detail.getMenuItemId());
|
||||
statement.setInt(3, detail.getQuantity());
|
||||
statement.setDouble(4, detail.getPrice());
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
System.out.println("Error saving order detail: " + e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
String sql = "SELECT id, order_id, menu_item_id, quantity, price FROM order_details WHERE order_id = ?";
|
||||
|
||||
return null;
|
||||
List<OrderDetail> details = new ArrayList<>();
|
||||
|
||||
try (
|
||||
Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)
|
||||
) {
|
||||
|
||||
statement.setInt(1, orderId);
|
||||
|
||||
ResultSet resultSet = statement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
|
||||
OrderDetail detail = new OrderDetail();
|
||||
|
||||
detail.setId(resultSet.getInt("id"));
|
||||
detail.setOrderId(resultSet.getInt("order_id"));
|
||||
detail.setMenuItemId(resultSet.getInt("menu_item_id"));
|
||||
detail.setQuantity(resultSet.getInt("quantity"));
|
||||
detail.setPrice(resultSet.getDouble("price"));
|
||||
|
||||
details.add(detail);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
|
||||
System.out.println("Error loading order details: " + e.getMessage());
|
||||
|
||||
}
|
||||
|
||||
return details;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,4 +12,43 @@ public class OrderDetail {
|
||||
|
||||
private double price;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(int orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public int getMenuItemId() {
|
||||
return menuItemId;
|
||||
}
|
||||
|
||||
public void setMenuItemId(int menuItemId) {
|
||||
this.menuItemId = menuItemId;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user