Skip to content

Commit

Permalink
feat: Add Script first route
Browse files Browse the repository at this point in the history
  • Loading branch information
asolovieff committed Nov 28, 2023
1 parent d0cade2 commit 81654ce
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ fun Application.module() {
configureRouting(
projectService = projectService,
moduleService = moduleService,
environmentService = environmentService
environmentService = environmentService,
scriptService = scriptService
)
configureSerialization()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import dao.environment.EnvironmentDao
import dao.module.ModuleDao
import dao.project.ProjectDao
import dao.project.ProjectHierarchyDao
import dao.script.ScriptDao
import environment.EnvironmentService
import module.ModuleService
import org.jooq.SQLDialect
import org.jooq.impl.DSL
import project.ProjectService
import script.ScriptService
import java.sql.DriverManager

val dslContext = DSL.using(
Expand All @@ -26,4 +28,7 @@ val moduleDao = ModuleDao(dslContext)
val moduleService = ModuleService(moduleDao)

val environmentDao = EnvironmentDao(dslContext)
val environmentService = EnvironmentService(environmentDao)
val environmentService = EnvironmentService(environmentDao)

val scriptDao = ScriptDao(dslContext)
val scriptService = ScriptService(scriptDao)
13 changes: 7 additions & 6 deletions modules/app-server/dao/src/main/kotlin/dao/script/ScriptDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import org.jooq.DSLContext
import org.jooq.impl.DSL.`val`
import script.Script
import script.ScriptCreationRequest
import script.ScriptDaoInterface

class ScriptDao(val dslContext: DSLContext) {
fun insert(data: ScriptCreationRequest): Script =
class ScriptDao(val dslContext: DSLContext): ScriptDaoInterface {
override fun insert(data: ScriptCreationRequest): Script =
dslContext.insertInto(DM_SCRIPT, DM_SCRIPT.NAME, DM_SCRIPT.CHECKSUM, DM_SCRIPT.CONTENT)
.values(
`val`(data.name),
Expand All @@ -16,13 +17,13 @@ class ScriptDao(val dslContext: DSLContext) {
).returningResult(DM_SCRIPT.CHECKSUM, DM_SCRIPT.NAME, DM_SCRIPT.CONTENT)
.fetchSingleInto(Script::class.java)

fun delete(id: String) {
override fun delete(checksum: String) {
dslContext.delete(DM_SCRIPT)
.where(DM_SCRIPT.CHECKSUM.eq(id))
.where(DM_SCRIPT.CHECKSUM.eq(checksum))
.execute()
}

fun findOneById(id: String): Script? =
dslContext.fetchOne(DM_SCRIPT, DM_SCRIPT.CHECKSUM.eq(id))
override fun findOneByChecksum(checksum: String): Script? =
dslContext.fetchOne(DM_SCRIPT, DM_SCRIPT.CHECKSUM.eq(checksum))
?.into(Script::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ScriptDaoTest : AbstractDaoTest() {
val checksum = UUID.randomUUID().toString()

// When
val loadedScript = scriptDao.findOneById(checksum)
val loadedScript = scriptDao.findOneByChecksum(checksum)

// Then
expectThat(loadedScript).isNull()
Expand All @@ -93,7 +93,7 @@ class ScriptDaoTest : AbstractDaoTest() {
val insertedChecksum = scriptDao.insert(script).checksum

// When
val loadedScript = scriptDao.findOneById(insertedChecksum)
val loadedScript = scriptDao.findOneByChecksum(insertedChecksum)

// Then
expectThat(loadedScript).isNotNull().and {
Expand All @@ -116,7 +116,7 @@ class ScriptDaoTest : AbstractDaoTest() {
scriptDao.delete(randomChecksum)

// Then
expectThat(scriptDao.findOneById(insertedChecksum)).isNotNull()
expectThat(scriptDao.findOneByChecksum(insertedChecksum)).isNotNull()
}

@Test
Expand All @@ -131,8 +131,8 @@ class ScriptDaoTest : AbstractDaoTest() {
scriptDao.delete(insertedChecksum1)

// Then
expectThat(scriptDao.findOneById(insertedChecksum1)).isNull()
expectThat(scriptDao.findOneById(insertedChecksum2)).isNotNull()
expectThat(scriptDao.findOneByChecksum(insertedChecksum1)).isNull()
expectThat(scriptDao.findOneByChecksum(insertedChecksum2)).isNotNull()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package script

interface ScriptDaoInterface {
fun insert(data: ScriptCreationRequest): Script
fun delete(checksum: String)
fun findOneByChecksum(checksum: String): Script?
}
13 changes: 13 additions & 0 deletions modules/app-server/domain/src/main/kotlin/script/ScriptService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package script

class ScriptService(
private val scriptDao: ScriptDaoInterface
) {
fun insert(data: ScriptCreationRequest): Script =
scriptDao.insert(data)

fun delete(checksum: String) = scriptDao.delete(checksum)

fun findOneByChecksum(checksum: String): Script? =
scriptDao.findOneByChecksum(checksum)
}
13 changes: 9 additions & 4 deletions modules/app-server/rest/src/main/kotlin/rest/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,38 @@ import io.ktor.server.routing.*
import module.ModuleService
import project.ProjectService
import rest.v1.route.routeV1
import script.ScriptService

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

fun Application.module(
projectService: ProjectService,
moduleService: ModuleService,
environmentService: EnvironmentService
environmentService: EnvironmentService,
scriptService: ScriptService
) {
configureSerialization()
configureRouting(
projectService = projectService,
moduleService = moduleService,
environmentService = environmentService
environmentService = environmentService,
scriptService = scriptService
)
}

fun Application.configureRouting(
projectService: ProjectService,
moduleService: ModuleService,
environmentService: EnvironmentService
environmentService: EnvironmentService,
scriptService: ScriptService
) {
routing {
route("/api") {
routeV1(
projectService = projectService,
moduleService = moduleService,
environmentService = environmentService
environmentService = environmentService,
scriptService = scriptService
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import project.ProjectService
import rest.v1.route.environment.environmentV1Routes
import rest.v1.route.module.moduleV1Routes
import rest.v1.route.project.projectV1Routes
import rest.v1.route.script.scriptV1Routes
import script.ScriptService

fun Route.routeV1(
projectService: ProjectService,
moduleService: ModuleService,
environmentService: EnvironmentService
environmentService: EnvironmentService,
scriptService: ScriptService
) {
route("/v1") {
projectV1Routes(projectService)
moduleV1Routes(moduleService)
environmentV1Routes(environmentService)
scriptV1Routes(scriptService)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package rest.v1.route.script

import script.Script

@kotlinx.serialization.Serializable
data class ScriptDtoV1(
val name: String,
val checksum: String,
val content: String
)

fun Script.toDtoV1() = ScriptDtoV1(
name = this.name,
checksum = this.checksum,
content = this.checksum
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package rest.v1.route.script

import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import script.ScriptService

internal const val scriptId = "scriptId"

internal fun ApplicationCall.scriptId() = this.parameters[scriptId]

internal fun Route.scriptV1Routes(scriptService: ScriptService) {
get("/scripts/{$scriptId}") {
call.respondNullable(call.scriptId()?.let{ scriptService.findOneByChecksum(it) }?.toDtoV1())
}
}

0 comments on commit 81654ce

Please sign in to comment.