Develop #1

Open
Z.Gharagozloo.M wants to merge 4 commits from develop into main
6 changed files with 172 additions and 15 deletions
Showing only changes of commit 699c044800 - Show all commits
+41 -5
View File
@@ -1,25 +1,61 @@
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 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;
}
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:
// Retrieve all orders of a user
try (Connection conn = DatabaseConnection.getConnection();
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;
}
}
+43 -5
View File
@@ -1,24 +1,62 @@
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_item_id, quantity, price_at_purchase) VALUES (?, ?, ?, ?)";
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) {
// TODO:
// Retrieve order details
List<OrderDetail> details = new ArrayList<>();
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;
}
}
+42 -5
View File
@@ -1,22 +1,59 @@
package dev.dao;
import dev.database.DatabaseConnection;
import dev.model.User;
import java.sql.*;
public class UserDao {
public boolean save(User user) {
// TODO:
// Insert user into database
String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
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) {
// TODO:
// Find a user by username
String sql = "SELECT * FROM users WHERE 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;
}
+15
View File
@@ -1,5 +1,6 @@
package dev.model;
import java.sql.Timestamp;
import java.time.LocalDateTime;
public class Order {
@@ -12,4 +13,18 @@ public class Order {
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;
}
}
+23
View File
@@ -12,4 +12,27 @@ public class OrderDetail {
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;
}
}
+8
View File
@@ -24,4 +24,12 @@ public class User {
public int getId() {
return this.id;
}
public String getEmail() {
return this.email;
}
public String getPassword() {
return this.password;
}
}