Updated Order, OrderDetail and User, completed OrderDao, UserDao and OrderDetailDoa.
This commit is contained in:
@@ -1,25 +1,61 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.Order;
|
import dev.model.Order;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OrderDao {
|
public class OrderDao {
|
||||||
|
|
||||||
public int save(Order order) {
|
public int save(Order order) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "INSERT INTO orders (user_id, total_price) VALUES (?, ?)";
|
||||||
// Insert order and return generated id
|
|
||||||
|
|
||||||
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||||
|
|
||||||
|
pstmt.setInt(1, order.getUserId());
|
||||||
|
pstmt.setDouble(2, order.getTotalPrice());
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
|
||||||
|
//Generated ID
|
||||||
|
try (ResultSet rs = pstmt.getGeneratedKeys()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getInt(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error saving order: " + e.getMessage());
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Order> findByUserId(int userId) {
|
public List<Order> findByUserId(int userId) {
|
||||||
|
List<Order> orders = new ArrayList<>();
|
||||||
|
String sql = "SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC";
|
||||||
|
|
||||||
// TODO:
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
// Retrieve all orders of a user
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
|
||||||
return null;
|
pstmt.setInt(1, userId);
|
||||||
|
|
||||||
|
try (ResultSet rs = pstmt.executeQuery()) {
|
||||||
|
while (rs.next()) {
|
||||||
|
Order order = new Order(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getInt("user_id"),
|
||||||
|
rs.getTimestamp("created_at"),
|
||||||
|
rs.getDouble("total_price")
|
||||||
|
);
|
||||||
|
orders.add(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error retrieving orders for user: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return orders;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,62 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.OrderDetail;
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
public class OrderDetailDao {
|
public class OrderDetailDao {
|
||||||
|
|
||||||
public void save(OrderDetail detail) {
|
public void save(OrderDetail detail) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "INSERT INTO order_details (order_id, menu_item_id, quantity, price_at_purchase) VALUES (?, ?, ?, ?)";
|
||||||
// Insert order detail
|
|
||||||
|
|
||||||
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
pstmt.setInt(1, detail.getOrderId());
|
||||||
|
pstmt.setInt(2, detail.getMenuItemId());
|
||||||
|
pstmt.setInt(3, detail.getQuantity());
|
||||||
|
pstmt.setDouble(4, detail.getPriceAtPurchase());
|
||||||
|
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error saving order detail: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<OrderDetail> findByOrderId(int orderId) {
|
public List<OrderDetail> findByOrderId(int orderId) {
|
||||||
|
|
||||||
// TODO:
|
List<OrderDetail> details = new ArrayList<>();
|
||||||
// Retrieve order details
|
String sql = "SELECT * FROM order_details WHERE order_id = ?";
|
||||||
|
|
||||||
return null;
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
pstmt.setInt(1, orderId);
|
||||||
|
|
||||||
|
try (ResultSet rs = pstmt.executeQuery()) {
|
||||||
|
while (rs.next()) {
|
||||||
|
OrderDetail detail = new OrderDetail(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getInt("order_id"),
|
||||||
|
rs.getInt("menu_item_id"),
|
||||||
|
rs.getInt("quantity"),
|
||||||
|
rs.getDouble("price_at_purchase")
|
||||||
|
);
|
||||||
|
details.add(detail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error retrieving order details: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return details;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,59 @@
|
|||||||
package dev.dao;
|
package dev.dao;
|
||||||
|
|
||||||
|
import dev.database.DatabaseConnection;
|
||||||
import dev.model.User;
|
import dev.model.User;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
public class UserDao {
|
public class UserDao {
|
||||||
|
|
||||||
public boolean save(User user) {
|
public boolean save(User user) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
|
||||||
// Insert user into database
|
|
||||||
|
|
||||||
return false;
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
pstmt.setString(1, user.getUsername());
|
||||||
|
pstmt.setString(2, user.getPassword());
|
||||||
|
|
||||||
|
if (user.getEmail() == null || user.getEmail().trim().isEmpty()) {
|
||||||
|
pstmt.setNull(3, Types.VARCHAR);
|
||||||
|
} else {
|
||||||
|
pstmt.setString(3, user.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
int rowsAffected = pstmt.executeUpdate();
|
||||||
|
return rowsAffected > 0;
|
||||||
|
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error saving user: " + e.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUsername(String username) {
|
public User findByUsername(String username) {
|
||||||
|
|
||||||
// TODO:
|
String sql = "SELECT * FROM users WHERE username = ?";
|
||||||
// Find a user by username
|
|
||||||
|
|
||||||
|
try (Connection conn = DatabaseConnection.getConnection();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||||
|
|
||||||
|
pstmt.setString(1, username);
|
||||||
|
|
||||||
|
try (ResultSet rs = pstmt.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return new User(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getString("username"),
|
||||||
|
rs.getString("password"),
|
||||||
|
rs.getString("email")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.err.println("Error finding user by username: " + e.getMessage());
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package dev.model;
|
package dev.model;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
public class Order {
|
public class Order {
|
||||||
@@ -12,4 +13,18 @@ public class Order {
|
|||||||
|
|
||||||
private double totalPrice;
|
private double totalPrice;
|
||||||
|
|
||||||
|
public Order(int id, int userId, Timestamp createdAt, double totalPrice) {
|
||||||
|
this.id = id;
|
||||||
|
this.userId = userId;
|
||||||
|
this.createdAt = createdAt.toLocalDateTime();
|
||||||
|
this.totalPrice = totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getTotalPrice() {
|
||||||
|
return totalPrice;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,27 @@ public class OrderDetail {
|
|||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
|
public OrderDetail(int id, int orderId, int menuItemId, int quantity, double priceAtPurchase) {
|
||||||
|
this.id = id;
|
||||||
|
this.orderId = orderId;
|
||||||
|
this.menuItemId = menuItemId;
|
||||||
|
this.quantity = quantity;
|
||||||
|
this.price = priceAtPurchase;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMenuItemId() {
|
||||||
|
return menuItemId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getQuantity() {
|
||||||
|
return quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPriceAtPurchase() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -24,4 +24,12 @@ public class User {
|
|||||||
public int getId() {
|
public int getId() {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return this.email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return this.password;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user