Merge branch 'feature/note-creation-backend'
This commit is contained in:
commit
0b0261aaaa
@ -14,4 +14,5 @@ val controllerModule = Kodein.Module(name = "Controller") {
|
|||||||
|
|
||||||
bind<KodeinController>().inSet() with singleton { UserController(this.kodein) }
|
bind<KodeinController>().inSet() with singleton { UserController(this.kodein) }
|
||||||
bind<KodeinController>().inSet() with singleton { HealthCheckController(this.kodein) }
|
bind<KodeinController>().inSet() with singleton { HealthCheckController(this.kodein) }
|
||||||
|
bind<KodeinController>().inSet() with singleton { NoteController(this.kodein) }
|
||||||
}
|
}
|
||||||
71
api/src/controllers/NoteController.kt
Normal file
71
api/src/controllers/NoteController.kt
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package be.vandewalleh.controllers
|
||||||
|
|
||||||
|
import be.vandewalleh.entities.Note
|
||||||
|
import be.vandewalleh.entities.Tag
|
||||||
|
import be.vandewalleh.errors.ApiError
|
||||||
|
import be.vandewalleh.tables.Notes
|
||||||
|
import be.vandewalleh.tables.Tags
|
||||||
|
import be.vandewalleh.tables.Users
|
||||||
|
import io.ktor.application.call
|
||||||
|
import io.ktor.auth.UserIdPrincipal
|
||||||
|
import io.ktor.auth.authenticate
|
||||||
|
import io.ktor.auth.principal
|
||||||
|
import io.ktor.http.HttpStatusCode
|
||||||
|
import io.ktor.locations.Location
|
||||||
|
import io.ktor.locations.post
|
||||||
|
import io.ktor.request.receive
|
||||||
|
import io.ktor.response.respond
|
||||||
|
import io.ktor.routing.Routing
|
||||||
|
import me.liuwj.ktorm.database.Database
|
||||||
|
import me.liuwj.ktorm.dsl.eq
|
||||||
|
import me.liuwj.ktorm.entity.add
|
||||||
|
import me.liuwj.ktorm.entity.find
|
||||||
|
import me.liuwj.ktorm.entity.sequenceOf
|
||||||
|
import org.kodein.di.Kodein
|
||||||
|
import org.kodein.di.generic.instance
|
||||||
|
|
||||||
|
class NoteController(kodein: Kodein) : KodeinController(kodein) {
|
||||||
|
private val db by instance<Database>()
|
||||||
|
|
||||||
|
override fun Routing.registerRoutes() {
|
||||||
|
authenticate {
|
||||||
|
post<Routes.Notes> {
|
||||||
|
data class Response(val message: String)
|
||||||
|
|
||||||
|
val (email) = call.principal<UserIdPrincipal>()!!
|
||||||
|
val body = call.receive<PostNotesBody>()
|
||||||
|
|
||||||
|
val user = db.sequenceOf(Users)
|
||||||
|
.find { Users.email eq email }
|
||||||
|
?: return@post call.respond(HttpStatusCode.Forbidden, ApiError.DeletedUserError)
|
||||||
|
|
||||||
|
|
||||||
|
val note = Note {
|
||||||
|
this.title = body.title
|
||||||
|
this.content = body.content
|
||||||
|
this.user = user
|
||||||
|
}
|
||||||
|
|
||||||
|
db.sequenceOf(Notes).add(note)
|
||||||
|
|
||||||
|
body.tags.forEach { tagName ->
|
||||||
|
val tag = Tag {
|
||||||
|
this.name = tagName
|
||||||
|
this.note = note
|
||||||
|
}
|
||||||
|
db.sequenceOf(Tags).add(tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
return@post call.respond(Response("created"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
object Routes {
|
||||||
|
@Location("/notes")
|
||||||
|
class Notes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data class PostNotesBody(val title: String, val content: String, val tags: List<String>)
|
||||||
@ -3,4 +3,5 @@ package be.vandewalleh.errors
|
|||||||
sealed class ApiError(val message: String){
|
sealed class ApiError(val message: String){
|
||||||
object InvalidCredentialError : ApiError("Invalid credentials")
|
object InvalidCredentialError : ApiError("Invalid credentials")
|
||||||
object ExistingUserError : ApiError("User already exists")
|
object ExistingUserError : ApiError("User already exists")
|
||||||
|
object DeletedUserError : ApiError("User has been deleted")
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user