diff --git a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/ActionTransaction.kt b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/ActionTransaction.kt new file mode 100644 index 0000000..96c2e43 --- /dev/null +++ b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/ActionTransaction.kt @@ -0,0 +1,71 @@ +package io.anthonyberg.connector.shared + +import io.anthonyberg.connector.shared.database.ActionDatabase +import io.anthonyberg.connector.shared.database.DriverFactory +import io.anthonyberg.connector.shared.entity.Action + +/** + * All database transactions for Action + */ +class ActionTransaction (driverFactory: DriverFactory) { + private val database = ActionDatabase(driverFactory) + + /** + * Inserts one action into the database + * + * @param procedureId ID of the procedure + * @param step The order of when the action should be executed + * @param type Dataref for the action + * @param goal Desired value for the dataref/type + */ + fun createAction(procedureId: Int, step: Int, type: String, goal: String) { + database.createAction( + procedureId = procedureId.toLong(), + step = step.toLong(), + type = type, + goal = goal, + ) + } + + /** + * Inserts a list of Action into the database + * + * @param actions List of [Action] + */ + fun createActionFromList(actions: List) { + database.createMultipleActions(actions = actions) + } + + /** + * Gets a list of Actions for the procedure + * + * @param procedureId ID of the procedure to get actions from + * @return List of [Action] that are for the Procedure ID + */ + fun getActions(procedureId: Int): List { + val actions = database.getActions(procedureId = procedureId.toLong()) + + return actions + } + + /** + * Counts the number of actions there are for a procedure + * + * @param procedureId ID of the procedure to get a count for + * @return Amount of actions for the procedure specified + */ + fun countActionsInProcedure(procedureId: Int): Long { + val count = database.countActions(procedureId = procedureId.toLong()) + + return count + } + + /** + * Deletes all actions for a specific procedure + * + * @param procedureId ID of the procedure to delete actions from + */ + fun deleteActionByProcedure(procedureId: Int) { + database.deleteByProcedure(procedureId = procedureId.toLong()) + } +} diff --git a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/database/ActionDatabase.kt b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/database/ActionDatabase.kt index 125de34..83fb907 100644 --- a/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/database/ActionDatabase.kt +++ b/connector/shared/src/commonMain/kotlin/io/anthonyberg/connector/shared/database/ActionDatabase.kt @@ -1,6 +1,80 @@ package io.anthonyberg.connector.shared.database +import io.anthonyberg.connector.shared.entity.Action + class ActionDatabase (driverFactory: DriverFactory) { private val database = Database(driverFactory.createDriver()) private val dbQuery = database.actionQueries + + /** + * Inserts an Action to the database + */ + internal fun createAction(procedureId: Long, step: Long, type: String, goal: String) { + dbQuery.createAction( + procedureId = procedureId, + step = step, + type = type, + goal = goal, + ) + } + + /** + * Inserts a list of Action to the database + */ + internal fun createMultipleActions(actions: List) { + dbQuery.transaction { + actions.forEach { action -> + dbQuery.createAction( + procedureId = action.procedureId.toLong(), + step = action.step.toLong(), + type = action.type, + goal = action.goal, + ) + } + } + } + + /** + * Gets all Actions for a procedure + * + * @param procedureId id of procedure in database + * @return List of all Actions for that certain procedure + */ + internal fun getActions(procedureId: Long): List { + return dbQuery.selectActions(procedureId, ::mapActionSelecting).executeAsList() + } + + /** + * Counts all actions that exist for a procedure + * + * @param procedureId ID of the procedure to count for + * @return Count of all the actions for the procedure specified + */ + internal fun countActions(procedureId: Long): Long { + return dbQuery.countActions(procedureId = procedureId).executeAsOne() + } + + /** + * Deletes all actions in a procedure + * @param procedureId id of procedure to delete all actions from + */ + internal fun deleteByProcedure(procedureId: Long) { + dbQuery.deleteByProcedure(procedureId = procedureId) + } + + private fun mapActionSelecting( + id: Long, + procedureId: Long, + step: Long, + type: String, + goal: String, + ): Action { + return Action( + id = id.toInt(), + procedureId = procedureId.toInt(), + step = step.toInt(), + type = type, + goal = goal, + ) + } } diff --git a/connector/shared/src/commonMain/sqldelight/io/anthonyberg/connector/shared/database/Action.sq b/connector/shared/src/commonMain/sqldelight/io/anthonyberg/connector/shared/database/Action.sq index 0fa295e..ecc0092 100644 --- a/connector/shared/src/commonMain/sqldelight/io/anthonyberg/connector/shared/database/Action.sq +++ b/connector/shared/src/commonMain/sqldelight/io/anthonyberg/connector/shared/database/Action.sq @@ -14,3 +14,11 @@ VALUES (?, ?, ?, ?); selectActions: SELECT * FROM Action WHERE procedureId = ?; + +countActions: +SELECT COUNT(*) FROM Action +WHERE procedureId = ?; + +deleteByProcedure: +DELETE FROM Action +WHERE procedureId = ?;