diff --git a/src/main/kotlin/application/Converters.kt b/src/main/kotlin/application/Converters.kt index 507634f..08dd7ae 100644 --- a/src/main/kotlin/application/Converters.kt +++ b/src/main/kotlin/application/Converters.kt @@ -1,8 +1,5 @@ package fr.dcproject.application -import fr.dcproject.component.article.ArticleForView -import fr.dcproject.component.article.ArticleRef -import fr.dcproject.component.article.ArticleRepository import fr.dcproject.component.citizen.Citizen import fr.dcproject.component.citizen.CitizenBasic import fr.dcproject.component.citizen.CitizenRef @@ -46,22 +43,6 @@ val converters: ConverterDeclaration = { } } - convert { - decode { values, _ -> - values.singleOrNull()?.let { - get().findById(UUID.fromString(it)) - ?: throw NotFoundException("Article $values not found") - } ?: throw NotFoundException("Article $values not found") - } - } - convert { - decode { values, _ -> - values.singleOrNull()?.let { - ArticleRef(UUID.fromString(it)) - } ?: throw NotFoundException("""UUID "$values" is not valid for Article""") - } - } - convert { decode { values, _ -> values.singleOrNull()?.let { diff --git a/src/main/kotlin/component/comment/article/routes/CreateCommentArticle.kt b/src/main/kotlin/component/comment/article/routes/CreateCommentArticle.kt index 32a63b8..c708d16 100644 --- a/src/main/kotlin/component/comment/article/routes/CreateCommentArticle.kt +++ b/src/main/kotlin/component/comment/article/routes/CreateCommentArticle.kt @@ -1,9 +1,10 @@ package fr.dcproject.component.comment.article.routes -import fr.dcproject.component.article.ArticleForView +import fr.dcproject.component.article.ArticleRef import fr.dcproject.component.auth.citizen import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.comment.article.CommentArticleRepository +import fr.dcproject.component.comment.article.routes.CreateCommentArticle.PostArticleCommentRequest.Input import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.CommentForUpdate import fr.dcproject.security.assert @@ -16,24 +17,22 @@ import io.ktor.locations.post import io.ktor.request.receive import io.ktor.response.respond import io.ktor.routing.Route +import java.util.UUID @KtorExperimentalLocationsAPI object CreateCommentArticle { @Location("/articles/{article}/comments") - class PostArticleCommentRequest( - val article: ArticleForView - ) { - class Comment( - val content: String - ) + class PostArticleCommentRequest(article: UUID) { + val article = ArticleRef(article) + class Input(val content: String) + } - suspend fun getComment(call: ApplicationCall) = call.receive().run { - CommentForUpdate( - target = article, - createdBy = call.citizen, - content = content - ) - } + suspend fun PostArticleCommentRequest.getComment(call: ApplicationCall) = call.receive().run { + CommentForUpdate( + target = article, + createdBy = call.citizen, + content = content + ) } fun Route.createCommentArticle(repo: CommentArticleRepository, ac: CommentAccessControl) { diff --git a/src/main/kotlin/component/comment/article/routes/GetArticleComments.kt b/src/main/kotlin/component/comment/article/routes/GetArticleComments.kt index 92fc428..5768cd5 100644 --- a/src/main/kotlin/component/comment/article/routes/GetArticleComments.kt +++ b/src/main/kotlin/component/comment/article/routes/GetArticleComments.kt @@ -14,17 +14,19 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route +import java.util.UUID @KtorExperimentalLocationsAPI object GetArticleComments { @Location("/articles/{article}/comments") class ArticleCommentsRequest( - val article: ArticleRef, + article: UUID, page: Int = 1, limit: Int = 50, val search: String? = null, sort: String = CommentArticleRepository.Sort.CREATED_AT.sql ) : PaginatedRequestI by PaginatedRequest(page, limit) { + val article = ArticleRef(article) val sort: CommentArticleRepository.Sort = CommentArticleRepository.Sort.fromString(sort) ?: CommentArticleRepository.Sort.CREATED_AT } diff --git a/src/main/kotlin/component/comment/generic/CommentRepository.kt b/src/main/kotlin/component/comment/generic/CommentRepository.kt index 768ba42..2bf94a8 100644 --- a/src/main/kotlin/component/comment/generic/CommentRepository.kt +++ b/src/main/kotlin/component/comment/generic/CommentRepository.kt @@ -69,7 +69,7 @@ abstract class CommentRepositoryAbs(override var requester: Request } } - fun comment(comment: CommentForUpdate) { + fun comment(comment: CommentForUpdate) { requester .getFunction("comment") .sendQuery( diff --git a/src/main/kotlin/component/follow/routes/article/FollowArticle.kt b/src/main/kotlin/component/follow/routes/article/FollowArticle.kt index c43c5c9..b476809 100644 --- a/src/main/kotlin/component/follow/routes/article/FollowArticle.kt +++ b/src/main/kotlin/component/follow/routes/article/FollowArticle.kt @@ -14,11 +14,14 @@ import io.ktor.locations.Location import io.ktor.locations.post import io.ktor.response.respond import io.ktor.routing.Route +import java.util.UUID @KtorExperimentalLocationsAPI object FollowArticle { @Location("/articles/{article}/follows") - class ArticleFollowRequest(val article: ArticleRef) + class ArticleFollowRequest(article: UUID) { + val article = ArticleRef(article) + } fun Route.followArticle(repo: FollowArticleRepository, ac: FollowAccessControl) { post { diff --git a/src/main/kotlin/component/follow/routes/article/GetFollowArticle.kt b/src/main/kotlin/component/follow/routes/article/GetFollowArticle.kt index 5592ad3..bac6a43 100644 --- a/src/main/kotlin/component/follow/routes/article/GetFollowArticle.kt +++ b/src/main/kotlin/component/follow/routes/article/GetFollowArticle.kt @@ -13,11 +13,14 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route +import java.util.UUID @KtorExperimentalLocationsAPI object GetFollowArticle { @Location("/articles/{article}/follows") - class ArticleFollowRequest(val article: ArticleRef) + class ArticleFollowRequest(article: UUID) { + val article = ArticleRef(article) + } fun Route.getFollowArticle(repo: FollowArticleRepository, ac: FollowAccessControl) { get { diff --git a/src/main/kotlin/component/follow/routes/article/UnfollowArticle.kt b/src/main/kotlin/component/follow/routes/article/UnfollowArticle.kt index d353171..47d3c83 100644 --- a/src/main/kotlin/component/follow/routes/article/UnfollowArticle.kt +++ b/src/main/kotlin/component/follow/routes/article/UnfollowArticle.kt @@ -14,11 +14,14 @@ import io.ktor.locations.Location import io.ktor.locations.delete import io.ktor.response.respond import io.ktor.routing.Route +import java.util.UUID @KtorExperimentalLocationsAPI object UnfollowArticle { @Location("/articles/{article}/follows") - class ArticleFollowRequest(val article: ArticleRef) + class ArticleFollowRequest(article: UUID) { + val article = ArticleRef(article) + } fun Route.unfollowArticle(repo: FollowArticleRepository, ac: FollowAccessControl) { delete { diff --git a/src/main/kotlin/component/opinion/routes/OpinionArticle.kt b/src/main/kotlin/component/opinion/routes/OpinionArticle.kt index a8c6246..ced5ebb 100644 --- a/src/main/kotlin/component/opinion/routes/OpinionArticle.kt +++ b/src/main/kotlin/component/opinion/routes/OpinionArticle.kt @@ -1,6 +1,6 @@ package fr.dcproject.component.opinion.routes -import fr.dcproject.component.article.ArticleForView +import fr.dcproject.component.article.ArticleRef import fr.dcproject.component.auth.citizen import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.opinion.OpinionAccessControl @@ -25,7 +25,8 @@ object OpinionArticle { * Put an opinion on one article */ @Location("/articles/{article}/opinions") - class ArticleOpinion(val article: ArticleForView) { + class ArticleOpinion(article: UUID) { + val article = ArticleRef(article) class Body(ids: List) { val ids: List = ids.map { it.toUUID() } } diff --git a/src/main/kotlin/component/vote/routes/PutVoteOnArticle.kt b/src/main/kotlin/component/vote/routes/PutVoteOnArticle.kt index 3473c10..5a8501b 100644 --- a/src/main/kotlin/component/vote/routes/PutVoteOnArticle.kt +++ b/src/main/kotlin/component/vote/routes/PutVoteOnArticle.kt @@ -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 { - val content = call.receive() + val input = call.receive() + 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) } diff --git a/src/main/kotlin/component/vote/routes/install.kt b/src/main/kotlin/component/vote/routes/install.kt index e4d2753..3a51678 100644 --- a/src/main/kotlin/component/vote/routes/install.kt +++ b/src/main/kotlin/component/vote/routes/install.kt @@ -15,7 +15,7 @@ fun Routing.installVoteRoutes() { authenticate(optional = true) { getCitizenVote(get(), get()) getCitizenVotesOnArticle(get(), get()) - putVoteOnArticle(get(), get()) + putVoteOnArticle(get(), get(), get()) putVoteOnComment(get(), get(), get()) voteConstitution(get(), get()) }