Develop #1
@@ -1,23 +1,74 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
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;
|
||||
|
||||
public class MenuItemDao {
|
||||
|
||||
public List<MenuItem> findAll() {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all menu items
|
||||
List<MenuItem> items = new ArrayList<>();
|
||||
|
||||
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) {
|
||||
|
||||
// TODO:
|
||||
// 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user