From 967b370a41af2de29f512dffea061ef9e5a27687 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 27 Feb 2020 17:14:56 +0100 Subject: [PATCH] implement route /articles/{article}/follows create repository for find_follow --- src/main/kotlin/fr/dcproject/entity/Extra.kt | 2 +- .../kotlin/fr/dcproject/repository/Follow.kt | 18 ++++++++++++++---- .../fr/dcproject/routes/FollowArticle.kt | 7 +++++++ src/main/resources/openapi.yaml | 16 ++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/entity/Extra.kt b/src/main/kotlin/fr/dcproject/entity/Extra.kt index ce24a38..6bdf4a0 100644 --- a/src/main/kotlin/fr/dcproject/entity/Extra.kt +++ b/src/main/kotlin/fr/dcproject/entity/Extra.kt @@ -40,7 +40,7 @@ interface TargetI : UuidEntityI { t.isSubclassOf(ConstitutionRef::class) -> TargetName.Constitution.targetReference t.isSubclassOf(CommentRef::class) -> TargetName.Comment.targetReference t.isSubclassOf(Opinion::class) -> TargetName.Opinion.targetReference - else -> throw error("target not implemented: ${t.qualifiedName}") + else -> throw error("target not implemented: ${t.qualifiedName} \nImplement it or return 'reference' from SQL") } } diff --git a/src/main/kotlin/fr/dcproject/repository/Follow.kt b/src/main/kotlin/fr/dcproject/repository/Follow.kt index 5946ea3..b46671f 100644 --- a/src/main/kotlin/fr/dcproject/repository/Follow.kt +++ b/src/main/kotlin/fr/dcproject/repository/Follow.kt @@ -12,7 +12,7 @@ import fr.dcproject.entity.Article as ArticleEntity import fr.dcproject.entity.Constitution as ConstitutionEntity import fr.dcproject.entity.Follow as FollowEntity -open class Follow(override var requester: Requester) : RepositoryI { +open class Follow(override var requester: Requester) : RepositoryI { open fun findByCitizen( citizen: CitizenI, page: Int = 1, @@ -25,13 +25,12 @@ open class Follow(override var requester: Requester) : RepositoryI page: Int = 1, limit: Int = 50 ): Paginated> { - return requester.run { - getFunction("find_follows_by_citizen") + return requester + .getFunction("find_follows_by_citizen") .select( page, limit, "created_by_id" to citizenId ) - } } fun follow(follow: FollowEntity) { @@ -53,6 +52,17 @@ open class Follow(override var requester: Requester) : RepositoryI "created_by_id" to follow.createdBy.id ) } + + open fun findFollow( + citizen: CitizenI, + target: UuidEntity + ): FollowEntity? = + requester + .getFunction("find_follow") + .selectOne( + "citizen_id" to citizen.id, + "target_id" to target.id + ) } class FollowArticle(requester: Requester) : Follow(requester) { diff --git a/src/main/kotlin/fr/dcproject/routes/FollowArticle.kt b/src/main/kotlin/fr/dcproject/routes/FollowArticle.kt index 90675b3..c389ffd 100644 --- a/src/main/kotlin/fr/dcproject/routes/FollowArticle.kt +++ b/src/main/kotlin/fr/dcproject/routes/FollowArticle.kt @@ -38,6 +38,13 @@ fun Route.followArticle(repo: FollowArticleRepository) { call.respond(HttpStatusCode.NoContent) } + get { + repo.findFollow(citizen, it.article)?.let { follow -> + assertCan(VIEW, follow) + call.respond(follow) + } ?: call.respond(HttpStatusCode.NotFound) + } + get { val follows = repo.findByCitizen(it.citizen) assertCan(VIEW, follows.result) diff --git a/src/main/resources/openapi.yaml b/src/main/resources/openapi.yaml index 1ef9f36..cbc81a5 100644 --- a/src/main/resources/openapi.yaml +++ b/src/main/resources/openapi.yaml @@ -533,6 +533,22 @@ paths: /articles/{article}/follows: parameters: - $ref: '#/components/parameters/article' + get: + security: + - JWTAuth: [] + summary: Return Follow or nothing if you not follow + tags: + - follow + - article + responses: + 200: + description: Return your follow + content: + application/json: + schema: + $ref: '#/components/schemas/FollowResponse' + 404: + description: You not follow this article post: security: - JWTAuth: []