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 29, 2024
1 parent f03d579 commit e7c7437
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void main(String[] args) {
CrudRepository<Integer, Pizza> pizzaRepository = new PizzaRepository();
CrudRepository<Integer, ExtComponent> extComponentRepository = new ExtComponentRepository();
CrudRepository<Integer, Customer> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer, OrderPizza> {

private String dbName;
private CrudRepository<Integer, Pizza> pizzaRepository;
private CrudRepository<Integer, ExtComponent> componentRepository;
/*
CREATE TABLE IF NOT EXISTS order_pizza (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Expand All @@ -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<Integer, Pizza> 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<Integer, Pizza> pizzaRepository,
CrudRepository<Integer, ExtComponent> componentRepository) {
this.dbName = dbName;
this.pizzaRepository = pizzaRepository;
this.componentRepository = componentRepository;
}

@Override
Expand Down Expand Up @@ -61,16 +80,24 @@ 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()) {
Integer pizzaId = rs.getInt("pizza_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) {
Expand All @@ -80,6 +107,7 @@ public OrderPizza findById(Integer id) {

@Override
public Collection<OrderPizza> findAll() {
// TODO
return null;
}

Expand All @@ -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);
}
}
}

0 comments on commit e7c7437

Please sign in to comment.