forked from AdvancedProgramming1404/WS-10-Database
database
This commit is contained in:
@@ -1,25 +1,133 @@
|
||||
package dev.dao;
|
||||
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.MenuItem;
|
||||
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
|
||||
public class MenuItemDao {
|
||||
|
||||
public List<MenuItem> findAll() {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all menu items
|
||||
|
||||
return null;
|
||||
public List<MenuItem> findAll(){
|
||||
|
||||
|
||||
List<MenuItem> list=new ArrayList<>();
|
||||
|
||||
|
||||
String sql="SELECT * FROM menu_items";
|
||||
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
|
||||
ResultSet rs=ps.executeQuery();
|
||||
|
||||
|
||||
|
||||
while(rs.next()){
|
||||
|
||||
|
||||
list.add(new MenuItem(
|
||||
|
||||
rs.getInt("id"),
|
||||
|
||||
rs.getString("name"),
|
||||
|
||||
rs.getString("description"),
|
||||
|
||||
rs.getDouble("price"),
|
||||
|
||||
rs.getString("category")
|
||||
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return list;
|
||||
|
||||
}
|
||||
|
||||
public MenuItem findById(int id) {
|
||||
|
||||
// TODO:
|
||||
// Find menu item by id
|
||||
|
||||
|
||||
|
||||
|
||||
public MenuItem findById(int id){
|
||||
|
||||
|
||||
String sql =
|
||||
"SELECT * FROM menu_items WHERE id=?";
|
||||
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setInt(1,id);
|
||||
|
||||
|
||||
|
||||
ResultSet rs=ps.executeQuery();
|
||||
|
||||
|
||||
|
||||
if(rs.next()){
|
||||
|
||||
|
||||
return new MenuItem(
|
||||
|
||||
rs.getInt("id"),
|
||||
|
||||
rs.getString("name"),
|
||||
|
||||
rs.getString("description"),
|
||||
|
||||
rs.getDouble("price"),
|
||||
|
||||
rs.getString("category")
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +1,130 @@
|
||||
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 con = DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)){
|
||||
|
||||
|
||||
ps.setInt(1, order.getUserId());
|
||||
|
||||
ps.setDouble(2, order.getTotalPrice());
|
||||
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
|
||||
|
||||
ResultSet rs = ps.getGeneratedKeys();
|
||||
|
||||
|
||||
if(rs.next()){
|
||||
|
||||
return rs.getInt(1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
public List<Order> findByUserId(int userId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve all orders of a user
|
||||
|
||||
return null;
|
||||
|
||||
public List<Order> findByUserId(int userId){
|
||||
|
||||
|
||||
List<Order> orders = new ArrayList<>();
|
||||
|
||||
|
||||
String sql =
|
||||
"SELECT * FROM orders WHERE user_id=?";
|
||||
|
||||
|
||||
try(Connection con = DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setInt(1,userId);
|
||||
|
||||
|
||||
|
||||
ResultSet rs = ps.executeQuery();
|
||||
|
||||
|
||||
|
||||
while(rs.next()){
|
||||
|
||||
|
||||
Order order = new Order();
|
||||
|
||||
|
||||
order.setId(rs.getInt("id"));
|
||||
|
||||
order.setUserId(rs.getInt("user_id"));
|
||||
|
||||
order.setTotalPrice(
|
||||
rs.getDouble("total_price")
|
||||
);
|
||||
|
||||
|
||||
Timestamp timestamp =
|
||||
rs.getTimestamp("created_at");
|
||||
|
||||
|
||||
if(timestamp != null){
|
||||
|
||||
order.setCreatedAt(
|
||||
timestamp.toLocalDateTime()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
orders.add(order);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return orders;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,24 +1,144 @@
|
||||
package dev.dao;
|
||||
|
||||
|
||||
import dev.database.DatabaseConnection;
|
||||
import dev.model.OrderDetail;
|
||||
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
|
||||
public class OrderDetailDao {
|
||||
|
||||
public void save(OrderDetail detail) {
|
||||
|
||||
// TODO:
|
||||
// Insert order detail
|
||||
|
||||
public void save(OrderDetail detail){
|
||||
|
||||
|
||||
|
||||
String sql =
|
||||
"""
|
||||
INSERT INTO order_details
|
||||
(order_id,menu_item_id,quantity,price)
|
||||
VALUES(?,?,?,?)
|
||||
""";
|
||||
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
|
||||
ps.setInt(1,detail.getOrderId());
|
||||
|
||||
ps.setInt(2,detail.getMenuItemId());
|
||||
|
||||
ps.setInt(3,detail.getQuantity());
|
||||
|
||||
ps.setDouble(4,detail.getPrice());
|
||||
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId) {
|
||||
|
||||
// TODO:
|
||||
// Retrieve order details
|
||||
|
||||
return null;
|
||||
|
||||
|
||||
public List<OrderDetail> findByOrderId(int orderId){
|
||||
|
||||
|
||||
List<OrderDetail> list =
|
||||
new ArrayList<>();
|
||||
|
||||
|
||||
String sql =
|
||||
"SELECT * FROM order_details WHERE order_id=?";
|
||||
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setInt(1,orderId);
|
||||
|
||||
|
||||
|
||||
ResultSet rs =
|
||||
ps.executeQuery();
|
||||
|
||||
|
||||
|
||||
while(rs.next()){
|
||||
|
||||
|
||||
OrderDetail detail =
|
||||
new OrderDetail();
|
||||
|
||||
|
||||
detail.setId(
|
||||
rs.getInt("id")
|
||||
);
|
||||
|
||||
|
||||
detail.setOrderId(
|
||||
rs.getInt("order_id")
|
||||
);
|
||||
|
||||
|
||||
detail.setMenuItemId(
|
||||
rs.getInt("menu_item_id")
|
||||
);
|
||||
|
||||
|
||||
detail.setQuantity(
|
||||
rs.getInt("quantity")
|
||||
);
|
||||
|
||||
|
||||
detail.setPrice(
|
||||
rs.getDouble("price")
|
||||
);
|
||||
|
||||
|
||||
list.add(detail);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return list;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,23 +1,109 @@
|
||||
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
|
||||
|
||||
public boolean save(User user){
|
||||
|
||||
|
||||
String sql =
|
||||
"INSERT INTO users(username,password,email) VALUES(?,?,?)";
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setString(1,user.getUsername());
|
||||
|
||||
ps.setString(2,user.getPassword());
|
||||
|
||||
ps.setString(3,user.getEmail());
|
||||
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public User findByUsername(String username) {
|
||||
|
||||
// TODO:
|
||||
// Find a user by username
|
||||
|
||||
|
||||
|
||||
public User findByUsername(String username){
|
||||
|
||||
|
||||
String sql =
|
||||
"SELECT * FROM users WHERE username=?";
|
||||
|
||||
|
||||
try(Connection con =
|
||||
DatabaseConnection.getConnection();
|
||||
|
||||
PreparedStatement ps =
|
||||
con.prepareStatement(sql)){
|
||||
|
||||
|
||||
ps.setString(1,username);
|
||||
|
||||
|
||||
|
||||
ResultSet rs=ps.executeQuery();
|
||||
|
||||
|
||||
|
||||
if(rs.next()){
|
||||
|
||||
|
||||
return new User(
|
||||
|
||||
rs.getInt("id"),
|
||||
|
||||
rs.getString("username"),
|
||||
|
||||
rs.getString("password"),
|
||||
|
||||
rs.getString("email")
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}catch(SQLException e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user