From a37afc1ada13b047e551732590e573a084800174 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Fri, 20 Sep 2019 22:42:50 +0200 Subject: [PATCH] return votes after vote --- src/main/kotlin/fr/dcproject/entity/Extra.kt | 4 +-- .../fr/dcproject/entity/VoteAggregation.kt | 4 ++- .../kotlin/fr/dcproject/repository/Vote.kt | 9 ++++--- .../kotlin/fr/dcproject/routes/VoteArticle.kt | 4 +-- src/main/resources/openApi.yaml | 26 +++++++++++++++++++ .../resources/sql/functions/vote/vote.sql | 4 ++- 6 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/fr/dcproject/entity/Extra.kt b/src/main/kotlin/fr/dcproject/entity/Extra.kt index e5743aa..0540c99 100644 --- a/src/main/kotlin/fr/dcproject/entity/Extra.kt +++ b/src/main/kotlin/fr/dcproject/entity/Extra.kt @@ -3,8 +3,8 @@ package fr.dcproject.entity import fr.postgresjson.entity.* import java.util.* -interface ExtraI >: - EntityI, +interface ExtraI : + EntityI, EntityCreatedAt, EntityCreatedBy{ var target: T diff --git a/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt b/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt index 6487af7..4740195 100644 --- a/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt +++ b/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt @@ -1,5 +1,6 @@ package fr.dcproject.entity +import fr.postgresjson.entity.EntityI import fr.postgresjson.entity.EntityUpdatedAt import fr.postgresjson.entity.EntityUpdatedAtImp @@ -7,4 +8,5 @@ open class VoteAggregation ( val up: Int, val neutral: Int, val down: Int -): EntityUpdatedAt by EntityUpdatedAtImp() +): EntityI, + EntityUpdatedAt by EntityUpdatedAtImp() diff --git a/src/main/kotlin/fr/dcproject/repository/Vote.kt b/src/main/kotlin/fr/dcproject/repository/Vote.kt index 0cd241f..f3f374c 100644 --- a/src/main/kotlin/fr/dcproject/repository/Vote.kt +++ b/src/main/kotlin/fr/dcproject/repository/Vote.kt @@ -2,6 +2,7 @@ package fr.dcproject.repository import fr.dcproject.entity.Article import fr.dcproject.entity.Constitution +import fr.dcproject.entity.VoteAggregation import fr.postgresjson.connexion.Requester import fr.postgresjson.entity.UuidEntity import fr.postgresjson.repository.RepositoryI @@ -11,19 +12,19 @@ import fr.dcproject.entity.Vote as VoteEntity open class Vote (override var requester: Requester): RepositoryI> { override val entityName = VoteEntity::class as KClass> - fun vote(vote: VoteEntity) { + fun vote(vote: VoteEntity): VoteAggregation { val reference = vote.target::class.simpleName!!.toLowerCase() val author = vote.createdBy ?: error("vote must be contain an author") val anonymous = author.voteAnonymous - requester + return requester .getFunction("vote") - .sendQuery( + .selectOne( "reference" to reference, "target_id" to vote.target.id, "note" to vote.note, "created_by_id" to author.id, "anonymous" to anonymous - ) + )!! } } diff --git a/src/main/kotlin/fr/dcproject/routes/VoteArticle.kt b/src/main/kotlin/fr/dcproject/routes/VoteArticle.kt index f7c7950..3d43420 100644 --- a/src/main/kotlin/fr/dcproject/routes/VoteArticle.kt +++ b/src/main/kotlin/fr/dcproject/routes/VoteArticle.kt @@ -35,7 +35,7 @@ fun Route.voteArticle(repo: VoteArticleRepository) { createdBy = this.citizen ) assertCan(CREATE, vote) - repo.vote(vote) - call.respond(HttpStatusCode.Created) + val votes = repo.vote(vote) + call.respond(HttpStatusCode.Created, votes) } } \ No newline at end of file diff --git a/src/main/resources/openApi.yaml b/src/main/resources/openApi.yaml index 1017151..c65ddce 100644 --- a/src/main/resources/openApi.yaml +++ b/src/main/resources/openApi.yaml @@ -444,6 +444,11 @@ paths: responses: 201: description: Return only http status 201 on success + + content: + application/json: + schema: + $ref: '#/components/schemas/VoteAggregation' /constitutions/{constitution}/vote: parameters: - name: constitution @@ -716,6 +721,10 @@ components: - $ref: '#/components/schemas/CreatedBy' - $ref: '#/components/schemas/CreatedAt' - $ref: '#/components/schemas/lastVersion' + - properties: + votes: + $ref: '#/components/schemas/VoteAggregation' + ArticleRequest: $ref: '#/components/schemas/ArticleBase' @@ -872,6 +881,23 @@ components: allOf: - $ref: '#/components/schemas/VoteBase' - $ref: '#/components/schemas/Extra' + VoteAggregation: + allOf: + - type: object + properties: + up: + type: number + minimum: 0 + neutral: + type: number + minimum: 0 + down: + type: number + minimum: 0 + - $ref: '#/components/schemas/UpdatedAt' + + + diff --git a/src/main/resources/sql/functions/vote/vote.sql b/src/main/resources/sql/functions/vote/vote.sql index 2f9a22f..9e27589 100644 --- a/src/main/resources/sql/functions/vote/vote.sql +++ b/src/main/resources/sql/functions/vote/vote.sql @@ -1,4 +1,4 @@ -create or replace function vote(reference regclass, _target_id uuid, _created_by_id uuid, _note int, _anonymous bool default true) returns void +create or replace function vote(reference regclass, _target_id uuid, _created_by_id uuid, _note int, _anonymous bool default true, out resource json) language plpgsql as $$ begin @@ -29,6 +29,8 @@ begin else raise exception '% no implemented', reference::text; end if; + + select count_vote(reference, _target_id) into resource; end; $$;