@@ -1,13 +1,20 @@
|
||||
package fr.dcproject
|
||||
|
||||
import com.fasterxml.jackson.core.util.DefaultIndenter
|
||||
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy
|
||||
import com.fasterxml.jackson.databind.SerializationFeature
|
||||
import com.fasterxml.jackson.datatype.joda.JodaModule
|
||||
import fr.dcproject.entity.Article
|
||||
import fr.dcproject.routes.article
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.application.install
|
||||
import io.ktor.auth.Authentication
|
||||
import io.ktor.features.AutoHeadResponse
|
||||
import io.ktor.features.ContentNegotiation
|
||||
import io.ktor.features.DataConversion
|
||||
import io.ktor.gson.gson
|
||||
import io.ktor.jackson.jackson
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Locations
|
||||
import io.ktor.routing.Routing
|
||||
@@ -57,12 +64,25 @@ fun Application.module() {
|
||||
install(Authentication) {
|
||||
}
|
||||
|
||||
install(AutoHeadResponse)
|
||||
|
||||
install(ContentNegotiation) {
|
||||
gson {
|
||||
// TODO move to postgresJson lib
|
||||
jackson {
|
||||
propertyNamingStrategy = PropertyNamingStrategy.SNAKE_CASE
|
||||
|
||||
registerModule(JodaModule())
|
||||
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
||||
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
configure(SerializationFeature.INDENT_OUTPUT, true)
|
||||
setDefaultPrettyPrinter(DefaultPrettyPrinter().apply {
|
||||
indentArraysWith(DefaultPrettyPrinter.FixedSpaceIndenter.instance)
|
||||
indentObjectsWith(DefaultIndenter(" ", "\n"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
install(Routing) {
|
||||
article()
|
||||
article(get())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ import java.util.*
|
||||
|
||||
|
||||
class Article(
|
||||
id: UUID,
|
||||
var versionId: UUID,
|
||||
var versionNumber: Int,
|
||||
var title: String,
|
||||
var annonymous: Boolean,
|
||||
var content: String,
|
||||
var description: String,
|
||||
id: UUID?,
|
||||
var versionId: UUID?,
|
||||
var versionNumber: Int?,
|
||||
var title: String?,
|
||||
var annonymous: Boolean?,
|
||||
var content: String?,
|
||||
var description: String?,
|
||||
var tags: List<String>
|
||||
):
|
||||
UuidEntity(id),
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.util.*
|
||||
|
||||
class User(
|
||||
id: UUID?,
|
||||
var username: String,
|
||||
var username: String?,
|
||||
var blockedAt: DateTime?,
|
||||
override var createdAt: DateTime?,
|
||||
override var updatedAt: DateTime?
|
||||
|
||||
@@ -4,11 +4,10 @@ import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.EntitiesCollections
|
||||
import fr.postgresjson.repository.RepositoryI
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
import fr.dcproject.entity.Article as ArticleEntity
|
||||
|
||||
class Article(override var requester: Requester) : RepositoryI<ArticleEntity> {
|
||||
override val entityName: KClass<fr.dcproject.entity.Article> = ArticleEntity::class
|
||||
override val entityName = ArticleEntity::class
|
||||
|
||||
fun findById(id: UUID): ArticleEntity? {
|
||||
val function = requester.getFunction("find_article_by_id")
|
||||
@@ -19,4 +18,12 @@ class Article(override var requester: Requester) : RepositoryI<ArticleEntity> {
|
||||
else -> e
|
||||
}
|
||||
}
|
||||
|
||||
fun upsert(article: ArticleEntity): ArticleEntity? {
|
||||
return requester
|
||||
.getFunction("upsert_article")
|
||||
.selectOne<ArticleEntity>("resource" to article)?.also {
|
||||
EntitiesCollections().set(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,15 +5,25 @@ import fr.postgresjson.serializer.serialize
|
||||
import io.ktor.application.call
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.get
|
||||
import io.ktor.locations.post
|
||||
import io.ktor.request.receive
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.response.respondText
|
||||
import io.ktor.routing.Route
|
||||
import fr.dcproject.entity.Article as ArticleEntity
|
||||
import fr.dcproject.repository.Article as ArticleRepository
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
fun Route.article() {
|
||||
fun Route.article(repo: ArticleRepository) {
|
||||
get<Paths.ArticlesRequest> {
|
||||
call.respondText("todo")
|
||||
}
|
||||
get<Paths.ArticleRequest> {
|
||||
call.respondText(it.article.serialize())
|
||||
}
|
||||
post<Paths.PostArticleRequest>() {
|
||||
val article = call.receive<ArticleEntity>()
|
||||
repo.upsert(article)
|
||||
call.respond(article)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user