Can Comment a Comment

This commit is contained in:
2019-10-15 15:46:19 +02:00
parent 499fbd6dcf
commit 8cb5c35296
4 changed files with 77 additions and 13 deletions

View File

@@ -7,12 +7,26 @@ open class Comment <T : UuidEntity> (
id: UUID = UUID.randomUUID(), id: UUID = UUID.randomUUID(),
createdBy: Citizen, createdBy: Citizen,
target: T, target: T,
override var targetReference: String = target::class.simpleName!!.toLowerCase(),
var content: String, var content: String,
var responses: List<Comment<T>>? = null, val responses: List<Comment<T>>? = null,
var parent: Comment<T>? = null, var parent: Comment<T>? = null,
var parentsIds: List<UUID>? = null, val parentsIds: List<UUID>? = null,
val childrenCount: Int? = null val childrenCount: Int? = null
) : Extra<T>(id, createdBy, target), ) : Extra<T>(id, createdBy, target),
EntityUpdatedAt by EntityUpdatedAtImp(), EntityUpdatedAt by EntityUpdatedAtImp(),
EntityDeletedAt by EntityDeletedAtImp(), EntityDeletedAt by EntityDeletedAtImp(),
Votable by VotableImp() Votable by VotableImp()
{
constructor(
createdBy: Citizen,
parent: Comment<T>,
content: String
) : this(
createdBy = createdBy,
parent = parent,
target = parent.target,
targetReference = parent.targetReference,
content = content
)
}

View File

@@ -62,11 +62,10 @@ abstract class Comment <T : UuidEntity>(override var requester: Requester) : Rep
} }
fun comment(comment: CommentEntity<T>) { fun comment(comment: CommentEntity<T>) {
val reference = comment.target::class.simpleName!!.toLowerCase()
requester requester
.getFunction("comment") .getFunction("comment")
.sendQuery( .sendQuery(
"reference" to reference, "reference" to comment.targetReference,
"resource" to comment "resource" to comment
) )
} }

View File

@@ -1,17 +1,19 @@
package fr.dcproject.routes package fr.dcproject.routes
import fr.dcproject.security.voter.CommentVoter.Action.UPDATE import fr.dcproject.citizen
import fr.dcproject.security.voter.CommentVoter.Action.VIEW 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 fr.dcproject.security.voter.assertCan
import io.ktor.application.call import io.ktor.application.call
import io.ktor.features.NotFoundException
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
import io.ktor.locations.KtorExperimentalLocationsAPI import io.ktor.locations.*
import io.ktor.locations.Location import io.ktor.request.receive
import io.ktor.locations.get
import io.ktor.locations.put
import io.ktor.request.receiveText import io.ktor.request.receiveText
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Route import io.ktor.routing.Route
import io.ktor.util.KtorExperimentalAPI
import java.util.* import java.util.*
import fr.dcproject.repository.CommentGeneric as CommentRepository import fr.dcproject.repository.CommentGeneric as CommentRepository
@@ -30,8 +32,14 @@ object CommentPaths {
val page: Int = if (page < 1) 1 else page val page: Int = if (page < 1) 1 else page
val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit 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 @KtorExperimentalLocationsAPI
fun Route.comment(repo: CommentRepository) { fun Route.comment(repo: CommentRepository) {
get<CommentPaths.CommentRequest> { get<CommentPaths.CommentRequest> {
@@ -54,6 +62,21 @@ fun Route.comment(repo: CommentRepository) {
call.respond(HttpStatusCode.OK, comments) call.respond(HttpStatusCode.OK, comments)
} }
post<CommentPaths.CreateCommentRequest> {
val parent = repo.findById(it.comment) ?: throw NotFoundException("Comment not found")
val newComment = Comment(
content = call.receive<Content>().content,
createdBy = citizen,
parent = parent
)
assertCan(CREATE, newComment)
repo.comment(newComment)
call.respond(HttpStatusCode.Created, newComment)
}
put<CommentPaths.CommentRequest> { put<CommentPaths.CommentRequest> {
val comment = repo.findById(it.comment)!! val comment = repo.findById(it.comment)!!
assertCan(UPDATE, comment) assertCan(UPDATE, comment)

View File

@@ -348,6 +348,7 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/ConstitutionResponse' $ref: '#/components/schemas/ConstitutionResponse'
/comments/{comment}: /comments/{comment}:
parameters: parameters:
- $ref: '#/components/parameters/comment' - $ref: '#/components/parameters/comment'
@@ -382,7 +383,6 @@ paths:
$ref: '#/components/schemas/CommentResponse' $ref: '#/components/schemas/CommentResponse'
401: 401:
$ref: '#/components/responses/401' $ref: '#/components/responses/401'
/comments/{comment}/children: /comments/{comment}/children:
parameters: parameters:
- $ref: '#/components/parameters/comment' - $ref: '#/components/parameters/comment'
@@ -404,6 +404,35 @@ paths:
type: array type: array
items: items:
$ref: '#/components/schemas/CommentResponse' $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: /articles/{article}/comments:
parameters: parameters:
@@ -706,7 +735,7 @@ components:
name: search name: search
in: query in: query
description: A text to seach description: A text to seach
example: John Doe example: content50
required: false required: false
schema: schema:
type: string type: string
@@ -1086,7 +1115,6 @@ components:
required: false required: false
allOf: allOf:
- $ref: '#/components/schemas/UUID' - $ref: '#/components/schemas/UUID'
- $ref: '#/components/schemas/UUID'
FollowBase: FollowBase:
allOf: allOf: