From e0400b4474c83df336a2c5085e32f6b65e7a0a8d Mon Sep 17 00:00:00 2001 From: Martim Francisco Date: Tue, 5 Apr 2022 00:37:37 +0100 Subject: [PATCH] Started data access #5 --- code/jvm/build.gradle.kts | 6 +++++ .../pt/isel/ion/teams/ConfigProperties.kt | 10 +++++++ .../ion/teams/IOnteamsServiceApplication.kt | 27 +++++++++++++++++-- .../organizations/OrganitazionController.kt | 15 +++++++++++ .../ion/teams/organizations/Organization.kt | 7 +++++ .../teams/organizations/OrganizationDAO.kt | 8 ++++++ .../src/main/resources/application.properties | 2 +- code/sql/.idea/.gitignore | 6 +++++ code/sql/.idea/dataSources.xml | 12 +++++++++ code/sql/.idea/modules.xml | 8 ++++++ code/sql/.idea/sql.iml | 8 ++++++ code/sql/.idea/vcs.xml | 6 +++++ code/sql/createTables.sql | 1 + 13 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 code/jvm/src/main/kotlin/pt/isel/ion/teams/ConfigProperties.kt create mode 100644 code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/OrganitazionController.kt create mode 100644 code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/Organization.kt create mode 100644 code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/OrganizationDAO.kt create mode 100644 code/sql/.idea/.gitignore create mode 100644 code/sql/.idea/dataSources.xml create mode 100644 code/sql/.idea/modules.xml create mode 100644 code/sql/.idea/sql.iml create mode 100644 code/sql/.idea/vcs.xml diff --git a/code/jvm/build.gradle.kts b/code/jvm/build.gradle.kts index 27b8a86..f1e2fff 100644 --- a/code/jvm/build.gradle.kts +++ b/code/jvm/build.gradle.kts @@ -16,6 +16,12 @@ repositories { } dependencies { + implementation("org.jdbi:jdbi3-kotlin-sqlobject:3.28.0") + implementation("org.jdbi:jdbi3-core:3.28.0") + implementation("org.jdbi:jdbi3-kotlin:3.28.0") + implementation("org.jdbi:jdbi3-postgres:3.28.0") + implementation("org.postgresql:postgresql:42.3.3") + implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") diff --git a/code/jvm/src/main/kotlin/pt/isel/ion/teams/ConfigProperties.kt b/code/jvm/src/main/kotlin/pt/isel/ion/teams/ConfigProperties.kt new file mode 100644 index 0000000..9005cd7 --- /dev/null +++ b/code/jvm/src/main/kotlin/pt/isel/ion/teams/ConfigProperties.kt @@ -0,0 +1,10 @@ +package pt.isel.ion.teams + +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.ConstructorBinding + +@ConstructorBinding +@ConfigurationProperties("app") +data class ConfigProperties( + val dbConnString: String +) \ No newline at end of file diff --git a/code/jvm/src/main/kotlin/pt/isel/ion/teams/IOnteamsServiceApplication.kt b/code/jvm/src/main/kotlin/pt/isel/ion/teams/IOnteamsServiceApplication.kt index 04c80ce..a4c9441 100644 --- a/code/jvm/src/main/kotlin/pt/isel/ion/teams/IOnteamsServiceApplication.kt +++ b/code/jvm/src/main/kotlin/pt/isel/ion/teams/IOnteamsServiceApplication.kt @@ -1,11 +1,34 @@ package pt.isel.ion.teams +import org.jdbi.v3.core.Jdbi +import org.jdbi.v3.core.kotlin.KotlinPlugin +import org.jdbi.v3.postgres.PostgresPlugin +import org.jdbi.v3.sqlobject.SqlObjectPlugin import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.context.properties.ConfigurationPropertiesScan import org.springframework.boot.runApplication +import org.springframework.context.annotation.Bean +import org.postgresql.ds.PGSimpleDataSource +import javax.sql.DataSource @SpringBootApplication -class IOnteamsServiceApplication +@ConfigurationPropertiesScan +class IOnteamsServiceApplication( + private val configProperties: ConfigProperties +) { + @Bean + fun dataSource() = PGSimpleDataSource().apply { + setURL(configProperties.dbConnString) + } + + @Bean + fun jdbi(dataSource: DataSource) = Jdbi.create(dataSource).apply { + installPlugin(KotlinPlugin()) + installPlugin(PostgresPlugin()) + installPlugin(SqlObjectPlugin()) + } +} fun main(args: Array) { - runApplication(*args) + runApplication(*args) } diff --git a/code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/OrganitazionController.kt b/code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/OrganitazionController.kt new file mode 100644 index 0000000..70603e7 --- /dev/null +++ b/code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/OrganitazionController.kt @@ -0,0 +1,15 @@ +package pt.isel.ion.teams.organizations + +import org.jdbi.v3.core.Jdbi +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("api/orgs") +class OrganizationController(val jdbi: Jdbi) { + + @GetMapping + fun getAllOrganizations() = jdbi.onDemand(OrganizationDAO::class.java).getAllOrganizations() + +} \ No newline at end of file diff --git a/code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/Organization.kt b/code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/Organization.kt new file mode 100644 index 0000000..ceb6601 --- /dev/null +++ b/code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/Organization.kt @@ -0,0 +1,7 @@ +package pt.isel.ion.teams.organizations + +data class Organization ( + val id: Int, + val name: String, + val description: String +) \ No newline at end of file diff --git a/code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/OrganizationDAO.kt b/code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/OrganizationDAO.kt new file mode 100644 index 0000000..74fb578 --- /dev/null +++ b/code/jvm/src/main/kotlin/pt/isel/ion/teams/organizations/OrganizationDAO.kt @@ -0,0 +1,8 @@ +package pt.isel.ion.teams.organizations + +import org.jdbi.v3.sqlobject.statement.SqlQuery + +interface OrganizationDAO { + @SqlQuery("SELECT * FROM organization") + fun getAllOrganizations(): List +} \ No newline at end of file diff --git a/code/jvm/src/main/resources/application.properties b/code/jvm/src/main/resources/application.properties index 8b13789..862db5a 100644 --- a/code/jvm/src/main/resources/application.properties +++ b/code/jvm/src/main/resources/application.properties @@ -1 +1 @@ - +app.dbConnString=jdbc:postgresql://localhost:5432/postgres?user=postgres&password=${POSTGRES_PASSWORD} \ No newline at end of file diff --git a/code/sql/.idea/.gitignore b/code/sql/.idea/.gitignore new file mode 100644 index 0000000..8bf4d45 --- /dev/null +++ b/code/sql/.idea/.gitignore @@ -0,0 +1,6 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/code/sql/.idea/dataSources.xml b/code/sql/.idea/dataSources.xml new file mode 100644 index 0000000..73b8e5e --- /dev/null +++ b/code/sql/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + postgresql + true + org.postgresql.Driver + jdbc:postgresql://localhost:5432/postgres + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/code/sql/.idea/modules.xml b/code/sql/.idea/modules.xml new file mode 100644 index 0000000..b02178e --- /dev/null +++ b/code/sql/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/code/sql/.idea/sql.iml b/code/sql/.idea/sql.iml new file mode 100644 index 0000000..0399c4b --- /dev/null +++ b/code/sql/.idea/sql.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/code/sql/.idea/vcs.xml b/code/sql/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/code/sql/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/code/sql/createTables.sql b/code/sql/createTables.sql index 99771d2..a4efa61 100644 --- a/code/sql/createTables.sql +++ b/code/sql/createTables.sql @@ -1,6 +1,7 @@ CREATE TABLE ORGANIZATION ( id serial, name varchar(50), + description varchar(256), PRIMARY KEY (id) );