diff --git a/opp/core/src/main/java/OperationTeam.java b/opp/core/src/main/java/OperationTeam.java index ce149f7caf7250efbd34f82b4832c0fc0a4d5a1e..dcc79708a20e363e03f11638423c26998c8cdd83 100644 --- a/opp/core/src/main/java/OperationTeam.java +++ b/opp/core/src/main/java/OperationTeam.java @@ -3,7 +3,18 @@ import java.util.List; public record OperationTeam( Id<OperationTeam> operationTeamId, - List<OPStaff> opStaffList, + String teamName, Instant lastUpdate ) { + + public static sealed interface Command permits OperationTeam.CreateTeam, OperationTeam.DeleteTeam {} + + public static record CreateTeam( + String teamName + ) implements OperationTeam.Command {} + + public static record DeleteTeam( + Id<OperationTeam> operationTeamId + ) implements OperationTeam.Command {} + } diff --git a/opp/core/src/main/java/OperationTeamImpl.java b/opp/core/src/main/java/OperationTeamImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..101be27b9f393e1536cad5ad9736f68438f00e11 --- /dev/null +++ b/opp/core/src/main/java/OperationTeamImpl.java @@ -0,0 +1,53 @@ +import java.sql.SQLException; +import java.time.Instant; +import java.util.Optional; + +public class OperationTeamImpl implements OperationTeamService{ + + private final Repository repo; + + public OperationTeamImpl(Repository repo) { + this.repo = repo; + } + + + @Override + public OperationTeam process(OperationTeam.Command cmd) throws Exception { + return switch(cmd){ + case OperationTeam.CreateTeam cr -> createOperationTeamimpl(cr); + case OperationTeam.DeleteTeam del -> deleteOperationTeamimpl(del); + }; + } + + @Override + public Optional<OperationTeam> getOperationTeam(Id<OperationTeam> operationTeamId) { + return null; //repo.findOperationTeam(operationTeamId); + } + + private OperationTeam deleteOperationTeamimpl(OperationTeam.DeleteTeam del) throws SQLException { + + OperationTeam operationTeam = repo.deleteOperationTeam(del.operationTeamId()); + + return operationTeam; + } + + private OperationTeam createOperationTeamimpl(OperationTeam.CreateTeam cr) { + OperationTeam operationTeam = + new OperationTeam( + repo.operationTeamId(), + cr.teamName(), + Instant.now() + ); + return operationTeam; + } + + @Override + public Optional<OperationTeam> getOperationTeams() { + return Optional.empty(); + } + + @Override + public Optional<OPStaff> getOperationTeamOPStaff(Id<OperationTeam> operationTeamId) { + return Optional.empty(); + } +} diff --git a/opp/core/src/main/java/OperationTeamService.java b/opp/core/src/main/java/OperationTeamService.java new file mode 100644 index 0000000000000000000000000000000000000000..b4c3b3019b5369fcfc0a99214fbef7a0692e7d36 --- /dev/null +++ b/opp/core/src/main/java/OperationTeamService.java @@ -0,0 +1,14 @@ +import java.util.Optional; + +public interface OperationTeamService { + + public OperationTeam process(OperationTeam.Command cmd) throws Exception; + + public Optional<OperationTeam> getOperationTeam(Id<OperationTeam> operationTeamId); + + public Optional<OperationTeam> getOperationTeams(); + + public Optional<OPStaff> getOperationTeamOPStaff(Id<OperationTeam> operationTeamId); + + +} diff --git a/opp/core/src/main/java/Repository.java b/opp/core/src/main/java/Repository.java index 6b91ac742e4458cd5d2d005934ea051307ab3d5b..85053f7b13e188408695ae0da9db7ca6c710fb87 100644 --- a/opp/core/src/main/java/Repository.java +++ b/opp/core/src/main/java/Repository.java @@ -1,4 +1,3 @@ -import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Optional; @@ -75,4 +74,13 @@ public interface Repository { void deleteOPStaff(Id<OPStaff> id) throws SQLException; + + + + + Id<OperationTeam> operationTeamId(); + + void saveOperationTeam(OperationTeam operationTeam) throws Exception; + + OperationTeam deleteOperationTeam(Id<OperationTeam> id) throws SQLException; } diff --git a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java index f4617e798652409e11ee8336a307b05898d7f601..f5361dbc9274bf87f44b138c4a32a10ca29595b4 100644 --- a/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java +++ b/opp/jdbc-repo-impl/src/main/java/JDBCRepository.java @@ -51,6 +51,7 @@ class JDBCRepository implements Repository stmt.execute(CREATE_OPERATION_TABLE); stmt.execute(CREATE_OPSTAFF_TABLE); + stmt.execute(CREATE_OPERATIONTEAM_TABLE); } catch (SQLException e){ throw new RuntimeException(e); @@ -548,6 +549,79 @@ class JDBCRepository implements Repository } } + + /** + * Returns a new generated ID in the operationTeam-SQLTable + * If the generated ID is already used, generate another one. + * @return operationTeamID + */ + @Override + public Id<OperationTeam> operationTeamId() { + + var id = new Id<OperationTeam>(randomUUID().toString()); + + return id; //TODO + // findOperation(id).isEmpty() ? id : operationId(); + } + + /** + * Connects to the database and checks if the ID of the given OperationTeam is already used. + * If yes, then update existing entry, else insert new entry. + * @param operationTeam the OPStaff to save into the database + */ + @Override + public void saveOperationTeam(OperationTeam operationTeam) { + + try ( + var stmt = conn.createStatement() + ){ + //TODO + /*var sql = + findOPStaff(opStaff.id()).isPresent() ? + updateOPStaffSQL(opStaff) : + insertOPStaffSQL(opStaff);*/ + var sql = insertOperationTeamSQL(operationTeam); + + stmt.executeUpdate(sql); + + } catch (SQLException e){ + throw new RuntimeException(e); + } + } + + private static String insertOperationTeamSQL(OperationTeam operationTeam){ + return + "INSERT INTO operationteam(" + + "id,teamname,lastUpdate" + + ") VALUES (" + + sqlValue(operationTeam.operationTeamId().value()) + "," + + sqlValue(operationTeam.teamName()) + "," + + sqlValue(operationTeam.lastUpdate()) + + ");"; + } + + + @Override + public OperationTeam deleteOperationTeam(Id<OperationTeam> id) throws SQLException{ + + //var opStaff = findOPStaff(id); + + //if(opStaff.isPresent()) { + conn.createStatement().executeUpdate( + "DELETE FROM operationteam WHERE id = " + quoted(id.value()) + ";"); + + //} + return null; + } + + private static final String CREATE_OPERATIONTEAM_TABLE = """ + CREATE TABLE IF NOT EXISTS operationteam( + id VARCHAR(50) PRIMARY KEY, + teamname VARCHAR(50) NOT NULL, + lastUpdate TIMESTAMP NOT NULL + ); + """; + } diff --git a/opp/jdbc-repo-impl/src/test/java/Tests.java b/opp/jdbc-repo-impl/src/test/java/Tests.java index e8dadf89023d33d30521a31086cece096cbde1d0..0a0deaede551a43ffea2917e660ef7e510c03f72 100644 --- a/opp/jdbc-repo-impl/src/test/java/Tests.java +++ b/opp/jdbc-repo-impl/src/test/java/Tests.java @@ -25,6 +25,10 @@ public final class Tests private static OPStaffService opStaffService; + private static OperationTeam testOperationTeam = null; + + private static OperationTeamService operationTeamService; + @BeforeClass public static void init() throws Exception { @@ -51,6 +55,16 @@ public final class Tests opStaffService = new OPStaffServiceImpl(repo); + + testOperationTeam = new OperationTeam( + new Id<>("1212121212121"), + ("Cooles Team"), + Instant.now() + ); + + operationTeamService = new OperationTeamImpl(repo) { + }; + } //@Test @@ -268,6 +282,30 @@ public final class Tests } + @Test + public void testCreateOperationTeam() throws Exception{ + + try { + repo.saveOperationTeam(testOperationTeam); + } catch (Exception e){ + e.printStackTrace(); + } + } + + @Test + public void testDeleteOperationTeam() throws Exception{ + + var id = new Id<OperationTeam>("1212"); + + OperationTeam.DeleteTeam deleteCommand = new OperationTeam.DeleteTeam( + id + ); + + OperationTeam deleteOperationTeam = operationTeamService.process(deleteCommand); + + } + + }