From a99eaf3eefa7fe69f9a0ecb5fa431b23bedb4ad3 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Wed, 18 Mar 2020 00:51:42 +0100 Subject: [PATCH] Fix comment voter --- .../kotlin/fr/dcproject/repository/Comment.kt | 29 +++++++++---------- .../fr/dcproject/routes/CommentArticle.kt | 4 +-- src/test/kotlin/feature/ArticleSteps.kt | 2 +- .../resources/feature/commentArticle.feature | 2 +- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/repository/Comment.kt b/src/main/kotlin/fr/dcproject/repository/Comment.kt index fcc196f..a27937f 100644 --- a/src/main/kotlin/fr/dcproject/repository/Comment.kt +++ b/src/main/kotlin/fr/dcproject/repository/Comment.kt @@ -11,6 +11,7 @@ import fr.postgresjson.repository.RepositoryI import java.util.* import fr.dcproject.entity.Citizen as CitizenEntity import fr.dcproject.entity.Comment as CommentEntity +import fr.dcproject.entity.Article as ArticleEntity abstract class Comment(override var requester: Requester) : RepositoryI { abstract fun findById(id: UUID): CommentEntity? @@ -68,7 +69,7 @@ abstract class Comment(override var requester: Requester) : Reposit } } - fun comment(comment: CommentEntity) { + fun comment(comment: CommentEntity) { requester .getFunction("comment") .sendQuery( @@ -77,7 +78,7 @@ abstract class Comment(override var requester: Requester) : Reposit ) } - fun edit(comment: CommentEntity) { + fun edit(comment: CommentEntity) { requester .getFunction("edit_comment") .sendQuery( @@ -123,8 +124,8 @@ class CommentGeneric(requester: Requester) : Comment(requester) { } } -class CommentArticle(requester: Requester) : Comment(requester) { - override fun findById(id: UUID): CommentEntity? { +class CommentArticle(requester: Requester) : Comment(requester) { + override fun findById(id: UUID): CommentEntity? { return requester .getFunction("find_comment_by_id") .selectOne(mapOf("id" to id)) @@ -134,7 +135,7 @@ class CommentArticle(requester: Requester) : Comment(requester) { citizen: CitizenEntity, page: Int, limit: Int - ): Paginated> { + ): Paginated> { return requester.run { getFunction("find_comments_by_citizen") .select( @@ -150,19 +151,17 @@ class CommentArticle(requester: Requester) : Comment(requester) { page: Int, limit: Int, sort: Sort - ): Paginated> { - return requester.run { - getFunction("find_comments_by_target") - .select( - page, limit, - "target_id" to target.id, - "sort" to sort.sql - ) - } - } + ): Paginated> = requester + .getFunction("find_comments_by_target") + .select( + page, limit, + "target_id" to target.id, + "sort" to sort.sql + ) enum class Sort(val sql: String) { CREATED_AT("created_at"), VOTES("votes"); + companion object { fun fromString(string: String): Sort? { return values().firstOrNull { it.sql == string } diff --git a/src/main/kotlin/fr/dcproject/routes/CommentArticle.kt b/src/main/kotlin/fr/dcproject/routes/CommentArticle.kt index 0b184fd..6a34afd 100644 --- a/src/main/kotlin/fr/dcproject/routes/CommentArticle.kt +++ b/src/main/kotlin/fr/dcproject/routes/CommentArticle.kt @@ -1,7 +1,7 @@ package fr.dcproject.routes import fr.dcproject.citizen -import fr.dcproject.entity.ArticleRef +import fr.dcproject.entity.Article import fr.dcproject.entity.Citizen import fr.dcproject.repository.CommentArticle.Sort import fr.dcproject.security.voter.CommentVoter.Action.CREATE @@ -24,7 +24,7 @@ import fr.dcproject.repository.CommentArticle as CommentArticleRepository object CommentArticlePaths { @Location("/articles/{article}/comments") class ArticleCommentRequest( - val article: ArticleRef, + val article: Article, page: Int = 1, limit: Int = 50, val search: String? = null, diff --git a/src/test/kotlin/feature/ArticleSteps.kt b/src/test/kotlin/feature/ArticleSteps.kt index b83cf75..03b3602 100644 --- a/src/test/kotlin/feature/ArticleSteps.kt +++ b/src/test/kotlin/feature/ArticleSteps.kt @@ -82,7 +82,7 @@ class ArticleSteps : En, KoinTest { ("$firstName-$lastName".toLowerCase()).toLowerCase().replace(' ', '-') ) ?: error("Citizen not exist") - val comment: CommentEntity = CommentEntity( + val comment: CommentEntity = CommentEntity( id = id ?: params?.get("id")?.let { UUID.fromString(it) } ?: UUID.randomUUID(), createdBy = citizen, target = article, diff --git a/src/test/resources/feature/commentArticle.feature b/src/test/resources/feature/commentArticle.feature index 5a1ead1..d8351ba 100644 --- a/src/test/resources/feature/commentArticle.feature +++ b/src/test/resources/feature/commentArticle.feature @@ -30,7 +30,7 @@ Feature: comment Article And I am authenticated as Pierre Curie And I have article | id | 5e209f63-57ce-43ca-922a-273b0d62f567 | - And I have comment created by Enrico Fermi on article "5e209f63-57ce-43ca-922a-273b0d62f567" + And I have comment created by Pierre Curie on article "5e209f63-57ce-43ca-922a-273b0d62f567" When I send a GET request to "/articles/5e209f63-57ce-43ca-922a-273b0d62f567/comments?sort=votes" Then the response status code should be 200 And the response should contain object: