From f6bc925494f48591271bac8c5d8c2ef759410db3 Mon Sep 17 00:00:00 2001 From: Sergey Iryupin Date: Sat, 25 May 2024 10:22:11 +0200 Subject: [PATCH] java pizza project #2 update --- .../src/main/java/pizza/JavaPizzaApp.java | 6 +-- .../pizza/repository/OrderDbRepository.java | 2 +- .../repository/OrderPizzaDbRepository.java | 38 ++++++++++++++++++- .../main/java/pizza/service/OrderService.java | 22 +++++------ 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java index 40414414..42d53569 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java @@ -24,13 +24,13 @@ public static void main(String[] args) { CrudRepository pizzaRepository = new PizzaRepository(); CrudRepository extComponentRepository = new ExtComponentRepository(); CrudRepository customerRepository = new CustomerRepository(); - CrudRepository 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(); } diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/OrderDbRepository.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/OrderDbRepository.java index a8452417..581820a8 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/OrderDbRepository.java +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/OrderDbRepository.java @@ -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 = ?"; diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/OrderPizzaDbRepository.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/OrderPizzaDbRepository.java index 3eece76f..808b0541 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/OrderPizzaDbRepository.java +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/OrderPizzaDbRepository.java @@ -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 { + private String dbName; + private CrudRepository 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 diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/service/OrderService.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/service/OrderService.java index 07feddff..f85cccb1 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/service/OrderService.java +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/service/OrderService.java @@ -13,12 +13,12 @@ public class OrderService { private OrderDbRepository repository; private CrudRepository pizzaRepository; - private CrudRepository orderPizzaRepozitory; + private OrderPizzaDbRepository orderPizzaRepozitory; private CrudRepository extComponentRepository; public OrderService(OrderDbRepository repository, CrudRepository pizzaRepository, - CrudRepository orderPizzaRepozitory, + OrderPizzaDbRepository orderPizzaRepozitory, CrudRepository extComponentRepository) { this.repository = repository; this.pizzaRepository = pizzaRepository; @@ -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) {