Skip to content

Commit

Permalink
legg til noe greier for demo
Browse files Browse the repository at this point in the history
  • Loading branch information
anderssonw committed Jun 13, 2024
1 parent 30b7f93 commit 1d2fd1d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import no.kartverket.matrikkel.bygning.db.DatabaseSingleton
import no.kartverket.matrikkel.bygning.repositories.DemoRepository
import no.kartverket.matrikkel.bygning.repositories.HealthRepository
import no.kartverket.matrikkel.bygning.routes.v1.baseRoutesV1
import no.kartverket.matrikkel.bygning.routes.v1.probeRouting
Expand Down Expand Up @@ -65,8 +66,7 @@ fun Application.module() {

install(NotarizedApplication()) {
spec = OpenApiSpec(
jsonSchemaDialect = "https://spec.openapis.org/oas/3.1/dialect/base",
info = Info(
jsonSchemaDialect = "https://spec.openapis.org/oas/3.1/dialect/base", info = Info(
title = "API For Egenregistrering av Bygningsdata",
version = "0.1",
)
Expand All @@ -80,7 +80,10 @@ fun Application.module() {

val egenregistreringsService = EgenregistreringsService()

baseRoutesV1(egenregistreringsService)

val demoRepository = DemoRepository(dbConnection)

baseRoutesV1(egenregistreringsService, demoRepository)
}

fun Application.internalModule() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package no.kartverket.matrikkel.bygning.repositories

import kotlinx.serialization.Serializable
import no.kartverket.matrikkel.bygning.models.EnergikildeDTO
import java.sql.Connection
import java.sql.ResultSet

@Serializable
data class DemospokelseDTO(
val id: Int,
val spokelsesnavn: String,
)

class DemoRepository(private val dbConnection: Connection) {
fun getDemospokelser(): List<DemospokelseDTO> {
val statement = dbConnection.createStatement()

val resultSet = statement.executeQuery(
"""
select * from demospokelser;
""".trimIndent()
)

return resultSet.toDemospokelseDTO()
}

fun ResultSet.toDemospokelseDTO(): List<DemospokelseDTO> {
val demospokelser: MutableList<DemospokelseDTO> = emptyList<DemospokelseDTO>().toMutableList()

while (this.next()) {
demospokelser.add(
DemospokelseDTO(
id = this.getInt("id"), spokelsesnavn = this.getString("spokelsesnavn")
)
)
}

return demospokelser
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package no.kartverket.matrikkel.bygning.routes.v1

import io.bkbn.kompendium.core.metadata.GetInfo
import io.bkbn.kompendium.core.plugin.NotarizedRoute
import io.bkbn.kompendium.core.routes.swagger
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import no.kartverket.matrikkel.bygning.repositories.DemoRepository
import no.kartverket.matrikkel.bygning.repositories.DemospokelseDTO
import no.kartverket.matrikkel.bygning.services.EgenregistreringsService

fun Application.baseRoutesV1(
egenregistreringsService: EgenregistreringsService
egenregistreringsService: EgenregistreringsService, demoRepository: DemoRepository
) {
routing {
swagger()
Expand All @@ -21,7 +25,31 @@ fun Application.baseRoutesV1(
}

route("/v1") {
route("/demospokelser") {
demoDoc()
get {
val spokelser = demoRepository.getDemospokelser();

call.respond(spokelser)
}
}
egenregistreringRouting(egenregistreringsService)
}

}
}

private fun Route.demoDoc() {
install(NotarizedRoute()) {
get = GetInfo.builder {
summary("Henter demospøkelser")
description("Henter demospøkelser med navn og id")

response {
responseCode(HttpStatusCode.OK)
responseType<List<DemospokelseDTO>>()
description("Demospøkelsene i databasen")
}
}
}
}
11 changes: 11 additions & 0 deletions src/main/resources/db/migration/V2__dummy_demo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create table if not exists demospokelser
(
id serial primary key,
spokelsesnavn varchar(50) not null
);

insert into demospokelser(spokelsesnavn)
values ('Mr. Spooky'),
('Ghostman'),
('Hattifnatten'),
('Mr. Bones');

0 comments on commit 1d2fd1d

Please sign in to comment.