Test openapi schema of /articles/{article}/comments
This commit is contained in:
@@ -9,7 +9,6 @@ import fr.dcproject.component.comment.article.database.CommentArticleRepository
|
||||
import fr.dcproject.component.comment.article.routes.CreateCommentArticle.PostArticleCommentRequest.Input
|
||||
import fr.dcproject.component.comment.generic.CommentAccessControl
|
||||
import fr.dcproject.component.comment.generic.database.CommentForUpdate
|
||||
import io.ktor.application.ApplicationCall
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
@@ -17,6 +16,7 @@ import io.ktor.locations.Location
|
||||
import io.ktor.locations.post
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
import org.joda.time.DateTime
|
||||
import java.util.UUID
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@@ -27,20 +27,60 @@ object CreateCommentArticle {
|
||||
class Input(val content: String)
|
||||
}
|
||||
|
||||
suspend fun PostArticleCommentRequest.getComment(call: ApplicationCall) = call.receiveOrBadRequest<Input>().run {
|
||||
CommentForUpdate(
|
||||
target = article,
|
||||
createdBy = call.citizen,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
|
||||
fun Route.createCommentArticle(repo: CommentArticleRepository, ac: CommentAccessControl) {
|
||||
post<PostArticleCommentRequest> {
|
||||
it.getComment(call).let { comment ->
|
||||
call.receiveOrBadRequest<Input>().run {
|
||||
CommentForUpdate(
|
||||
target = it.article,
|
||||
createdBy = citizen,
|
||||
content = content
|
||||
)
|
||||
}.let { comment ->
|
||||
ac.assert { canCreate(comment, citizenOrNull) }
|
||||
repo.comment(comment)
|
||||
call.respond(HttpStatusCode.Created, comment)
|
||||
call.respond(
|
||||
HttpStatusCode.Created,
|
||||
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
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,6 +467,38 @@ paths:
|
||||
404:
|
||||
description: Citizen not found
|
||||
|
||||
/articles/{article}/comments:
|
||||
parameters:
|
||||
- $ref: '#/components/parameters/article'
|
||||
post:
|
||||
security:
|
||||
- JWTAuth: [ ]
|
||||
summary: Create Comment to article
|
||||
tags:
|
||||
- comment
|
||||
- article
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required:
|
||||
- content
|
||||
properties:
|
||||
content:
|
||||
type: string
|
||||
example:
|
||||
Lorem ipsum...
|
||||
responses:
|
||||
201:
|
||||
description: Return Comment and children
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/CommentResponse'
|
||||
401:
|
||||
$ref: '#/components/responses/401'
|
||||
|
||||
components:
|
||||
parameters:
|
||||
page:
|
||||
@@ -758,6 +790,7 @@ components:
|
||||
- name
|
||||
- email
|
||||
- createdAt
|
||||
- user
|
||||
properties:
|
||||
id:
|
||||
$ref: '#/components/schemas/UUID'
|
||||
@@ -791,12 +824,98 @@ components:
|
||||
required:
|
||||
- username
|
||||
properties:
|
||||
id:
|
||||
$ref: '#/components/schemas/UUID'
|
||||
username:
|
||||
type: string
|
||||
example:
|
||||
john-doe
|
||||
CitizenCreator:
|
||||
additionalProperties: false
|
||||
type: object
|
||||
required:
|
||||
- id
|
||||
- name
|
||||
- user
|
||||
properties:
|
||||
id:
|
||||
$ref: '#/components/schemas/UUID'
|
||||
name:
|
||||
type: object
|
||||
required:
|
||||
- firstName
|
||||
- lastName
|
||||
properties:
|
||||
firstName:
|
||||
type: string
|
||||
example:
|
||||
john
|
||||
lastName:
|
||||
type: string
|
||||
example:
|
||||
Doe
|
||||
user:
|
||||
type: object
|
||||
required:
|
||||
- username
|
||||
properties:
|
||||
username:
|
||||
type: string
|
||||
example:
|
||||
john-doe
|
||||
|
||||
CommentResponse:
|
||||
additionalProperties: false
|
||||
type: object
|
||||
required:
|
||||
- id
|
||||
- content
|
||||
- parent
|
||||
- createdAt
|
||||
- createdBy
|
||||
- votes
|
||||
- target
|
||||
properties:
|
||||
id:
|
||||
$ref: '#/components/schemas/UUID'
|
||||
content:
|
||||
type: string
|
||||
example:
|
||||
Lorem ipsum...
|
||||
parent:
|
||||
nullable: true
|
||||
properties:
|
||||
id:
|
||||
$ref: '#/components/schemas/UUID'
|
||||
reference:
|
||||
type: string
|
||||
createdBy:
|
||||
$ref: '#/components/schemas/CitizenCreator'
|
||||
createdAt:
|
||||
type: string
|
||||
format: 'date-time'
|
||||
target:
|
||||
additionalProperties: false
|
||||
properties:
|
||||
id:
|
||||
$ref: '#/components/schemas/UUID'
|
||||
reference:
|
||||
type: string
|
||||
votes:
|
||||
properties:
|
||||
up:
|
||||
type: number
|
||||
minimum: 0
|
||||
neutral:
|
||||
type: number
|
||||
minimum: 0
|
||||
down:
|
||||
type: number
|
||||
minimum: 0
|
||||
total:
|
||||
type: number
|
||||
minimum: 0
|
||||
score:
|
||||
type: number
|
||||
|
||||
securitySchemes:
|
||||
JWTAuth:
|
||||
type: http
|
||||
|
||||
Reference in New Issue
Block a user