MenuItemDao, DatabaseConnection and MenuItem completed.
This commit is contained in:
@@ -1,23 +1,60 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.MenuItem;
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MenuItemDao {
|
public class MenuItemDao {
|
||||||
|
|
||||||
public List<MenuItem> findAll() {
|
public List<MenuItem> findAll() {
|
||||||
|
List<MenuItem> items = new ArrayList<>();
|
||||||
|
String sql = "select * FROM menu_item";
|
||||||
|
|
||||||
// TODO:
|
try (Connection connection = DatabaseConnection.getConnection();
|
||||||
// Retrieve all menu items
|
Statement statement = connection.createStatement();
|
||||||
|
ResultSet resultSet = statement.executeQuery(sql)
|
||||||
|
) {
|
||||||
|
while (resultSet.next()) {
|
||||||
|
MenuItem item = new MenuItem(
|
||||||
|
resultSet.getInt("id"),
|
||||||
|
resultSet.getString("name"),
|
||||||
|
resultSet.getString("description"),
|
||||||
|
resultSet.getDouble("price"),
|
||||||
|
resultSet.getString("category")
|
||||||
|
);
|
||||||
|
items.add(item);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error fetching menu items: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
public MenuItem findById(int id) {
|
||||||
|
String sql = "SELECT * FROM menu_items WHERE id = ?";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Find menu item by id
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
pstmt.setInt(1, id);
|
||||||
|
try (ResultSet rs = pstmt.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return new MenuItem(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getString("name"),
|
||||||
|
rs.getString("description"),
|
||||||
|
rs.getDouble("price"),
|
||||||
|
rs.getString("category")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error finding menu item: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package dev.database;
|
package dev.database;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class DatabaseConnection {
|
public class DatabaseConnection {
|
||||||
@@ -17,11 +18,7 @@ public class DatabaseConnection {
|
|||||||
|
|
||||||
public static Connection getConnection()
|
public static Connection getConnection()
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
|
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||||
// TODO:
|
|
||||||
// Return a valid PostgreSQL connection
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,11 @@ public class MenuItem {
|
|||||||
|
|
||||||
private String category;
|
private String category;
|
||||||
|
|
||||||
|
public MenuItem(int id, String name, String description, double price, String category) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.price = price;
|
||||||
|
this.category = category;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user