feat(connector): create Procedure transactions and database

This commit is contained in:
Anthony 2024-04-06 12:08:04 +02:00
parent c895e6aaf6
commit 2b1294c822
4 changed files with 213 additions and 0 deletions

View File

@ -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<Procedure> {
// 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
}
}

View File

@ -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<Procedure> {
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,
)
}
}

View File

@ -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)
}

View File

@ -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 = ?;