diff --git a/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepository.java b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepository.java new file mode 100644 index 0000000..a21a8a3 --- /dev/null +++ b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepository.java @@ -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 { + 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 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 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); + } + } +} diff --git a/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepositorySpec.groovy b/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepositorySpec.groovy new file mode 100644 index 0000000..d5ddafd --- /dev/null +++ b/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepositorySpec.groovy @@ -0,0 +1,144 @@ +package space.kklochko.jdbc_hospital_example.db.repositories + +import groovy.sql.Sql +import space.kklochko.jdbc_hospital_example.config.factories.LoadDataBaseConfigFromEnvFile +import space.kklochko.jdbc_hospital_example.config.models.DataBaseConfig +import space.kklochko.jdbc_hospital_example.db.factories.DataBaseConnection +import space.kklochko.jdbc_hospital_example.models.Department +import spock.lang.Shared +import spock.lang.Specification +import spock.lang.Stepwise +import spock.lang.Subject + +@Stepwise +class DepartmentRepositorySpec extends Specification { + @Shared + Sql sql + + @Shared + DataBaseConfig db + + @Shared + DataBaseConnection dbc + + def setupSpec() { + db = (new LoadDataBaseConfigFromEnvFile()).load("db.testing") + dbc = new DataBaseConnection(db) + + DepartmentRepository repo = new DepartmentRepository(dbc) + repo.createTable() + + sql = Sql.newInstance(db.getUrl(), db.getProperties().get("user"), db.getProperties().get("password")) + + sql.execute("insert into departments values " + + "('bcbbcdb4-702c-11ee-9113-c0e4349366cb', 'First department', 'Stepan Bandera Street', '380123451244')," + + "('becbafac-702c-11ee-9113-c0e4349366cb', 'Second department', 'Taras Shevchenko street', '380123451245')") + + } + + def cleanupSpec() { + sql.execute("delete from departments") + } + + def "Read one department"() { + given: "I have an existing department" + UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366cb") + + and: "I have a repo" + @Subject + DepartmentRepository repo = new DepartmentRepository(dbc) + + when: "reading the entry" + def result = repo.read(id) + + then: "checking that the reading was successful" + result + + and: "checking the entry with the expected data" + def data = sql.firstRow("SELECT name, location, phone FROM departments WHERE id = ?", id) + with(data) { + get("name") == 'Second department' + get("location") == 'Taras Shevchenko street' + get("phone") == '380123451245' + } + } + + def "Read all indicators"() { + given: "I have a repo" + @Subject + DepartmentRepository repo = new DepartmentRepository(dbc) + + when: "reading the entries" + def result = repo.readAll() + + then: "checking that the reading was successful" + result + + and: "checking the entries with the expected data" + def data = sql.rows("SELECT name, location, phone FROM departments") + data.collect{it.get("location")} == ['Stepan Bandera Street', 'Taras Shevchenko street'] + } + + def "Add one department"() { + given: "I create an department" + Department department = new Department("Department", "Lesya Ukrainka Street", "380123451235") + + and: "I have a repo" + @Subject + DepartmentRepository repo = new DepartmentRepository(dbc) + + when: "inserting the entry" + def result = repo.create(department) + + then: "checking that the insert was successful" + result + department.id + + and: "checking that the entry is added" + def isExist = sql.firstRow("SELECT name, location, phone FROM departments WHERE id = ?", department.id) + isExist + } + + def "Update one department"() { + given: "I have an existing department" + UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366cb") + Department department = new Department(id, "Department", "Lesya Ukrainka Street", "380123451235") + + and: "I have a repo" + @Subject + DepartmentRepository repo = new DepartmentRepository(dbc) + + when: "updating the entry" + def result = repo.update(department) + + then: "checking that the update was successful" + result + + and: "checking that the entry is updated" + def data = sql.firstRow("SELECT name, location, phone FROM departments WHERE id = ?", department.id) + with(data) { + get("name") == department.name + get("location") == department.location + get("phone") == department.phone + } + } + + def "Remove one department"() { + given: "I have the id of an existing department" + UUID id = UUID.fromString("bcbbcdb4-702c-11ee-9113-c0e4349366cb") + + and: "I have a repo" + @Subject + DepartmentRepository repo = new DepartmentRepository(dbc) + + when: "deleting the entry" + def result = repo.delete(id) + + then: "checking that the delete was successful" + result + + and: "checking that the entry was deleted" + def isExist = sql.firstRow("SELECT name, location, phone FROM departments WHERE id = ?", id) + !isExist + } +} diff --git a/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/PatientRepositorySpec.groovy b/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/PatientRepositorySpec.groovy index fc44281..3b24a1a 100644 --- a/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/PatientRepositorySpec.groovy +++ b/src/test/groovy/space/kklochko/jdbc_hospital_example/db/repositories/PatientRepositorySpec.groovy @@ -39,7 +39,7 @@ class PatientRepositorySpec extends Specification { sql.execute("delete from patients") } - def "Read one indicator"() { + def "Read one patients"() { given: "I have an existing patient" UUID id = UUID.fromString("becbafac-702c-11ee-9113-c0e4349366bb")