Add the repository for Deparment.

This commit is contained in:
2023-10-22 20:42:42 +03:00
parent e528454576
commit fead10291c
3 changed files with 279 additions and 1 deletions
@@ -0,0 +1,134 @@
package space.kklochko.jdbc_hospital_example.db.repositories;
import space.kklochko.jdbc_hospital_example.db.factories.DataBaseConnection;
import space.kklochko.jdbc_hospital_example.db.repositories.blocks.TransactionalInsert;
import space.kklochko.jdbc_hospital_example.db.repositories.blocks.TransactionalUpdate;
import space.kklochko.jdbc_hospital_example.db.repositories.blocks.CreateTable;
import space.kklochko.jdbc_hospital_example.models.Department;
import java.sql.*;
import java.util.ArrayList;
import java.util.UUID;
public class DepartmentRepository extends AbstractRepository<Department> {
private final String createTableStatement = "create table if not exists departments ("
+ "id uuid default uuid_generate_v1(),"
+ "name varchar(100) not null,"
+ "location varchar(100) not null,"
+ "phone varchar(25) not null)";
public DepartmentRepository(DataBaseConnection db){
super(db);
}
public boolean createTable() {
CreateTable createTable = new CreateTable();
return createTable.create(db, createTableStatement);
}
public boolean create(Department object) {
try (Connection connection = db.connect()) {
connection.setAutoCommit(false);
String sql = "insert into departments (name, location, phone) values (?, ?, ?) returning id;";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, object.getName());
preparedStatement.setString(2, object.getLocation());
preparedStatement.setString(3, object.getPhone());
ResultSet resultSet = (new TransactionalInsert()).run(connection, preparedStatement);
if (resultSet.next()) {
UUID uuid = (UUID) resultSet.getObject("id");
object.setId(uuid);
return true;
}
return false;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Department read(UUID id) {
try (Connection connection = db.connect()) {
String sql = "select id, name, location, phone from departments where id = ?;";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
UUID uuid = (UUID) resultSet.getObject("id");
String name = resultSet.getString("name");
String location = resultSet.getString("location");
String phone = resultSet.getString("phone");
return new Department(uuid, name, location, phone);
}
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public ArrayList<Department> readAll() {
try (Connection connection = db.connect()) {
String sql = "select id, name, location, phone from departments;";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ArrayList<Department> patients = new ArrayList<>();
while (resultSet.next()) {
UUID uuid = (UUID) resultSet.getObject("id");
String name = resultSet.getString("name");
String location = resultSet.getString("location");
String phone = resultSet.getString("phone");
patients.add(new Department(uuid, name, location, phone));
}
return patients;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public boolean update(Department object) {
try (Connection connection = db.connect()) {
connection.setAutoCommit(false);
String sql = "update departments set name = ?, location = ?, phone = ? where id = ?;";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, object.getName());
preparedStatement.setString(2, object.getLocation());
preparedStatement.setString(3, object.getPhone());
preparedStatement.setObject(4, object.getId());
int rowCount = (new TransactionalUpdate()).run(connection, preparedStatement);
return rowCount > 0;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public boolean delete(UUID id) {
try (Connection connection = db.connect()) {
connection.setAutoCommit(false);
String sql = "delete from departments where id = ?;";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1, id);
int rowCount = (new TransactionalUpdate()).run(connection, preparedStatement);
return rowCount > 0;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}