From 2b1294c82272ed30a87f250b29ce5ae263c97870 Mon Sep 17 00:00:00 2001 From: Anthony Date: Sat, 6 Apr 2024 12:08:04 +0200 Subject: [PATCH] feat(connector): create Procedure transactions and database --- .../connector/shared/ProcedureTransaction.kt | 85 +++++++++++++++++++ .../shared/database/ProcedureDatabase.kt | 73 ++++++++++++++++ .../connector/shared/entity/Procedure.kt | 29 +++++++ .../connector/shared/database/Procedure.sq | 26 ++++++ 4 files changed, 213 insertions(+) create mode 100644 connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/ProcedureTransaction.kt create mode 100644 connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/database/ProcedureDatabase.kt create mode 100644 connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/entity/Procedure.kt create mode 100644 connector/shared/src/commonMain/sqldelight/io/anthonyberg/connector/shared/database/Procedure.sq diff --git a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/ProcedureTransaction.kt b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/ProcedureTransaction.kt new file mode 100644 index 0000000..2f541ad --- /dev/null +++ b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/ProcedureTransaction.kt @@ -0,0 +1,85 @@ +package io.anthonyberg.connector.shared + +import io.anthonyberg.connector.shared.database.DriverFactory +import io.anthonyberg.connector.shared.database.ProcedureDatabase +import io.anthonyberg.connector.shared.entity.Procedure +import kotlinx.datetime.Clock + + +/** + * All database transactions for Procedure + */ +class ProcedureTransaction (driverFactory: DriverFactory) { + private val database = ProcedureDatabase(driverFactory) + + /** + * Creates a procedure for a project + * + * @param name Name of the procedure + * @param type Procedure type (e.g. Normal, Emergency) + * @param description Description of what the procedure will do + */ + fun createProject(name: String, type: String, description: String) { + // TODO add dynamic procedureId insertion + val projectId = 1 + val currentTime = Clock.System.now().toString() + + database.createProcedure( + projectId = projectId, + name = name, + type = type, + description = description, + createdUTC = currentTime + ) + } + + /** + * Gets all procedures in current project + * + * @return List of [Procedure] in current project + */ + fun getProcedures(): List { + // TODO add dynamic procedureId insertion + val projectId = 1 + + val procedures = database.getAllProcedures(projectId = projectId) + + return procedures + } + + /** + * Gets a specific procedure by its ID + * + * @param id Procedure ID + * @return [Procedure] based on ID given + */ + fun getProcedureById(id: Int): Procedure { + val procedure = database.getProcedureById(id) + + return procedure + } + + /** + * Counts the amount of procedures that exist based on the current project + * + * @return Number of procedures in the database + */ + fun countProcedures(): Long { + // TODO add dynamic procedureId insertion + val projectId = 1 + + return database.countProcedures(projectId = projectId) + } + + /** + * Checks if there are procedures on the database for the current project + * + * @return `true` if procedures exist for project + */ + fun proceduresExist(): Boolean { + // TODO add dynamic procedureId insertion + val projectId = 1 + + return database.countProcedures(projectId = projectId) > 0 + } +} diff --git a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/database/ProcedureDatabase.kt b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/database/ProcedureDatabase.kt new file mode 100644 index 0000000..72dbbf5 --- /dev/null +++ b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/database/ProcedureDatabase.kt @@ -0,0 +1,73 @@ +package io.anthonyberg.connector.shared.database + +import io.anthonyberg.connector.shared.entity.Procedure + +class ProcedureDatabase (driverFactory: DriverFactory) { + private val database = Database(driverFactory.createDriver()) + private val dbQuery = database.procedureQueries + + /** + * Creates a procedure on the database + * + * @param projectId Project ID + * @param name Name of the procedure + * @param type The type of the procedure + * @param description Description of what the procedure will do + * @param createdUTC Time of running this command + */ + internal fun createProcedure(projectId: Int, name: String, type: String, description: String, createdUTC: String) { + dbQuery.createProcedure( + projectId = projectId.toLong(), + name = name, + type = type, + description = description, + createdUTC = createdUTC + ) + } + + /** + * Gets all the procedures for a project + * @param projectId Project ID + */ + internal fun getAllProcedures(projectId: Int): List { + return dbQuery.selectProcedures(projectId.toLong(), ::mapProcedureSelecting).executeAsList() + } + + /** + * Gets a specific procedure by its ID + * @param id Procedure ID + */ + internal fun getProcedureById(id: Int): Procedure { + return dbQuery.selectProcedureById(id.toLong(), ::mapProcedureSelecting).executeAsOne() + } + + /** + * Counts how many procedures there are for a project + * @param projectId Project ID + */ + internal fun countProcedures(projectId: Int): Long { + return dbQuery.countProcedures(projectId.toLong()).executeAsOne() + } + + private fun mapProcedureSelecting( + id: Long, + projectId: Long, + name: String, + type: String, + description: String, + createdUTC: String, + modifiedUTC: String? + ): Procedure { + return Procedure( + id = id.toInt(), + projectId = projectId.toInt(), + name = name, + type = type, + description = description, + createdUTC = createdUTC, + modifiedUTC = modifiedUTC, + ) + } + + +} diff --git a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/entity/Procedure.kt b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/entity/Procedure.kt new file mode 100644 index 0000000..2f921ef --- /dev/null +++ b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/entity/Procedure.kt @@ -0,0 +1,29 @@ +package io.anthonyberg.connector.shared.entity + +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toInstant +import kotlinx.datetime.toLocalDateTime +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class Procedure( + @SerialName("id") + val id: Int, + @SerialName("project_id") + val projectId: Int, + @SerialName("name") + val name: String, + @SerialName("type") + val type: String, + @SerialName("description") + val description: String, + @SerialName("created_utc") + val createdUTC: String, + @SerialName("modified_utc") + val modifiedUTC: String?, +) { + // Convert String time to LocalDateTime variable + var created = createdUTC.toInstant().toLocalDateTime(TimeZone.UTC) + var modified = modifiedUTC?.toInstant()?.toLocalDateTime(TimeZone.UTC) +} diff --git a/connector/shared/src/commonMain/sqldelight/io/anthonyberg/connector/shared/database/Procedure.sq b/connector/shared/src/commonMain/sqldelight/io/anthonyberg/connector/shared/database/Procedure.sq new file mode 100644 index 0000000..83f6bd5 --- /dev/null +++ b/connector/shared/src/commonMain/sqldelight/io/anthonyberg/connector/shared/database/Procedure.sq @@ -0,0 +1,26 @@ +CREATE TABLE IF NOT EXISTS Procedure ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + projectId INTEGER NOT NULL, + name TEXT NOT NULL, + type TEXT NOT NULL, + description TEXT NOT NULL, + createdUTC TEXT NOT NULL, + modifiedUTC TEXT, + FOREIGN KEY (projectId) REFERENCES Project(id) +); + +createProcedure: +INSERT INTO Procedure(projectId, name, type, description, createdUTC) +VALUES (?, ?, ?, ?, ?); + +selectProcedures: +SELECT * FROM Procedure +WHERE projectId = ?; + +selectProcedureById: +SELECT * FROM Procedure +WHERE id = ?; + +countProcedures: +SELECT COUNT(*) FROM Procedure +WHERE projectId = ?;