Fix get all commet of article

This commit is contained in:
2020-01-30 15:02:52 +01:00
parent 24bc1520f7
commit 5161dca1d5
4 changed files with 48 additions and 7 deletions

View File

@@ -98,6 +98,13 @@ fun Application.module(env: Env = PROD) {
} ?: throw NotFoundException("Comment $values not found")
}
}
convert<ConstitutionRef> {
decode { values, _ ->
values.singleOrNull()?.let {
ConstitutionRef(UUID.fromString(it))
} ?: throw NotFoundException("Constitution $values not found")
}
}
convert<Constitution> {
decode { values, _ ->

View File

@@ -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<T : TargetI>(override var requester: Requester) : RepositoryI {
abstract fun findById(id: UUID): CommentEntity<T>?
@@ -127,10 +127,24 @@ class CommentArticle(requester: Requester) : Comment<ArticleRef>(requester) {
)
}
}
override fun findByTarget(
target: UuidEntityI,
page: Int,
limit: Int
): Paginated<CommentEntity<ArticleRef>> {
return requester.run {
getFunction("find_comments_by_target")
.select(
page, limit,
"target_id" to target.id
)
}
}
}
class CommentConstitution(requester: Requester) : Comment<ConstitutionEntity>(requester) {
override fun findById(id: UUID): CommentEntity<ConstitutionEntity>? {
class CommentConstitution(requester: Requester) : Comment<ConstitutionRef>(requester) {
override fun findById(id: UUID): CommentEntity<ConstitutionRef>? {
return requester
.getFunction("find_comment_by_id")
.selectOne(mapOf("id" to id))
@@ -140,13 +154,27 @@ class CommentConstitution(requester: Requester) : Comment<ConstitutionEntity>(re
citizen: CitizenEntity,
page: Int,
limit: Int
): Paginated<CommentEntity<ConstitutionEntity>> {
): Paginated<CommentEntity<ConstitutionRef>> {
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<CommentEntity<ConstitutionRef>> {
return requester.run {
getFunction("find_comments_by_target")
.select(
page, limit,
"target_id" to target.id
)
}
}

View File

@@ -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)

View File

@@ -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"