#57 I can filter articles by workgroup
update lib "postgres-json:1.2.1"
This commit is contained in:
@@ -8,7 +8,7 @@ import fr.postgresjson.entity.mutable.UuidEntityVersioning
|
||||
import java.util.*
|
||||
|
||||
class Article(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
id: UUID? = null,
|
||||
title: String,
|
||||
override var anonymous: Boolean = true,
|
||||
override var content: String,
|
||||
@@ -27,8 +27,22 @@ class Article(
|
||||
}
|
||||
}
|
||||
|
||||
class ArticleForUpdate(
|
||||
id: UUID?,
|
||||
val title: String,
|
||||
val anonymous: Boolean = true,
|
||||
val content: String,
|
||||
val description: String,
|
||||
tags: List<String> = emptyList(),
|
||||
val draft: Boolean = false,
|
||||
val createdBy: CitizenRef,
|
||||
val workgroup: WorkgroupRef? = null
|
||||
) : ArticleRefVersioning(id) {
|
||||
val tags: List<String> = tags.distinct()
|
||||
}
|
||||
|
||||
open class ArticleSimple(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
id: UUID? = null,
|
||||
override var title: String,
|
||||
override val createdBy: CitizenBasic,
|
||||
override var draft: Boolean = false,
|
||||
@@ -43,14 +57,14 @@ open class ArticleSimple(
|
||||
Opinionable by OpinionableImp()
|
||||
|
||||
open class ArticleRefVersioning(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
id: UUID? = null,
|
||||
versionNumber: Int? = null,
|
||||
versionId: UUID = UUID.randomUUID()
|
||||
) : ArticleRef(id),
|
||||
EntityVersioning<UUID, Int> by UuidEntityVersioning(versionNumber, versionId)
|
||||
|
||||
open class ArticleRef(
|
||||
id: UUID = UUID.randomUUID()
|
||||
id: UUID? = null
|
||||
) : ArticleI, TargetRef(id)
|
||||
|
||||
interface ArticleI : UuidEntityI, TargetI
|
||||
|
||||
@@ -3,6 +3,8 @@ package fr.dcproject.entity
|
||||
import fr.postgresjson.entity.immutable.*
|
||||
import fr.postgresjson.entity.mutable.EntityDeletedAt
|
||||
import fr.postgresjson.entity.mutable.EntityDeletedAtImp
|
||||
import fr.postgresjson.entity.mutable.EntityVersioning
|
||||
import fr.postgresjson.entity.mutable.UuidEntityVersioning
|
||||
import java.util.*
|
||||
|
||||
class Constitution(
|
||||
@@ -41,7 +43,7 @@ open class ConstitutionSimple<Cr : CitizenRefWithUser, T : ConstitutionSimple.Ti
|
||||
override val createdBy: Cr,
|
||||
versionId: UUID = UUID.randomUUID()
|
||||
) : ConstitutionRef(id),
|
||||
EntityVersioning<UUID, Int?> by UuidEntityVersioning(versionId = versionId),
|
||||
EntityVersioning<UUID, Int> by UuidEntityVersioning(versionId = versionId),
|
||||
EntityCreatedAt by EntityCreatedAtImp(),
|
||||
EntityCreatedBy<Cr> by EntityCreatedByImp(createdBy),
|
||||
EntityDeletedAt by EntityDeletedAtImp() {
|
||||
|
||||
@@ -15,7 +15,7 @@ interface ExtraI<T : TargetI, C : CitizenI> :
|
||||
val target: T
|
||||
}
|
||||
|
||||
open class TargetRef(id: UUID = UUID.randomUUID(), reference: String = "") : TargetI, UuidEntity(id) {
|
||||
open class TargetRef(id: UUID? = null, reference: String = "") : TargetI, UuidEntity(id) {
|
||||
|
||||
final override val reference: String
|
||||
get() = if (field != "") field else TargetI.getReference(this)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package fr.dcproject.repository
|
||||
|
||||
import fr.dcproject.entity.ArticleFull
|
||||
import fr.dcproject.entity.ArticleForUpdate
|
||||
import fr.dcproject.entity.ArticleSimple
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.connexion.Requester
|
||||
@@ -42,13 +42,14 @@ class Article(override var requester: Requester) : RepositoryI {
|
||||
)
|
||||
}
|
||||
|
||||
fun upsert(article: ArticleFull): ArticleEntity? {
|
||||
fun upsert(article: ArticleForUpdate): ArticleEntity? {
|
||||
return requester
|
||||
.getFunction("upsert_article")
|
||||
.selectOne("resource" to article)
|
||||
}
|
||||
|
||||
class Filter(
|
||||
val createdById: String? = null
|
||||
val createdById: String? = null,
|
||||
val workgroupId: String? = null
|
||||
) : Parameter
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package fr.dcproject.routes
|
||||
|
||||
import fr.dcproject.citizen
|
||||
import fr.dcproject.citizenOrNull
|
||||
import fr.dcproject.entity.ArticleForUpdate
|
||||
import fr.dcproject.entity.CitizenRef
|
||||
import fr.dcproject.entity.WorkgroupRef
|
||||
import fr.dcproject.entity.WorkgroupSimple
|
||||
@@ -39,7 +40,8 @@ object ArticlesPaths {
|
||||
val sort: String? = null,
|
||||
val direction: RepositoryI.Direction? = null,
|
||||
val search: String? = null,
|
||||
val createdBy: String? = null
|
||||
val createdBy: String? = null,
|
||||
val workgroup: String? = null
|
||||
) {
|
||||
val page: Int = if (page < 1) 1 else page
|
||||
val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit
|
||||
@@ -62,7 +64,7 @@ object ArticlesPaths {
|
||||
}
|
||||
|
||||
@Location("/articles")
|
||||
class PostArticleRequest: KoinComponent {
|
||||
class PostArticleRequest : KoinComponent {
|
||||
class Article(
|
||||
val id: UUID?,
|
||||
val title: String,
|
||||
@@ -77,8 +79,8 @@ object ArticlesPaths {
|
||||
|
||||
private val workgroupRepository: WorkgroupRepository by inject()
|
||||
|
||||
suspend fun getNewArticle(call: ApplicationCall): ArticleEntity = call.receive<Article>().run {
|
||||
ArticleEntity(
|
||||
suspend fun getNewArticle(call: ApplicationCall): ArticleForUpdate = call.receive<Article>().run {
|
||||
ArticleForUpdate(
|
||||
id ?: UUID.randomUUID(),
|
||||
title,
|
||||
anonymous,
|
||||
@@ -98,8 +100,14 @@ object ArticlesPaths {
|
||||
@KtorExperimentalLocationsAPI
|
||||
fun Route.article(repo: ArticleRepository, viewManager: ArticleViewManager) {
|
||||
get<ArticlesPaths.ArticlesRequest> {
|
||||
val articles =
|
||||
repo.find(it.page, it.limit, it.sort, it.direction, it.search, Filter(createdById = it.createdBy))
|
||||
val articles = repo.find(
|
||||
it.page,
|
||||
it.limit,
|
||||
it.sort,
|
||||
it.direction,
|
||||
it.search,
|
||||
Filter(createdById = it.createdBy, workgroupId = it.workgroup)
|
||||
)
|
||||
assertCan(VIEW, articles.result)
|
||||
call.respond(articles)
|
||||
}
|
||||
@@ -127,9 +135,9 @@ fun Route.article(repo: ArticleRepository, viewManager: ArticleViewManager) {
|
||||
post<ArticlesPaths.PostArticleRequest> {
|
||||
it.getNewArticle(call).also { article ->
|
||||
assertCan(CREATE, article)
|
||||
repo.upsert(article)
|
||||
val newArticle = repo.upsert(article) ?: error("Article not updated")
|
||||
call.respond(article)
|
||||
raiseEvent(ArticleUpdate.event, ArticleUpdate(article))
|
||||
raiseEvent(ArticleUpdate.event, ArticleUpdate(newArticle))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,6 +218,14 @@ paths:
|
||||
- $ref: '#/components/parameters/direction'
|
||||
- $ref: '#/components/parameters/search'
|
||||
- $ref: '#/components/parameters/createdBy'
|
||||
- name: workgroup
|
||||
in: query
|
||||
description: ID of workgroup
|
||||
example: 82a0e60a-bb55-dbc0-1c3d-0a804df2b5df
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
format: uuid
|
||||
responses:
|
||||
200:
|
||||
description: The Article objects
|
||||
|
||||
@@ -15,6 +15,7 @@ begin
|
||||
from article a
|
||||
where (_search is null or _search = '' or a ==> dsl.multi_match('{title^3, content, description, tags}', _search))
|
||||
and (_filter->>'created_by_id' is null or a.created_by_id = (_filter->>'created_by_id')::uuid)
|
||||
and (_filter->>'workgroup_id' is null or a.workgroup_id = (_filter->>'workgroup_id')::uuid)
|
||||
and a.last_version = true
|
||||
)
|
||||
into resource, total
|
||||
@@ -33,6 +34,7 @@ begin
|
||||
or a ==> dsl.multi_match('{title^3, content, description, tags}', _search)
|
||||
)
|
||||
and (_filter->>'created_by_id' is null or a.created_by_id = (_filter->>'created_by_id')::uuid)
|
||||
and (_filter->>'workgroup_id' is null or a.workgroup_id = (_filter->>'workgroup_id')::uuid)
|
||||
and a.last_version = true
|
||||
|
||||
order by
|
||||
|
||||
Reference in New Issue
Block a user