Add validation on route PutVoteOnComment
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
package fr.dcproject.component.vote.routes
|
||||
|
||||
import fr.dcproject.application.http.badRequestIfNotValid
|
||||
import fr.dcproject.common.security.assert
|
||||
import fr.dcproject.common.utils.receiveOrBadRequest
|
||||
import fr.dcproject.component.auth.citizen
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.auth.mustBeAuth
|
||||
import fr.dcproject.component.comment.generic.database.CommentRef
|
||||
import fr.dcproject.component.comment.generic.database.CommentRepository
|
||||
import fr.dcproject.component.vote.VoteAccessControl
|
||||
import fr.dcproject.component.vote.database.VoteCommentRepository
|
||||
import fr.dcproject.component.vote.database.VoteForUpdate
|
||||
import io.konform.validation.Validation
|
||||
import io.konform.validation.jsonschema.maximum
|
||||
import io.konform.validation.jsonschema.minimum
|
||||
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
|
||||
@@ -21,18 +27,29 @@ import java.util.UUID
|
||||
@KtorExperimentalLocationsAPI
|
||||
object PutVoteOnComment {
|
||||
@Location("/comments/{comment}/vote")
|
||||
class CommentVoteRequest(val comment: UUID) {
|
||||
data class Content(var note: Int)
|
||||
class CommentVoteRequest(comment: UUID) {
|
||||
val comment = CommentRef(comment)
|
||||
data class Input(var note: Int) {
|
||||
fun validate() = Validation<Input> {
|
||||
Input::note {
|
||||
minimum(-1)
|
||||
maximum(1)
|
||||
}
|
||||
}.validate(this)
|
||||
}
|
||||
}
|
||||
|
||||
fun Route.putVoteOnComment(voteCommentRepo: VoteCommentRepository, commentRepo: CommentRepository, ac: VoteAccessControl) {
|
||||
put<CommentVoteRequest> {
|
||||
mustBeAuth()
|
||||
val comment = commentRepo.findById(it.comment)!!
|
||||
val content = call.receiveOrBadRequest<CommentVoteRequest.Content>()
|
||||
|
||||
val comment = commentRepo.findById(it.comment.id) ?: throw NotFoundException("Comment ${it.comment.id} not found")
|
||||
val input = call.receiveOrBadRequest<CommentVoteRequest.Input>()
|
||||
.apply { validate().badRequestIfNotValid() }
|
||||
|
||||
val vote = VoteForUpdate(
|
||||
target = comment,
|
||||
note = content.note,
|
||||
note = input.note,
|
||||
createdBy = this.citizen
|
||||
)
|
||||
ac.assert { canCreate(vote, citizenOrNull) }
|
||||
|
||||
Reference in New Issue
Block a user