Test openapi schema of PUT /comments/{comment}
This commit is contained in:
@@ -74,12 +74,14 @@ object GetArticleComments {
|
||||
}
|
||||
}
|
||||
}
|
||||
val votes: Any = object {
|
||||
val up: Int = 0
|
||||
val neutral: Int = 0
|
||||
val down: Int = 0
|
||||
val total: Int = 0
|
||||
val score: Int = 0
|
||||
val votes: Any = comment.votes.let { v ->
|
||||
object {
|
||||
val up: Int = v.up
|
||||
val neutral: Int = v.neutral
|
||||
val down: Int = v.down
|
||||
val total: Int = v.total
|
||||
val score: Int = v.score
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.dcproject.component.comment.generic.routes
|
||||
|
||||
import fr.dcproject.common.security.assert
|
||||
import fr.dcproject.common.utils.receiveOrBadRequest
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.comment.generic.CommentAccessControl
|
||||
import fr.dcproject.component.comment.generic.database.CommentRef
|
||||
@@ -11,9 +12,9 @@ import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.put
|
||||
import io.ktor.request.receiveText
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
import org.joda.time.DateTime
|
||||
import java.util.UUID
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@@ -21,6 +22,7 @@ object EditComment {
|
||||
@Location("/comments/{comment}")
|
||||
class EditCommentRequest(comment: UUID) {
|
||||
val comment = CommentRef(comment)
|
||||
class Input(val content: String)
|
||||
}
|
||||
|
||||
fun Route.editComment(repo: CommentRepository, ac: CommentAccessControl) {
|
||||
@@ -28,10 +30,55 @@ object EditComment {
|
||||
val comment = repo.findById(it.comment.id) ?: throw NotFoundException("Comment not found")
|
||||
ac.assert { canUpdate(comment, citizenOrNull) }
|
||||
|
||||
comment.content = call.receiveText()
|
||||
comment.content = call.receiveOrBadRequest<EditCommentRequest.Input>().content
|
||||
repo.edit(comment)
|
||||
|
||||
call.respond(HttpStatusCode.OK, comment)
|
||||
call.respond(
|
||||
HttpStatusCode.OK,
|
||||
object {
|
||||
val id: UUID = comment.id
|
||||
val content: String = comment.content
|
||||
val childrenCount: Int = comment.childrenCount ?: 0
|
||||
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 = comment.votes.let { v ->
|
||||
object {
|
||||
val up: Int = v.up
|
||||
val neutral: Int = v.neutral
|
||||
val down: Int = v.down
|
||||
val total: Int = v.total
|
||||
val score: Int = v.score
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,12 +63,14 @@ object GetOneComment {
|
||||
}
|
||||
}
|
||||
}
|
||||
val votes: Any = object {
|
||||
val up: Int = 0
|
||||
val neutral: Int = 0
|
||||
val down: Int = 0
|
||||
val total: Int = 0
|
||||
val score: Int = 0
|
||||
val votes: Any = comment.votes.let { v ->
|
||||
object {
|
||||
val up: Int = v.up
|
||||
val neutral: Int = v.neutral
|
||||
val down: Int = v.down
|
||||
val total: Int = v.total
|
||||
val score: Int = v.score
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -493,7 +493,7 @@ paths:
|
||||
- votes
|
||||
responses:
|
||||
200:
|
||||
description: Return Comment and children
|
||||
description: Return paginated comments of article
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
@@ -526,7 +526,7 @@ paths:
|
||||
Lorem ipsum...
|
||||
responses:
|
||||
201:
|
||||
description: Return Comment and children
|
||||
description: Return created Comment
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
@@ -542,11 +542,38 @@ paths:
|
||||
- comment
|
||||
responses:
|
||||
200:
|
||||
description: Return Comment and children
|
||||
description: Return Comment
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/CommentResponse'
|
||||
put:
|
||||
security:
|
||||
- JWTAuth: []
|
||||
summary: Edit existing comment
|
||||
tags:
|
||||
- comment
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
required:
|
||||
- content
|
||||
properties:
|
||||
content:
|
||||
type: string
|
||||
example:
|
||||
Lorem ipsum...
|
||||
responses:
|
||||
200:
|
||||
description: Return updated comment
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/CommentResponse'
|
||||
401:
|
||||
$ref: '#/components/responses/401'
|
||||
|
||||
components:
|
||||
parameters:
|
||||
page:
|
||||
|
||||
@@ -100,7 +100,9 @@ class `Comment articles routes` : BaseTest() {
|
||||
`When I send a PUT request`("/comments/fd30d20f-656c-42c6-8955-f61c04537464") {
|
||||
`authenticated as`("Hubert", "Reeves")
|
||||
`with body`("""
|
||||
Hello boy
|
||||
{
|
||||
"content": "Hello boy"
|
||||
}
|
||||
""")
|
||||
} `Then the response should be` OK and {
|
||||
`And the response should not be null`()
|
||||
|
||||
Reference in New Issue
Block a user