Skip to content

Commit

Permalink
java pizza project #2 update
Browse files Browse the repository at this point in the history
  • Loading branch information
biblelamp committed May 25, 2024
1 parent 8caaee1 commit f6bc925
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public static void main(String[] args) {
CrudRepository<Integer, Pizza> pizzaRepository = new PizzaRepository();
CrudRepository<Integer, ExtComponent> extComponentRepository = new ExtComponentRepository();
CrudRepository<Integer, Customer> customerRepository = new CustomerRepository();
CrudRepository<Integer, OrderPizza> orderPizzaRepozitory = new OrderPizzaRepozitory();
OrderDbRepository orderRepository = new OrderDbRepository(SQLITE_DB_NAME, customerRepository, pizzaRepository);
OrderPizzaDbRepository orderPizzaRepository = new OrderPizzaDbRepository(SQLITE_DB_NAME);
OrderDbRepository orderRepository = new OrderDbRepository(SQLITE_DB_NAME, customerRepository, orderPizzaRepository);
// create all services
PizzaService pizzaService = new PizzaService(pizzaRepository);
ExtComponentService extComponentService = new ExtComponentService(extComponentRepository);
CustomerService customerService = new CustomerService(customerRepository);
OrderService orderService = new OrderService(orderRepository, pizzaRepository, orderPizzaRepozitory, extComponentRepository);
OrderService orderService = new OrderService(orderRepository, pizzaRepository, orderPizzaRepository, extComponentRepository);
// create & run controller
new AppController(pizzaService, extComponentService, customerService, orderService).run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ CREATE TABLE IF NOT EXISTS order_order_pizza (
)
*/

private final String SQL_INSERT = "INSERT INTO `order` (customerId,state,create_date) VALUES (?,?,?)";
private final String SQL_INSERT = "INSERT INTO `order` (customer_id,state,create_date) VALUES (?,?,?)";
private final String SQL_UPDATE = "UPDATE `order` SET customer_id = ?, state = ?, close_date = ? WHERE id = ?";
private final String SQL_FIND_BY_ID = "SELECT * FROM `order` WHERE id = ?";
private final String SQL_DELETE_BY_ID = "DELETE FROM `order` WHERE id = ?";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
package pizza.repository;

import pizza.domain.Customer;
import pizza.domain.OrderPizza;
import pizza.domain.Pizza;

import java.sql.*;
import java.util.Collection;

public class OrderPizzaDbRepository implements CrudRepository<Integer, OrderPizza> {

private String dbName;
private CrudRepository<Integer, Pizza> pizzaRepository;
/*
CREATE TABLE IF NOT EXISTS order_pizza (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
pizza_id INTEGER NOT NULL
)
CREATE TABLE IF NOT EXISTS order_pizza_component (
order_pizza_id INTEGER NOT NULL,
component_id INTEGER NOT NULL
)
*/

private final String SQL_INSERT = "INSERT INTO order_pizza (pizzaId) VALUES (?)";
private final String SQL_UPDATE = "UPDATE order_pizza SET pizza_id = ? WHERE id = ?";
private final String SQL_FIND_BY_ID = "SELECT * FROM order_pizza WHERE id = ?";
private final String SQL_DELETE_BY_ID = "DELETE FROM order_pizza WHERE id = ?";
private final String SQL_FIND_ALL = "SELECT * FROM order_pizza";

public OrderPizzaDbRepository(String dbName) {
this.dbName = dbName;
}

@Override
public void save(OrderPizza value) {
public void save(OrderPizza orderPizza) {
try (Connection connection = DriverManager.getConnection(dbName);
PreparedStatement psi = connection.prepareStatement(SQL_INSERT, Statement.RETURN_GENERATED_KEYS);
PreparedStatement psu = connection.prepareStatement(SQL_UPDATE)) {
if (orderPizza.getId() == null) {

} else {

}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
public class OrderService {
private OrderDbRepository repository;
private CrudRepository<Integer, Pizza> pizzaRepository;
private CrudRepository<Integer, OrderPizza> orderPizzaRepozitory;
private OrderPizzaDbRepository orderPizzaRepozitory;
private CrudRepository<Integer, ExtComponent> extComponentRepository;

public OrderService(OrderDbRepository repository,
CrudRepository<Integer, Pizza> pizzaRepository,
CrudRepository<Integer, OrderPizza> orderPizzaRepozitory,
OrderPizzaDbRepository orderPizzaRepozitory,
CrudRepository<Integer, ExtComponent> extComponentRepository) {
this.repository = repository;
this.pizzaRepository = pizzaRepository;
Expand All @@ -44,20 +44,18 @@ public boolean delete(int id) {
return false;
}

public void addOrderPizza(int orderId, int pizzaId) {
public void addOrderPizza(int orderId, int orderPizzaId) {
Order order = get(orderId);
Pizza pizza = pizzaRepository.findById(pizzaId);
//OrderPizza orderPizza = new OrderPizza(pizza);
order.getOrderPizzas().add(pizza);
repository.addPizza(order, pizza);
OrderPizza orderPizza = orderPizzaRepozitory.findById(orderPizzaId);
order.getOrderPizzas().add(orderPizza);
repository.addPizza(order, orderPizza);
}

public void deleteOrderPizza(int orderId, int pizzaId) {
public void deleteOrderPizza(int orderId, int orderPizzaId) {
Order order = get(orderId);
Pizza pizza = pizzaRepository.findById(pizzaId);
//OrderPizza orderPizza = orderPizzaRepozitory.findById(orderPizzaId);
order.getOrderPizzas().remove(pizza);
repository.deletePizza(order, pizza);
OrderPizza orderPizza = orderPizzaRepozitory.findById(orderPizzaId);
order.getOrderPizzas().remove(orderPizza);
repository.deletePizza(order, orderPizza);
}

public void addExtСomponent(int orderId, int orderPizzaId, int extСomponentId) {
Expand Down

0 comments on commit f6bc925

Please sign in to comment.