diff --git a/src/main/kotlin/fr/dcproject/entity/Comment.kt b/src/main/kotlin/fr/dcproject/entity/Comment.kt index 6bfddfa..5ac4f2d 100644 --- a/src/main/kotlin/fr/dcproject/entity/Comment.kt +++ b/src/main/kotlin/fr/dcproject/entity/Comment.kt @@ -7,12 +7,26 @@ open class Comment ( id: UUID = UUID.randomUUID(), createdBy: Citizen, target: T, + override var targetReference: String = target::class.simpleName!!.toLowerCase(), var content: String, - var responses: List>? = null, + val responses: List>? = null, var parent: Comment? = null, - var parentsIds: List? = null, + val parentsIds: List? = null, val childrenCount: Int? = null ) : Extra(id, createdBy, target), EntityUpdatedAt by EntityUpdatedAtImp(), EntityDeletedAt by EntityDeletedAtImp(), Votable by VotableImp() +{ + constructor( + createdBy: Citizen, + parent: Comment, + content: String + ) : this( + createdBy = createdBy, + parent = parent, + target = parent.target, + targetReference = parent.targetReference, + content = content + ) +} diff --git a/src/main/kotlin/fr/dcproject/repository/Comment.kt b/src/main/kotlin/fr/dcproject/repository/Comment.kt index 98b8e98..0149f27 100644 --- a/src/main/kotlin/fr/dcproject/repository/Comment.kt +++ b/src/main/kotlin/fr/dcproject/repository/Comment.kt @@ -62,11 +62,10 @@ abstract class Comment (override var requester: Requester) : Rep } fun comment(comment: CommentEntity) { - val reference = comment.target::class.simpleName!!.toLowerCase() requester .getFunction("comment") .sendQuery( - "reference" to reference, + "reference" to comment.targetReference, "resource" to comment ) } diff --git a/src/main/kotlin/fr/dcproject/routes/Comment.kt b/src/main/kotlin/fr/dcproject/routes/Comment.kt index b917a77..e866e7e 100644 --- a/src/main/kotlin/fr/dcproject/routes/Comment.kt +++ b/src/main/kotlin/fr/dcproject/routes/Comment.kt @@ -1,17 +1,19 @@ package fr.dcproject.routes -import fr.dcproject.security.voter.CommentVoter.Action.UPDATE -import fr.dcproject.security.voter.CommentVoter.Action.VIEW +import fr.dcproject.citizen +import fr.dcproject.entity.Comment +import fr.dcproject.routes.CommentPaths.CreateCommentRequest.Content +import fr.dcproject.security.voter.CommentVoter.Action.* import fr.dcproject.security.voter.assertCan import io.ktor.application.call +import io.ktor.features.NotFoundException import io.ktor.http.HttpStatusCode -import io.ktor.locations.KtorExperimentalLocationsAPI -import io.ktor.locations.Location -import io.ktor.locations.get -import io.ktor.locations.put +import io.ktor.locations.* +import io.ktor.request.receive import io.ktor.request.receiveText import io.ktor.response.respond import io.ktor.routing.Route +import io.ktor.util.KtorExperimentalAPI import java.util.* import fr.dcproject.repository.CommentGeneric as CommentRepository @@ -30,8 +32,14 @@ object CommentPaths { val page: Int = if (page < 1) 1 else page val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit } + + @Location("/comments/{comment}/children") + class CreateCommentRequest(val comment: UUID) { + class Content(val content: String) + } } +@KtorExperimentalAPI @KtorExperimentalLocationsAPI fun Route.comment(repo: CommentRepository) { get { @@ -54,6 +62,21 @@ fun Route.comment(repo: CommentRepository) { call.respond(HttpStatusCode.OK, comments) } + post { + val parent = repo.findById(it.comment) ?: throw NotFoundException("Comment not found") + val newComment = Comment( + content = call.receive().content, + createdBy = citizen, + parent = parent + ) + + assertCan(CREATE, newComment) + repo.comment(newComment) + + + call.respond(HttpStatusCode.Created, newComment) + } + put { val comment = repo.findById(it.comment)!! assertCan(UPDATE, comment) diff --git a/src/main/resources/openApi.yaml b/src/main/resources/openApi.yaml index 1e97360..61e7d16 100644 --- a/src/main/resources/openApi.yaml +++ b/src/main/resources/openApi.yaml @@ -348,6 +348,7 @@ paths: application/json: schema: $ref: '#/components/schemas/ConstitutionResponse' + /comments/{comment}: parameters: - $ref: '#/components/parameters/comment' @@ -382,7 +383,6 @@ paths: $ref: '#/components/schemas/CommentResponse' 401: $ref: '#/components/responses/401' - /comments/{comment}/children: parameters: - $ref: '#/components/parameters/comment' @@ -404,6 +404,35 @@ paths: type: array items: $ref: '#/components/schemas/CommentResponse' + post: + security: + - JWTAuth: [] + summary: Create Comment on other comment + tags: + - comment + requestBody: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/CommentRequest' + responses: + 201: + description: Return the created Comment + content: + application/json: + schema: + allOf: + - type: object + properties: + result: + type: array + items: + $ref: '#/components/schemas/CommentResponse' + 404: + description: Comment not found + 401: + $ref: '#/components/responses/401' /articles/{article}/comments: parameters: @@ -706,7 +735,7 @@ components: name: search in: query description: A text to seach - example: John Doe + example: content50 required: false schema: type: string @@ -1086,7 +1115,6 @@ components: required: false allOf: - $ref: '#/components/schemas/UUID' - - $ref: '#/components/schemas/UUID' FollowBase: allOf: