Remove converter for article

This commit is contained in:
2021-02-05 00:32:19 +01:00
parent 16eadc0921
commit aeaab860b2
10 changed files with 45 additions and 48 deletions

View File

@@ -1,6 +1,7 @@
package fr.dcproject.component.vote.routes
import fr.dcproject.component.article.ArticleForView
import fr.dcproject.component.article.ArticleRef
import fr.dcproject.component.article.ArticleRepository
import fr.dcproject.component.auth.citizen
import fr.dcproject.component.auth.citizenOrNull
import fr.dcproject.component.vote.VoteAccessControl
@@ -8,6 +9,7 @@ import fr.dcproject.component.vote.VoteArticleRepository
import fr.dcproject.component.vote.entity.VoteForUpdate
import fr.dcproject.security.assert
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
@@ -15,20 +17,23 @@ import io.ktor.locations.put
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Route
import java.util.UUID
@KtorExperimentalLocationsAPI
object PutVoteOnArticle {
@Location("/articles/{article}/vote")
class ArticleVoteRequest(val article: ArticleForView) {
data class Content(var note: Int)
class ArticleVoteRequest(article: UUID) {
val article = ArticleRef(article)
data class Input(var note: Int)
}
fun Route.putVoteOnArticle(repo: VoteArticleRepository, ac: VoteAccessControl) {
fun Route.putVoteOnArticle(repo: VoteArticleRepository, ac: VoteAccessControl, articleRepo: ArticleRepository) {
put<ArticleVoteRequest> {
val content = call.receive<ArticleVoteRequest.Content>()
val input = call.receive<ArticleVoteRequest.Input>()
val article = articleRepo.findById(it.article.id) ?: throw NotFoundException("Article ${it.article.id} not found")
val vote = VoteForUpdate(
target = it.article,
note = content.note,
target = article,
note = input.note,
createdBy = this.citizen
)
ac.assert { canCreate(vote, citizenOrNull) }