From 742927a59017c976f14d2ad04242019b48b8a237 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Thu, 5 Sep 2019 17:15:54 +0200 Subject: [PATCH] Add route for get all versions of one article --- .../kotlin/fr/dcproject/repository/Article.kt | 6 ++++ .../kotlin/fr/dcproject/routes/Article.kt | 12 +++++++ src/main/resources/openApi.yaml | 32 +++++++++++++++++-- src/test/resources/feature/articles.feature | 4 +++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/repository/Article.kt b/src/main/kotlin/fr/dcproject/repository/Article.kt index 0a229dc..14cb0ad 100644 --- a/src/main/kotlin/fr/dcproject/repository/Article.kt +++ b/src/main/kotlin/fr/dcproject/repository/Article.kt @@ -16,6 +16,12 @@ class Article(override var requester: Requester) : RepositoryI { return function.selectOne("id" to id) } + fun findVerionsByVersionsId(page: Int = 1, limit: Int = 50, versionId: UUID): Paginated { + return requester + .getFunction("find_articles_versions_by_version_id") + .select(page, limit, "version_id" to versionId) + } + fun find( page: Int = 1, limit: Int = 50, diff --git a/src/main/kotlin/fr/dcproject/routes/Article.kt b/src/main/kotlin/fr/dcproject/routes/Article.kt index bcd84ff..42014cd 100644 --- a/src/main/kotlin/fr/dcproject/routes/Article.kt +++ b/src/main/kotlin/fr/dcproject/routes/Article.kt @@ -23,6 +23,10 @@ object ArticlesPaths { val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit } @Location("/articles/{article}") class ArticleRequest(val article: ArticleEntity) + @Location("/articles/{article}/versions") class ArticleVersionsRequest(val article: ArticleEntity, page: Int = 1, limit: Int = 50, val sort: String? = null, val direction: RepositoryI.Direction? = null, val search: String? = null) { + val page: Int = if (page < 1) 1 else page + val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit + } @Location("/articles") class PostArticleRequest } @@ -40,6 +44,14 @@ fun Route.article(repo: ArticleRepository) { call.respond(it.article) } + get { + assertCan(VIEW, it.article) + + val versions = repo.findVerionsByVersionsId(it.page, it.limit, it.article.versionId) + + call.respond(versions) + } + post { val article = call.receive() article.createdBy = citizen diff --git a/src/main/resources/openApi.yaml b/src/main/resources/openApi.yaml index 9c08704..721ff06 100644 --- a/src/main/resources/openApi.yaml +++ b/src/main/resources/openApi.yaml @@ -159,9 +159,35 @@ paths: content: application/json: schema: - type: array - items: - $ref: '#/components/schemas/ArticleResponse' + $ref: '#/components/schemas/ArticleResponse' + /articles/{article}/versions: + get: + summary: Get all versions of articles + tags: + - article + operationId: getArticleVersions + parameters: + - name: article + in: path + required: true + description: the ID of article + schema: + type: string + format: uuid + responses: + 200: + description: The versions of Article + content: + application/json: + schema: + allOf: + - $ref: '#/components/schemas/Paginated' + - type: object + properties: + result: + type: array + items: + $ref: '#/components/schemas/ArticleResponse' /constitutions: get: diff --git a/src/test/resources/feature/articles.feature b/src/test/resources/feature/articles.feature index 000e4e9..5bdd35d 100644 --- a/src/test/resources/feature/articles.feature +++ b/src/test/resources/feature/articles.feature @@ -4,6 +4,10 @@ Feature: articles routes When I send a GET request to "/articles" Then the response status code should be 200 + Scenario: Can get versions of article by the id + When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b/versions" + Then the response status code should be 200 + Scenario: The route for get one article must response a 200 and return article When I send a GET request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b" Then the response status code should be 200