Skip to content

Instantly share code, notes, and snippets.

@JuliaKrivonos
Created August 30, 2021 11:23
Show Gist options
  • Select an option

  • Save JuliaKrivonos/7fd4a3b02074f6b25ba640871c3064cb to your computer and use it in GitHub Desktop.

Select an option

Save JuliaKrivonos/7fd4a3b02074f6b25ba640871c3064cb to your computer and use it in GitHub Desktop.
package org.jazzteam.fitnesclub.repository.jdbc;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.jazzteam.fitnesclub.model.Inventory;
import org.jazzteam.fitnesclub.model.InventoryType;
import org.jazzteam.fitnesclub.repository.addictional.InventoryRepository;
import org.jazzteam.fitnesclub.repository.addictional.InventoryTypeRepository;
import org.jazzteam.fitnesclub.repository.addictional.impl.InventoryTypeRepositoryImpl;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
@Slf4j
public class JdbcInventoryRepository implements InventoryRepository {
private InventoryTypeRepository inventoryTypeRepository = new InventoryTypeRepositoryImpl();
@Override
public Inventory findById(Long id) {
try (Connection connection = ConnectionUtil.getConnection()) {
return getInventoryById(id, connection);
} catch (SQLException throwables) {
log.debug("{}", throwables.getSQLState());
log.debug("{}", throwables.getMessage());
return null;
}
}
@Override
public Inventory save(Inventory inventory) {
try (Connection connection = ConnectionUtil.getConnection()) {
if (getInventoryById(inventory.getId(), connection) == null) {
return saveNew(inventory, connection);
}
return update(inventory, connection);
} catch (SQLException throwables) {
log.debug("{}", throwables.getSQLState());
log.debug("{}", throwables.getMessage());
return null;
}
}
@Override
public void delete(Inventory inventory) {
try (Connection connection = ConnectionUtil.getConnection()) {
String deleteCommand = "delete from fitness_db.inventories where id = ?";
PreparedStatement statement = connection.prepareStatement(deleteCommand, Statement.RETURN_GENERATED_KEYS);
statement.setLong(1, inventory.getId());
statement.execute();
} catch (SQLException throwables) {
log.error(throwables.getMessage());
throw new IllegalStateException();
}
}
@Override
public List<Inventory> findAll() {
List<Inventory> inventories = new ArrayList<>();
try (Connection connection = ConnectionUtil.getConnection()) {
String findCommand = "select id, type, amount from fitness_db.inventories";
PreparedStatement statement = connection.prepareStatement(findCommand);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Inventory inventory = getInventory(resultSet);
inventories.add(inventory);
}
} catch (SQLException throwables) {
log.error(throwables.getMessage());
}
return inventories;
}
@Override
public List<Inventory> findAll(Predicate<Inventory> predicate) {
List<Inventory> inventories = new ArrayList<>();
try (Connection connection = ConnectionUtil.getConnection()) {
String command = "select id, type, amount from fitness_db.inventories";
PreparedStatement statement = connection.prepareStatement(command);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Inventory inventory = getInventory(resultSet);
if (predicate.test(inventory)) {
inventories.add(inventory);
}
}
} catch (SQLException throwables) {
log.error(throwables.getMessage());
}
return inventories;
}
@Override
public List<Inventory> findByInventoryType(InventoryType inventoryType) {
List<Inventory> inventories = new ArrayList<>();
try (Connection connection = ConnectionUtil.getConnection()) {
String command = "select id, type, amount from fitness_db.inventories where type = ?";
PreparedStatement statement = connection.prepareStatement(command);
statement.setLong(1, inventoryType.getId());
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Inventory inventory = getInventory(resultSet);
inventories.add(inventory);
}
} catch (SQLException throwables) {
log.error(throwables.getMessage());
}
return inventories;
}
private Inventory getInventory(ResultSet resultSet) throws SQLException {
return new Inventory(
resultSet.getLong("id"),
inventoryTypeRepository.findById(resultSet.getLong("type")),
resultSet.getInt("amount"),
Lists.newArrayList());
}
private InventoryType getInventoryType(ResultSet resultSet) throws SQLException {
return new InventoryType(
resultSet.getLong("id"),
resultSet.getString("type_name"),
Lists.newArrayList(),
Lists.newArrayList());
}
private Inventory getInventoryById(Long id, Connection connection) {
log.debug("try find by id = {}", id);
String sqlCommand = "select id, type, amount from fitness_db.inventories where id = ?";
try (PreparedStatement statement = connection.prepareStatement(sqlCommand)) {
statement.setLong(1, id);
log.debug("{}", id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return getInventory(resultSet);
}
} catch (Exception e) {
log.debug("Not found by id {}", id);
return null;
}
return null;
}
private Inventory saveNew(Inventory inventory, Connection connection) throws SQLException {
String saveCommand = "insert into fitness_db.inventories (type, amount) values (?, ?)";
PreparedStatement statement = connection.prepareStatement(saveCommand, Statement.RETURN_GENERATED_KEYS);
statement.setLong(1, inventory.getInventoryType().getId());
statement.setInt(2, inventory.getAmount());
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new IllegalArgumentException("Failed save inventory");
}
ResultSet generated = statement.getGeneratedKeys();
if (generated.next()) {
inventory.setId(generated.getLong(1));
} else {
connection.rollback();
throw new IllegalArgumentException("No id obtained");
}
return getInventoryById(inventory.getId(), connection);
}
private Inventory update(Inventory inventory, Connection connection) throws SQLException {
String updateCommand = "update FITNESS_DB.inventories set amount = ?, type = ? where id = ?";
PreparedStatement statement = connection.prepareStatement(updateCommand, Statement.RETURN_GENERATED_KEYS);
statement.setInt(1, inventory.getAmount());
statement.setLong(2, inventory.getInventoryType().getId());
statement.setLong(3, inventory.getId());
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new IllegalArgumentException("Failed save inventory");
}
return getInventoryById(inventory.getId(), connection);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment