Add user and notes services

This commit is contained in:
Hubert Van De Walle 2020-04-20 15:41:26 +02:00
parent fce1cc0e9c
commit 8dd4fc9f4d
5 changed files with 91 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import be.vandewalleh.features.configurationFeature
import be.vandewalleh.features.configurationModule import be.vandewalleh.features.configurationModule
import be.vandewalleh.features.features import be.vandewalleh.features.features
import be.vandewalleh.migrations.Migration import be.vandewalleh.migrations.Migration
import be.vandewalleh.services.serviceModule
import io.ktor.application.Application import io.ktor.application.Application
import io.ktor.application.log import io.ktor.application.log
import io.ktor.auth.authenticate import io.ktor.auth.authenticate
@ -29,6 +30,7 @@ fun Application.module() {
kodein = Kodein { kodein = Kodein {
import(controllerModule) import(controllerModule)
import(configurationModule) import(configurationModule)
import(serviceModule)
bind<Feature>() with singleton { Migration(this.kodein) } bind<Feature>() with singleton { Migration(this.kodein) }
bind<Database>() with singleton { Database.Companion.connect(this.instance<DataSource>()) } bind<Database>() with singleton { Database.Companion.connect(this.instance<DataSource>()) }

View File

@ -76,12 +76,11 @@ class ChaptersController(kodein: Kodein) : AuthCrudController("/notes/{noteTitle
val chapterNumber = call.chapterNumber() val chapterNumber = call.chapterNumber()
?:return@post call.respondStatus(HttpStatusCode.BadRequest) ?:return@post call.respondStatus(HttpStatusCode.BadRequest)
val exists = val exists = false
val (title, content) = call.receive<PostRequestBody>() val (title, content) = call.receive<PostRequestBody>()
if()
} }
} }

View File

@ -0,0 +1,47 @@
package be.vandewalleh.services
import be.vandewalleh.tables.Notes
import be.vandewalleh.tables.Tags
import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.dsl.*
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.generic.instance
import java.time.format.DateTimeFormatter
/**
* service to handle database queries at the Notes level.
*/
class NotesService(override val kodein: Kodein) : KodeinAware {
val db by instance<Database>()
/**
* returns a list of [NotesDTO] associated with the userId
*/
fun getNotes(userId: Int): List<NotesDTO> {
val notes = db.from(Notes)
.select(Notes.id, Notes.title, Notes.updatedAt)
.where { Notes.userId eq userId }
.orderBy(Notes.updatedAt.desc())
.map { row ->
Notes.createEntity(row)
}
.toList()
return notes.map { note ->
val tags = db.from(Tags)
.select(Tags.name)
.where { Tags.noteId eq note.id }
.map { it[Tags.name]!! }
.toList()
val updatedAt = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(note.updatedAt)
NotesDTO(note.title, tags, updatedAt)
}
}
}
data class NotesDTO(val title: String, val tags: List<String>, val updatedAt: String)

View File

@ -0,0 +1,13 @@
package be.vandewalleh.services
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.singleton
/**
* [Kodein] controller module containing the app services
*/
val serviceModule = Kodein.Module(name = "Services") {
bind<NotesService>() with singleton { NotesService(this.kodein) }
}

View File

@ -0,0 +1,28 @@
package be.vandewalleh.services
import be.vandewalleh.tables.Users
import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.dsl.*
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.generic.instance
/**
* service to handle database queries for users.
*/
class UserService(override val kodein: Kodein) : KodeinAware {
private val db by instance<Database>()
/**
* returns a user ID if present or null
*/
fun getUserId(userEmail: String): Int? {
return db.from(Users)
.select(Users.id)
.where { Users.email eq userEmail }
.limit(0, 1)
.map { it[Users.id] }
.firstOrNull()
}
}