Test openapi schema of /articles/{article}/comments
This commit is contained in:
@@ -4,8 +4,9 @@ import fr.dcproject.common.entity.EntityI
|
||||
import fr.dcproject.common.entity.TargetI
|
||||
import fr.dcproject.component.article.database.ArticleForView
|
||||
import fr.dcproject.component.article.database.ArticleRef
|
||||
import fr.dcproject.component.citizen.database.CitizenCreator
|
||||
import fr.dcproject.component.citizen.database.CitizenCreatorI
|
||||
import fr.dcproject.component.citizen.database.CitizenI
|
||||
import fr.dcproject.component.citizen.database.CitizenRef
|
||||
import fr.dcproject.component.comment.generic.database.CommentForView
|
||||
import fr.dcproject.component.comment.generic.database.CommentRepositoryAbs
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
@@ -13,25 +14,26 @@ import fr.postgresjson.connexion.Requester
|
||||
import java.util.UUID
|
||||
|
||||
class CommentArticleRepository(requester: Requester) : CommentRepositoryAbs<ArticleForView>(requester) {
|
||||
override fun findById(id: UUID): CommentForView<ArticleForView, CitizenRef>? {
|
||||
override fun findById(id: UUID): CommentForView<ArticleForView, CitizenCreatorI>? {
|
||||
return requester
|
||||
.getFunction("find_comment_by_id")
|
||||
.selectOne(mapOf("id" to id))
|
||||
.selectOne<CommentForView<ArticleForView, CitizenCreator>>(mapOf("id" to id))
|
||||
as CommentForView<ArticleForView, CitizenCreatorI>?
|
||||
}
|
||||
|
||||
override fun findByCitizen(
|
||||
citizen: CitizenI,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<CommentForView<ArticleForView, CitizenRef>> {
|
||||
): Paginated<CommentForView<ArticleForView, CitizenCreatorI>> {
|
||||
return requester.run {
|
||||
getFunction("find_comments_by_citizen")
|
||||
.select(
|
||||
.select<CommentForView<ArticleForView, CitizenCreator>>(
|
||||
page,
|
||||
limit,
|
||||
"created_by_id" to citizen.id,
|
||||
"reference" to TargetI.getReference(ArticleRef::class)
|
||||
)
|
||||
) as Paginated<CommentForView<ArticleForView, CitizenCreatorI>>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,14 +42,16 @@ class CommentArticleRepository(requester: Requester) : CommentRepositoryAbs<Arti
|
||||
page: Int,
|
||||
limit: Int,
|
||||
sort: Sort
|
||||
): Paginated<CommentForView<ArticleForView, CitizenRef>> = requester
|
||||
.getFunction("find_comments_by_target")
|
||||
.select(
|
||||
page,
|
||||
limit,
|
||||
"target_id" to target.id,
|
||||
"sort" to sort.sql
|
||||
)
|
||||
): Paginated<CommentForView<ArticleForView, CitizenCreatorI>> {
|
||||
return requester
|
||||
.getFunction("find_comments_by_target")
|
||||
.select<CommentForView<ArticleForView, CitizenCreator>>(
|
||||
page,
|
||||
limit,
|
||||
"target_id" to target.id,
|
||||
"sort" to sort.sql
|
||||
) as Paginated<CommentForView<ArticleForView, CitizenCreatorI>>
|
||||
}
|
||||
|
||||
enum class Sort(val sql: String) {
|
||||
CREATED_AT("created_at"),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package fr.dcproject.component.comment.article.routes
|
||||
|
||||
import fr.dcproject.common.dto.toOutput
|
||||
import fr.dcproject.common.security.assert
|
||||
import fr.dcproject.component.article.database.ArticleRef
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
@@ -14,6 +15,7 @@ import io.ktor.locations.Location
|
||||
import io.ktor.locations.get
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
import org.joda.time.DateTime
|
||||
import java.util.UUID
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@@ -32,11 +34,55 @@ object GetArticleComments {
|
||||
|
||||
fun Route.getArticleComments(repo: CommentArticleRepository, ac: CommentAccessControl) {
|
||||
get<ArticleCommentsRequest> {
|
||||
val comment = repo.findByTarget(it.article, it.page, it.limit, it.sort)
|
||||
if (comment.result.isNotEmpty()) {
|
||||
ac.assert { canView(comment.result, citizenOrNull) }
|
||||
val comments = repo.findByTarget(it.article, it.page, it.limit, it.sort)
|
||||
if (comments.result.isNotEmpty()) {
|
||||
ac.assert { canView(comments.result, citizenOrNull) }
|
||||
}
|
||||
call.respond(HttpStatusCode.OK, comment)
|
||||
call.respond(
|
||||
HttpStatusCode.OK,
|
||||
comments.toOutput { comment ->
|
||||
object {
|
||||
val id: UUID = comment.id
|
||||
val content: String = comment.content
|
||||
val createdAt: DateTime = comment.createdAt
|
||||
val parent: Any? = comment.parent?.let { p ->
|
||||
object {
|
||||
val id: UUID = p.id
|
||||
val reference: String = p.reference
|
||||
}
|
||||
}
|
||||
val target: Any = comment.target.let { t ->
|
||||
object {
|
||||
val id: UUID = t.id
|
||||
val reference: String = t.reference
|
||||
}
|
||||
}
|
||||
val createdBy: Any = comment.createdBy.let { c ->
|
||||
object {
|
||||
val id: UUID = c.id
|
||||
val name: Any = c.name.let { n ->
|
||||
object {
|
||||
val firstName: String = n.firstName
|
||||
val lastName: String = n.lastName
|
||||
}
|
||||
}
|
||||
val user: Any = c.user.let { u ->
|
||||
object {
|
||||
val username: String = u.username
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val votes: Any = object {
|
||||
val up: Int = 0
|
||||
val neutral: Int = 0
|
||||
val down: Int = 0
|
||||
val total: Int = 0
|
||||
val score: Int = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ package fr.dcproject.component.comment.constitution.database
|
||||
|
||||
import fr.dcproject.common.entity.EntityI
|
||||
import fr.dcproject.common.entity.TargetI
|
||||
import fr.dcproject.component.citizen.database.CitizenCreatorI
|
||||
import fr.dcproject.component.citizen.database.CitizenI
|
||||
import fr.dcproject.component.citizen.database.CitizenRef
|
||||
import fr.dcproject.component.comment.article.database.CommentArticleRepository
|
||||
import fr.dcproject.component.comment.generic.database.CommentForView
|
||||
import fr.dcproject.component.comment.generic.database.CommentRepositoryAbs
|
||||
@@ -13,7 +13,7 @@ import fr.postgresjson.connexion.Requester
|
||||
import java.util.UUID
|
||||
|
||||
class CommentConstitutionRepository(requester: Requester) : CommentRepositoryAbs<ConstitutionRef>(requester) {
|
||||
override fun findById(id: UUID): CommentForView<ConstitutionRef, CitizenRef>? {
|
||||
override fun findById(id: UUID): CommentForView<ConstitutionRef, CitizenCreatorI>? {
|
||||
return requester
|
||||
.getFunction("find_comment_by_id")
|
||||
.selectOne(mapOf("id" to id))
|
||||
@@ -23,7 +23,7 @@ class CommentConstitutionRepository(requester: Requester) : CommentRepositoryAbs
|
||||
citizen: CitizenI,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<CommentForView<ConstitutionRef, CitizenRef>> {
|
||||
): Paginated<CommentForView<ConstitutionRef, CitizenCreatorI>> {
|
||||
return requester.run {
|
||||
getFunction("find_comments_by_citizen")
|
||||
.select(
|
||||
@@ -40,7 +40,7 @@ class CommentConstitutionRepository(requester: Requester) : CommentRepositoryAbs
|
||||
page: Int,
|
||||
limit: Int,
|
||||
sort: CommentArticleRepository.Sort
|
||||
): Paginated<CommentForView<ConstitutionRef, CitizenRef>> {
|
||||
): Paginated<CommentForView<ConstitutionRef, CitizenCreatorI>> {
|
||||
return requester.run {
|
||||
getFunction("find_comments_by_target")
|
||||
.select(
|
||||
|
||||
@@ -9,13 +9,14 @@ import fr.dcproject.common.entity.HasTarget
|
||||
import fr.dcproject.common.entity.TargetI
|
||||
import fr.dcproject.common.entity.TargetRef
|
||||
import fr.dcproject.common.entity.UpdatedAt
|
||||
import fr.dcproject.component.citizen.database.CitizenRef
|
||||
import fr.dcproject.component.citizen.database.CitizenCreatorI
|
||||
import fr.dcproject.component.citizen.database.CitizenI
|
||||
import fr.dcproject.component.vote.entity.Votable
|
||||
import fr.dcproject.component.vote.entity.VotableImp
|
||||
import org.joda.time.DateTime
|
||||
import java.util.UUID
|
||||
|
||||
class CommentForView<T : TargetI, C : CitizenRef>(
|
||||
class CommentForView<T : TargetI, C : CitizenCreatorI>(
|
||||
id: UUID = UUID.randomUUID(),
|
||||
override val createdBy: C,
|
||||
override val target: T,
|
||||
@@ -44,7 +45,7 @@ class CommentForView<T : TargetI, C : CitizenRef>(
|
||||
)
|
||||
}
|
||||
|
||||
open class CommentForUpdate<T : TargetI, C : CitizenRef>(
|
||||
open class CommentForUpdate<T : TargetI, C : CitizenI>(
|
||||
override val id: UUID = UUID.randomUUID(),
|
||||
override val createdBy: C,
|
||||
override val target: T,
|
||||
|
||||
@@ -3,8 +3,9 @@ package fr.dcproject.component.comment.generic.database
|
||||
import fr.dcproject.common.entity.EntityI
|
||||
import fr.dcproject.common.entity.TargetI
|
||||
import fr.dcproject.common.entity.TargetRef
|
||||
import fr.dcproject.component.citizen.database.CitizenCreator
|
||||
import fr.dcproject.component.citizen.database.CitizenCreatorI
|
||||
import fr.dcproject.component.citizen.database.CitizenI
|
||||
import fr.dcproject.component.citizen.database.CitizenRef
|
||||
import fr.dcproject.component.comment.article.database.CommentArticleRepository
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.connexion.Requester
|
||||
@@ -12,19 +13,19 @@ import fr.postgresjson.repository.RepositoryI
|
||||
import java.util.UUID
|
||||
|
||||
abstract class CommentRepositoryAbs<T : TargetI>(override var requester: Requester) : RepositoryI {
|
||||
abstract fun findById(id: UUID): CommentForView<T, CitizenRef>?
|
||||
abstract fun findById(id: UUID): CommentForView<T, CitizenCreatorI>?
|
||||
|
||||
abstract fun findByCitizen(
|
||||
citizen: CitizenI,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<CommentForView<T, CitizenRef>>
|
||||
): Paginated<CommentForView<T, CitizenCreatorI>>
|
||||
|
||||
open fun findByParent(
|
||||
parent: CommentForView<T, CitizenRef>,
|
||||
parent: CommentForView<T, CitizenCreatorI>,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<CommentForView<T, CitizenRef>> {
|
||||
): Paginated<CommentForView<T, CitizenCreatorI>> {
|
||||
return findByParent(parent.id, page, limit)
|
||||
}
|
||||
|
||||
@@ -32,14 +33,15 @@ abstract class CommentRepositoryAbs<T : TargetI>(override var requester: Request
|
||||
parentId: UUID,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<CommentForView<T, CitizenRef>> {
|
||||
): Paginated<CommentForView<T, CitizenCreatorI>> {
|
||||
return requester.run {
|
||||
getFunction("find_comments_by_parent")
|
||||
.select(
|
||||
.select<CommentForView<T, CitizenCreator>>(
|
||||
page,
|
||||
limit,
|
||||
"parent_id" to parentId
|
||||
)
|
||||
as Paginated<CommentForView<T, CitizenCreatorI>>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +50,7 @@ abstract class CommentRepositoryAbs<T : TargetI>(override var requester: Request
|
||||
page: Int = 1,
|
||||
limit: Int = 50,
|
||||
sort: CommentArticleRepository.Sort = CommentArticleRepository.Sort.CREATED_AT
|
||||
): Paginated<CommentForView<T, CitizenRef>> {
|
||||
): Paginated<CommentForView<T, CitizenCreatorI>> {
|
||||
return findByTarget(target.id, page, limit, sort)
|
||||
}
|
||||
|
||||
@@ -57,19 +59,20 @@ abstract class CommentRepositoryAbs<T : TargetI>(override var requester: Request
|
||||
page: Int = 1,
|
||||
limit: Int = 50,
|
||||
sort: CommentArticleRepository.Sort = CommentArticleRepository.Sort.CREATED_AT
|
||||
): Paginated<CommentForView<T, CitizenRef>> {
|
||||
): Paginated<CommentForView<T, CitizenCreatorI>> {
|
||||
return requester.run {
|
||||
getFunction("find_comments_by_target")
|
||||
.select(
|
||||
.select<CommentForView<T, CitizenCreator>>(
|
||||
page,
|
||||
limit,
|
||||
"target_id" to targetId,
|
||||
"sort" to sort.sql
|
||||
)
|
||||
as Paginated<CommentForView<T, CitizenCreatorI>>
|
||||
}
|
||||
}
|
||||
|
||||
fun <I : TargetI, C : CitizenRef> comment(comment: CommentForUpdate<I, C>) {
|
||||
fun <I : TargetI, C : CitizenCreatorI> comment(comment: CommentForUpdate<I, C>) {
|
||||
requester
|
||||
.getFunction("comment")
|
||||
.sendQuery(
|
||||
@@ -78,7 +81,7 @@ abstract class CommentRepositoryAbs<T : TargetI>(override var requester: Request
|
||||
)
|
||||
}
|
||||
|
||||
fun <I : T> edit(comment: CommentForUpdate<I, CitizenRef>) {
|
||||
fun <I : T> edit(comment: CommentForUpdate<I, CitizenCreatorI>) {
|
||||
requester
|
||||
.getFunction("edit_comment")
|
||||
.sendQuery(
|
||||
@@ -89,24 +92,25 @@ abstract class CommentRepositoryAbs<T : TargetI>(override var requester: Request
|
||||
}
|
||||
|
||||
class CommentRepository(requester: Requester) : CommentRepositoryAbs<TargetRef>(requester) {
|
||||
override fun findById(id: UUID): CommentForView<TargetRef, CitizenRef>? {
|
||||
override fun findById(id: UUID): CommentForView<TargetRef, CitizenCreatorI>? {
|
||||
return requester
|
||||
.getFunction("find_comment_by_id")
|
||||
.selectOne(mapOf("id" to id))
|
||||
.selectOne<CommentForView<TargetRef, CitizenCreator>>(mapOf("id" to id))
|
||||
as CommentForView<TargetRef, CitizenCreatorI>?
|
||||
}
|
||||
|
||||
override fun findByCitizen(
|
||||
citizen: CitizenI,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<CommentForView<TargetRef, CitizenRef>> {
|
||||
): Paginated<CommentForView<TargetRef, CitizenCreatorI>> {
|
||||
return requester.run {
|
||||
getFunction("find_comments_by_citizen")
|
||||
.select(
|
||||
.select<CommentForView<TargetRef, CitizenCreator>>(
|
||||
page,
|
||||
limit,
|
||||
"created_by_id" to citizen.id
|
||||
)
|
||||
) as Paginated<CommentForView<TargetRef, CitizenCreatorI>>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,14 +118,15 @@ class CommentRepository(requester: Requester) : CommentRepositoryAbs<TargetRef>(
|
||||
parentId: UUID,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<CommentForView<TargetRef, CitizenRef>> {
|
||||
): Paginated<CommentForView<TargetRef, CitizenCreatorI>> {
|
||||
return requester.run {
|
||||
getFunction("find_comments_by_parent")
|
||||
.select(
|
||||
.select<CommentForView<TargetRef, CitizenCreator>>(
|
||||
page,
|
||||
limit,
|
||||
"parent_id" to parentId
|
||||
)
|
||||
as Paginated<CommentForView<TargetRef, CitizenCreatorI>>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference
|
||||
import fr.dcproject.common.entity.TargetI
|
||||
import fr.dcproject.common.entity.TargetRef
|
||||
import fr.dcproject.component.article.database.ArticleForView
|
||||
import fr.dcproject.component.citizen.database.CitizenCreatorI
|
||||
import fr.dcproject.component.citizen.database.CitizenI
|
||||
import fr.dcproject.component.citizen.database.CitizenRef
|
||||
import fr.dcproject.component.comment.generic.database.CommentForView
|
||||
import fr.dcproject.component.constitution.database.Constitution
|
||||
import fr.dcproject.component.vote.entity.VoteAggregation
|
||||
@@ -86,31 +86,31 @@ class VoteArticleRepository(requester: Requester) : VoteRepositoryAbs<ArticleFor
|
||||
)
|
||||
}
|
||||
|
||||
class VoteArticleCommentRepository(requester: Requester) : VoteRepositoryAbs<CommentForView<ArticleForView, CitizenRef>>(requester) {
|
||||
class VoteArticleCommentRepository(requester: Requester) : VoteRepositoryAbs<CommentForView<ArticleForView, CitizenCreatorI>>(requester) {
|
||||
fun findByCitizen(
|
||||
citizen: CitizenEntity,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<VoteEntity<CommentForView<ArticleForView, CitizenRef>>> =
|
||||
): Paginated<VoteEntity<CommentForView<ArticleForView, CitizenCreatorI>>> =
|
||||
findByCitizen(
|
||||
citizen.id,
|
||||
"article",
|
||||
object : TypeReference<List<VoteEntity<CommentForView<ArticleForView, CitizenRef>>>>() {},
|
||||
object : TypeReference<List<VoteEntity<CommentForView<ArticleForView, CitizenCreatorI>>>>() {},
|
||||
page,
|
||||
limit
|
||||
)
|
||||
}
|
||||
|
||||
class VoteCommentRepository(requester: Requester) : VoteRepositoryAbs<CommentForView<TargetRef, CitizenRef>>(requester) {
|
||||
class VoteCommentRepository(requester: Requester) : VoteRepositoryAbs<CommentForView<TargetRef, CitizenCreatorI>>(requester) {
|
||||
fun findByCitizen(
|
||||
citizen: CitizenEntity,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<VoteEntity<CommentForView<TargetRef, CitizenRef>>> =
|
||||
): Paginated<VoteEntity<CommentForView<TargetRef, CitizenCreatorI>>> =
|
||||
findByCitizen(
|
||||
citizen.id,
|
||||
"article",
|
||||
object : TypeReference<List<VoteEntity<CommentForView<TargetRef, CitizenRef>>>>() {},
|
||||
object : TypeReference<List<VoteEntity<CommentForView<TargetRef, CitizenCreatorI>>>>() {},
|
||||
page,
|
||||
limit
|
||||
)
|
||||
|
||||
@@ -470,6 +470,41 @@ paths:
|
||||
/articles/{article}/comments:
|
||||
parameters:
|
||||
- $ref: '#/components/parameters/article'
|
||||
get:
|
||||
summary: Get comments of one article
|
||||
tags:
|
||||
- comment
|
||||
- article
|
||||
parameters:
|
||||
- $ref: '#/components/parameters/page'
|
||||
- $ref: '#/components/parameters/limit'
|
||||
- $ref: '#/components/parameters/search'
|
||||
- name: sort
|
||||
in: query
|
||||
required: false
|
||||
example:
|
||||
- created_at
|
||||
- votes
|
||||
schema:
|
||||
type: string
|
||||
default: created_at
|
||||
enum:
|
||||
- created_at
|
||||
- votes
|
||||
responses:
|
||||
200:
|
||||
description: Return Comment and children
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
allOf:
|
||||
- $ref: '#/components/schemas/Paginated'
|
||||
- type: object
|
||||
properties:
|
||||
result:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/CommentResponse'
|
||||
post:
|
||||
security:
|
||||
- JWTAuth: [ ]
|
||||
|
||||
Reference in New Issue
Block a user