From 5161dca1d502ba6f8f758ae57317a3ff824306e7 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 30 Jan 2020 15:02:52 +0100 Subject: [PATCH] Fix get all commet of article --- src/main/kotlin/fr/dcproject/Application.kt | 7 ++++ .../kotlin/fr/dcproject/repository/Comment.kt | 38 ++++++++++++++++--- .../dcproject/routes/CommentConstitution.kt | 4 +- .../resources/feature/commentArticle.feature | 6 +++ 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/Application.kt b/src/main/kotlin/fr/dcproject/Application.kt index b15a730..ab8a840 100644 --- a/src/main/kotlin/fr/dcproject/Application.kt +++ b/src/main/kotlin/fr/dcproject/Application.kt @@ -98,6 +98,13 @@ fun Application.module(env: Env = PROD) { } ?: throw NotFoundException("Comment $values not found") } } + convert { + decode { values, _ -> + values.singleOrNull()?.let { + ConstitutionRef(UUID.fromString(it)) + } ?: throw NotFoundException("Constitution $values not found") + } + } convert { decode { values, _ -> diff --git a/src/main/kotlin/fr/dcproject/repository/Comment.kt b/src/main/kotlin/fr/dcproject/repository/Comment.kt index e31d3ee..532ce46 100644 --- a/src/main/kotlin/fr/dcproject/repository/Comment.kt +++ b/src/main/kotlin/fr/dcproject/repository/Comment.kt @@ -1,6 +1,7 @@ package fr.dcproject.repository import fr.dcproject.entity.ArticleRef +import fr.dcproject.entity.ConstitutionRef import fr.dcproject.entity.TargetI import fr.dcproject.entity.TargetRef import fr.postgresjson.connexion.Paginated @@ -10,7 +11,6 @@ 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.Constitution as ConstitutionEntity abstract class Comment(override var requester: Requester) : RepositoryI { abstract fun findById(id: UUID): CommentEntity? @@ -127,10 +127,24 @@ class CommentArticle(requester: Requester) : Comment(requester) { ) } } + + override fun findByTarget( + target: UuidEntityI, + page: Int, + limit: Int + ): Paginated> { + return requester.run { + getFunction("find_comments_by_target") + .select( + page, limit, + "target_id" to target.id + ) + } + } } -class CommentConstitution(requester: Requester) : Comment(requester) { - override fun findById(id: UUID): CommentEntity? { +class CommentConstitution(requester: Requester) : Comment(requester) { + override fun findById(id: UUID): CommentEntity? { return requester .getFunction("find_comment_by_id") .selectOne(mapOf("id" to id)) @@ -140,13 +154,27 @@ class CommentConstitution(requester: Requester) : Comment(re citizen: CitizenEntity, page: Int, limit: Int - ): Paginated> { + ): Paginated> { return requester.run { getFunction("find_comments_by_citizen") .select( page, limit, "created_by_id" to citizen.id, - "reference" to TargetI.getReference(ConstitutionEntity::class) + "reference" to TargetI.getReference(ConstitutionRef::class) + ) + } + } + + override fun findByTarget( + target: UuidEntityI, + page: Int, + limit: Int + ): Paginated> { + return requester.run { + getFunction("find_comments_by_target") + .select( + page, limit, + "target_id" to target.id ) } } diff --git a/src/main/kotlin/fr/dcproject/routes/CommentConstitution.kt b/src/main/kotlin/fr/dcproject/routes/CommentConstitution.kt index 6890aae..2721669 100644 --- a/src/main/kotlin/fr/dcproject/routes/CommentConstitution.kt +++ b/src/main/kotlin/fr/dcproject/routes/CommentConstitution.kt @@ -2,6 +2,7 @@ package fr.dcproject.routes import fr.dcproject.citizen import fr.dcproject.entity.Citizen +import fr.dcproject.entity.ConstitutionRef import fr.dcproject.security.voter.CommentVoter.Action.CREATE import fr.dcproject.security.voter.CommentVoter.Action.VIEW import fr.dcproject.security.voter.assertCan @@ -15,13 +16,12 @@ import io.ktor.request.receiveText import io.ktor.response.respond import io.ktor.routing.Route import fr.dcproject.entity.Comment as CommentEntity -import fr.dcproject.entity.Constitution as ConstitutionEntity import fr.dcproject.repository.CommentConstitution as CommentConstitutionRepository @KtorExperimentalLocationsAPI object CommentConstitutionPaths { @Location("/constitutions/{constitution}/comments") - class ConstitutionCommentRequest(val constitution: ConstitutionEntity) + class ConstitutionCommentRequest(val constitution: ConstitutionRef) @Location("/citizens/{citizen}/comments/constitutions") class CitizenCommentConstitutionRequest(val citizen: Citizen) diff --git a/src/test/resources/feature/commentArticle.feature b/src/test/resources/feature/commentArticle.feature index 4909f6a..d548899 100644 --- a/src/test/resources/feature/commentArticle.feature +++ b/src/test/resources/feature/commentArticle.feature @@ -11,6 +11,12 @@ Feature: comment Article """ Then the response status code should be 201 + Scenario: Can get all comment on article + Given I am authenticated as John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" + And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" + When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/comments" + Then the response status code should be 200 + Scenario: Can get comments on articles of the current citizen Given I have citizen John Doe with id "64b7b379-2298-43ec-b428-ba134930cabd" And I have article with id "9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b"