feat(connector): add Action database transactions

This commit is contained in:
Anthony 2024-04-26 14:45:39 +01:00
parent 6f149d3910
commit 8d8c140b35
3 changed files with 153 additions and 0 deletions

View File

@ -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<Action>) {
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<Action> {
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())
}
}

View File

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

View File

@ -14,3 +14,11 @@ VALUES (?, ?, ?, ?);
selectActions:
SELECT * FROM Action
WHERE procedureId = ?;
countActions:
SELECT COUNT(*) FROM Action
WHERE procedureId = ?;
deleteByProcedure:
DELETE FROM Action
WHERE procedureId = ?;