Compare commits
5
Commits
main
...
7ed7206372
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ed7206372 | ||
|
|
17ae2dfeaf | ||
|
|
1564167420 | ||
|
|
55763a1c46 | ||
|
|
a58476f109 |
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,67 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDao {
|
||||
|
||||
public int save(Order order) {
|
||||
|
||||
// TODO:
|
||||
// Insert order and return generated id
|
||||
String sql = "INSERT INTO orders (user_id, total_price, created_at) VALUES(?, ?, ?) RETURNING id";
|
||||
|
||||
try (Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql)){
|
||||
|
||||
preparedStatement.setInt(1, order.getUserId());
|
||||
preparedStatement.setDouble(2, order.getTotalPrice());
|
||||
preparedStatement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()));
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
if(resultSet.next()) return resultSet.getInt("id");
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
String sql = "SELECT * FROM orders WHERE user_id=?";
|
||||
List<Order> orders = new ArrayList<>();
|
||||
|
||||
return null;
|
||||
try(Connection connection = DatabaseConnection.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql) ) {
|
||||
|
||||
preparedStatement.setInt(1, userId);
|
||||
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
while (resultSet.next()) {
|
||||
|
||||
Order order = new Order();
|
||||
|
||||
order.setId(resultSet.getInt("id"));
|
||||
order.setUserId(resultSet.getInt("user_id"));
|
||||
order.setCreatedAt(resultSet.getTimestamp("created_at").toLocalDateTime());
|
||||
order.setTotalPrice(resultSet.getDouble("total_price"));
|
||||
|
||||
orders.add(order);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return orders;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,4 +12,9 @@ public class MenuItem {
|
||||
|
||||
private String category;
|
||||
|
||||
public void setId(int id) { this.id = id; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
public void setPrice(double price) { this.price = price; }
|
||||
public void setCategory(String category) { this.category = category; }
|
||||
}
|
||||
@@ -12,4 +12,13 @@ public class Order {
|
||||
|
||||
private double totalPrice;
|
||||
|
||||
public void setId(int id) { this.id = id; }
|
||||
public void setUserId(int userId) { this.userId = userId; }
|
||||
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
|
||||
public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; }
|
||||
|
||||
public int getId() { return id; }
|
||||
public double getTotalPrice() { return totalPrice; }
|
||||
public int getUserId() { return userId; }
|
||||
public LocalDateTime getCreatedAt() { return createdAt; }
|
||||
}
|
||||
@@ -12,4 +12,17 @@ public class OrderDetail {
|
||||
|
||||
private double price;
|
||||
|
||||
public void setId(int id) { this.id = id; }
|
||||
public void setOrderId(int orderId) { this.orderId = orderId; }
|
||||
public void setMenuItemId(int menuItemId) { this.menuItemId = menuItemId; }
|
||||
public void setQuantity(int quantity) { this.quantity = quantity; }
|
||||
public void setPrice(double price) { this.price = price; }
|
||||
|
||||
|
||||
public int getId() { return id; }
|
||||
public int getOrderId() { return orderId; }
|
||||
public int getMenuItemId() { return menuItemId; }
|
||||
public int getQuantity() { return quantity; }
|
||||
public double getPrice() { return price; }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user