From e7c74379722a09027613a54813db95484269aff8 Mon Sep 17 00:00:00 2001 From: Sergey Iryupin Date: Wed, 29 May 2024 08:59:34 +0200 Subject: [PATCH] java pizza project #2 update --- .../src/main/java/pizza/JavaPizzaApp.java | 2 +- .../repository/db/OrderPizzaDbRepository.java | 58 ++++++++++++++++--- 2 files changed, 51 insertions(+), 9 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 1021c843..eac8db40 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/JavaPizzaApp.java @@ -26,7 +26,7 @@ public static void main(String[] args) { CrudRepository pizzaRepository = new PizzaRepository(); CrudRepository extComponentRepository = new ExtComponentRepository(); CrudRepository customerRepository = new CustomerRepository(); - OrderPizzaDbRepository orderPizzaRepository = new OrderPizzaDbRepository(SQLITE_DB_NAME, pizzaRepository); + OrderPizzaDbRepository orderPizzaRepository = new OrderPizzaDbRepository(SQLITE_DB_NAME, pizzaRepository, extComponentRepository); OrderDbRepository orderRepository = new OrderDbRepository(SQLITE_DB_NAME, customerRepository, orderPizzaRepository); // create all services PizzaService pizzaService = new PizzaService(pizzaRepository); diff --git a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/db/OrderPizzaDbRepository.java b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/db/OrderPizzaDbRepository.java index 94823fd0..d6a52118 100644 --- a/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/db/OrderPizzaDbRepository.java +++ b/BerlinAIT/Cohort42.1project/JavaPizzaMvn/src/main/java/pizza/repository/db/OrderPizzaDbRepository.java @@ -1,18 +1,30 @@ package pizza.repository.db; -import pizza.domain.Customer; import pizza.domain.ExtComponent; import pizza.domain.OrderPizza; import pizza.domain.Pizza; import pizza.repository.CrudRepository; -import java.sql.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; import java.util.Collection; +/** + * Order pizza repository + * Implementation of access methods to the Order pizza data source + * + * @author Sergey Iryupin + * @version 29-May-24 + */ public class OrderPizzaDbRepository implements CrudRepository { private String dbName; private CrudRepository pizzaRepository; + private CrudRepository componentRepository; /* CREATE TABLE IF NOT EXISTS order_pizza ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, @@ -30,9 +42,16 @@ CREATE TABLE IF NOT EXISTS order_pizza_component ( 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, CrudRepository pizzaRepository) { + private final String SQL_COMPONENT_INSERT = "INSERT INTO order_pizza_component (order_pizza_id,component_id) VALUES (?,?)"; + private final String SQL_COMPONENT_DELETE = "DELETE FROM order_pizza_component WHERE order_pizza_id = ? AND component_id = ?"; + private final String SQL_FIND_BY_ORDER_PIZZA_ID = "SELECT * FROM order_pizza_component WHERE order_pizza_id = ?"; + + public OrderPizzaDbRepository(String dbName, + CrudRepository pizzaRepository, + CrudRepository componentRepository) { this.dbName = dbName; this.pizzaRepository = pizzaRepository; + this.componentRepository = componentRepository; } @Override @@ -61,8 +80,9 @@ public OrderPizza save(OrderPizza orderPizza) { public OrderPizza findById(Integer id) { OrderPizza orderPizza = null; try (Connection connection = DriverManager.getConnection(dbName); - PreparedStatement ps = connection.prepareStatement(SQL_FIND_BY_ID)) { - // read order + PreparedStatement ps = connection.prepareStatement(SQL_FIND_BY_ID); + PreparedStatement psc = connection.prepareStatement(SQL_FIND_BY_ORDER_PIZZA_ID)) { + // read order pizza ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if (rs.next()) { @@ -70,7 +90,14 @@ public OrderPizza findById(Integer id) { Pizza pizza = pizzaRepository.findById(pizzaId); orderPizza = new OrderPizza(pizza); orderPizza.setId(id); - // TODO read components + // read components + psc.setInt(1, orderPizza.getId()); + ResultSet rsc = psc.executeQuery(); + while (rsc.next()) { + Integer componentId = rsc.getInt("component_id"); + ExtComponent component = componentRepository.findById(componentId); + orderPizza.getComponents().add(component); + } } return orderPizza; } catch (SQLException e) { @@ -80,6 +107,7 @@ public OrderPizza findById(Integer id) { @Override public Collection findAll() { + // TODO return null; } @@ -100,10 +128,24 @@ public void deleteAll() { } public void addComponent(OrderPizza orderPizza, ExtComponent extComponent) { - // TODO + try (Connection connection = DriverManager.getConnection(dbName); + PreparedStatement psi = connection.prepareStatement(SQL_COMPONENT_INSERT)) { + psi.setInt(1, orderPizza.getId()); + psi.setInt(2, extComponent.getId()); + psi.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } } public void deleteComponents(OrderPizza orderPizza, ExtComponent extСomponent) { - // TODO + try (Connection connection = DriverManager.getConnection(dbName); + PreparedStatement psd = connection.prepareStatement(SQL_COMPONENT_DELETE)) { + psd.setInt(1, orderPizza.getId()); + psd.setInt(2, extСomponent.getId()); + psd.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException(e); + } } }