implement MenuiItemDao.java
This commit is contained in:
@@ -1,23 +1,74 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.MenuItem;
|
import dev.model.MenuItem;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
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() {
|
||||||
|
|
||||||
// TODO:
|
List<MenuItem> items = new ArrayList<>();
|
||||||
// Retrieve all menu items
|
|
||||||
|
|
||||||
return null;
|
// Retrieve all menu items
|
||||||
|
try ( Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM menu_items");
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery()) {
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
|
||||||
|
MenuItem menuItem = new MenuItem();
|
||||||
|
|
||||||
|
menuItem.setId(resultSet.getInt("id"));
|
||||||
|
menuItem.setName(resultSet.getString("name"));
|
||||||
|
menuItem.setDescription(resultSet.getString("description"));
|
||||||
|
menuItem.setPrice(resultSet.getDouble("price"));
|
||||||
|
menuItem.setCategory(resultSet.getString("category"));
|
||||||
|
|
||||||
|
items.add(menuItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MenuItem findById(int id) {
|
public MenuItem findById(int id) {
|
||||||
|
|
||||||
// TODO:
|
|
||||||
// Find menu item by id
|
// Find menu item by id
|
||||||
|
String sql = "SELECT * FROM menu_items WHERE id=?";
|
||||||
|
|
||||||
|
try (Connection connection = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, id);
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
if(resultSet.next()) {
|
||||||
|
|
||||||
|
MenuItem menuItem = new MenuItem();
|
||||||
|
|
||||||
|
menuItem.setId(resultSet.getInt("id"));
|
||||||
|
menuItem.setName(resultSet.getString("name"));
|
||||||
|
menuItem.setDescription(resultSet.getString("description"));
|
||||||
|
menuItem.setPrice(resultSet.getDouble("price"));
|
||||||
|
menuItem.setCategory(resultSet.getString("category"));
|
||||||
|
|
||||||
|
return menuItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user