complete DAO (and model) package
This commit is contained in:
@@ -1,23 +1,65 @@
|
||||
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
|
||||
String sql = "SELECT id, name, price, category FROM menu_items";
|
||||
List<MenuItem> items = new ArrayList<>();
|
||||
|
||||
return null;
|
||||
try(Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
ResultSet rs = stmt.executeQuery())
|
||||
{
|
||||
while (rs.next())
|
||||
{
|
||||
MenuItem item = new MenuItem();
|
||||
item.setId(rs.getInt("id"));
|
||||
item.setName(rs.getString("name"));
|
||||
item.setPrice(rs.getDouble("price"));
|
||||
item.setCategory(rs.getString("category"));
|
||||
items.add(item);
|
||||
}
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
public MenuItem findById(int id) {
|
||||
|
||||
// TODO:
|
||||
// Find menu item by id
|
||||
String sql = "SELECT id, name, price, category FROM menu_items WHERE id = ?";
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||
{
|
||||
stmt.setInt(1, id);
|
||||
try (ResultSet rs = stmt.executeQuery())
|
||||
{
|
||||
if (rs.next())
|
||||
{
|
||||
MenuItem item = new MenuItem();
|
||||
item.setId(rs.getInt("id"));
|
||||
item.setName(rs.getString("name"));
|
||||
item.setPrice(rs.getDouble("price"));
|
||||
item.setCategory(rs.getString("category"));
|
||||
return item;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,76 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.Order;
|
||||
|
||||
import java.sql.*;
|
||||
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) VALUES (?, ?)";
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS))
|
||||
{
|
||||
stmt.setInt(1, order.getUserId());
|
||||
stmt.setDouble(2, order.getTotalPrice());
|
||||
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
if (affectedRows == 0)
|
||||
{
|
||||
throw new SQLException("unsuccessful");
|
||||
}
|
||||
|
||||
try (ResultSet generatedKeys = stmt.getGeneratedKeys())
|
||||
{
|
||||
if (generatedKeys.next())
|
||||
{
|
||||
return generatedKeys.getInt(1);
|
||||
} else
|
||||
{
|
||||
throw new SQLException("unsuccessful");
|
||||
}
|
||||
}
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
String sql = "SELECT id, user_id, orderDate, total_price FROM orders WHERE user_id = ? ORDER BY orderDate DESC";
|
||||
List<Order> orders = new ArrayList<>();
|
||||
|
||||
return null;
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||
{
|
||||
|
||||
stmt.setInt(1, userId);
|
||||
try (ResultSet rs = stmt.executeQuery())
|
||||
{
|
||||
while (rs.next())
|
||||
{
|
||||
Order order = new Order();
|
||||
order.setId(rs.getInt("id"));
|
||||
order.setUserId(rs.getInt("user_id"));
|
||||
order.setCreatedAt(rs.getTimestamp("order_date").toLocalDateTime());
|
||||
order.setTotalPrice(rs.getDouble("total_price"));
|
||||
orders.add(order);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return orders;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +1,73 @@
|
||||
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_items_id, quantity, price_at_purchase) VALUES (?,?,?,?)";
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||
{
|
||||
stmt.setInt(2, detail.getOrderId());
|
||||
stmt.setInt(3, detail.getMenuItemId());
|
||||
stmt.setInt(4, detail.getQuantity());
|
||||
stmt.setDouble(5, detail.getPrice());
|
||||
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
if (affectedRows == 0)
|
||||
{
|
||||
throw new SQLException("unsuccessful");
|
||||
}
|
||||
|
||||
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
String sql = "SELECT id, order_id, menu_items_id, quantity, price_at_purchase FROM order_details WHERE order_id = ?";
|
||||
|
||||
return null;
|
||||
List<OrderDetail> orderDetails = new ArrayList<>();
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||
{
|
||||
|
||||
stmt.setInt(1, orderId);
|
||||
try (ResultSet rs = stmt.executeQuery())
|
||||
{
|
||||
while (rs.next())
|
||||
{
|
||||
OrderDetail orderDetail = new OrderDetail();
|
||||
orderDetail.setId(rs.getInt("id"));
|
||||
orderDetail.setOrderId(rs.getInt("order_id"));
|
||||
orderDetail.setMenuItemId(rs.getInt("menu_item_id"));
|
||||
orderDetail.setQuantity(rs.getInt("quantity"));
|
||||
orderDetail.setPrice(rs.getDouble("price_at_purchase"));
|
||||
|
||||
orderDetails.add(orderDetail);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return orderDetails;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +1,59 @@
|
||||
package dev.dao;
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.User;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class UserDao {
|
||||
|
||||
public boolean save(User user) {
|
||||
|
||||
// TODO:
|
||||
// Insert user into database
|
||||
String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||
{
|
||||
stmt.setString(1, user.getUsername());
|
||||
stmt.setString(2, user.getPassword());
|
||||
stmt.setString(3, user.getEmail());
|
||||
|
||||
return false;
|
||||
int affectedRows = stmt.executeUpdate();
|
||||
return affectedRows > 0;
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
|
||||
// TODO:
|
||||
// Find a user by username
|
||||
|
||||
String sql = "SELECT id, username, password, email FROM users WHERE username = ?";
|
||||
try (Connection conn = DatabaseConnection.getConnection();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql))
|
||||
{
|
||||
stmt.setString(1, username);
|
||||
try (ResultSet rs = stmt.executeQuery())
|
||||
{
|
||||
if (rs.next())
|
||||
{
|
||||
User user = new User();
|
||||
user.setId(rs.getInt("id"));
|
||||
user.setUsername(rs.getString("username"));
|
||||
user.setPassword(rs.getString("password"));
|
||||
user.setEmail(rs.getString("email"));
|
||||
return user;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,15 +1,16 @@
|
||||
package dev.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatabaseConnection {
|
||||
|
||||
private static final String URL = "jdbc:postgresql://localhost:5432/restaurant_db"; // DB Server
|
||||
private static final String URL = "jdbc:postgresql://localhost:5432/restaurant"; // DB Server
|
||||
|
||||
private static final String USER = "postgres"; // Your Username
|
||||
|
||||
private static final String PASSWORD = "password"; // Your Password
|
||||
private static final String PASSWORD = "11"; // Your Password
|
||||
|
||||
private DatabaseConnection() {
|
||||
|
||||
@@ -17,11 +18,9 @@ public class DatabaseConnection {
|
||||
|
||||
public static Connection getConnection()
|
||||
throws SQLException {
|
||||
|
||||
// TODO:
|
||||
// Return a valid PostgreSQL connection
|
||||
return DriverManager.getConnection(URL, USER, PASSWORD);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,4 +12,43 @@ public class MenuItem {
|
||||
|
||||
private String category;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,35 @@ public class Order {
|
||||
|
||||
private double totalPrice;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public double getTotalPrice() {
|
||||
return totalPrice;
|
||||
}
|
||||
|
||||
public void setTotalPrice(double totalPrice) {
|
||||
this.totalPrice = totalPrice;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(LocalDateTime createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,43 @@ public class OrderDetail {
|
||||
|
||||
private double price;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public int getMenuItemId() {
|
||||
return menuItemId;
|
||||
}
|
||||
|
||||
public void setMenuItemId(int menuItemId) {
|
||||
this.menuItemId = menuItemId;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(int orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
}
|
||||
@@ -10,4 +10,35 @@ public class User {
|
||||
|
||||
private String email;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user