return votes after vote

This commit is contained in:
2019-09-20 22:42:50 +02:00
parent 2311d3986e
commit a37afc1ada
6 changed files with 41 additions and 10 deletions

View File

@@ -3,8 +3,8 @@ package fr.dcproject.entity
import fr.postgresjson.entity.* import fr.postgresjson.entity.*
import java.util.* import java.util.*
interface ExtraI <T: EntityI<UUID>>: interface ExtraI <T: EntityI>:
EntityI<UUID>, EntityI,
EntityCreatedAt, EntityCreatedAt,
EntityCreatedBy<Citizen>{ EntityCreatedBy<Citizen>{
var target: T var target: T

View File

@@ -1,5 +1,6 @@
package fr.dcproject.entity package fr.dcproject.entity
import fr.postgresjson.entity.EntityI
import fr.postgresjson.entity.EntityUpdatedAt import fr.postgresjson.entity.EntityUpdatedAt
import fr.postgresjson.entity.EntityUpdatedAtImp import fr.postgresjson.entity.EntityUpdatedAtImp
@@ -7,4 +8,5 @@ open class VoteAggregation (
val up: Int, val up: Int,
val neutral: Int, val neutral: Int,
val down: Int val down: Int
): EntityUpdatedAt by EntityUpdatedAtImp() ): EntityI,
EntityUpdatedAt by EntityUpdatedAtImp()

View File

@@ -2,6 +2,7 @@ package fr.dcproject.repository
import fr.dcproject.entity.Article import fr.dcproject.entity.Article
import fr.dcproject.entity.Constitution import fr.dcproject.entity.Constitution
import fr.dcproject.entity.VoteAggregation
import fr.postgresjson.connexion.Requester import fr.postgresjson.connexion.Requester
import fr.postgresjson.entity.UuidEntity import fr.postgresjson.entity.UuidEntity
import fr.postgresjson.repository.RepositoryI import fr.postgresjson.repository.RepositoryI
@@ -11,19 +12,19 @@ import fr.dcproject.entity.Vote as VoteEntity
open class Vote <T: UuidEntity>(override var requester: Requester): RepositoryI<VoteEntity<T>> { open class Vote <T: UuidEntity>(override var requester: Requester): RepositoryI<VoteEntity<T>> {
override val entityName = VoteEntity::class as KClass<VoteEntity<T>> override val entityName = VoteEntity::class as KClass<VoteEntity<T>>
fun vote(vote: VoteEntity<T>) { fun vote(vote: VoteEntity<T>): VoteAggregation {
val reference = vote.target::class.simpleName!!.toLowerCase() val reference = vote.target::class.simpleName!!.toLowerCase()
val author = vote.createdBy ?: error("vote must be contain an author") val author = vote.createdBy ?: error("vote must be contain an author")
val anonymous = author.voteAnonymous val anonymous = author.voteAnonymous
requester return requester
.getFunction("vote") .getFunction("vote")
.sendQuery( .selectOne(
"reference" to reference, "reference" to reference,
"target_id" to vote.target.id, "target_id" to vote.target.id,
"note" to vote.note, "note" to vote.note,
"created_by_id" to author.id, "created_by_id" to author.id,
"anonymous" to anonymous "anonymous" to anonymous
) )!!
} }
} }

View File

@@ -35,7 +35,7 @@ fun Route.voteArticle(repo: VoteArticleRepository) {
createdBy = this.citizen createdBy = this.citizen
) )
assertCan(CREATE, vote) assertCan(CREATE, vote)
repo.vote(vote) val votes = repo.vote(vote)
call.respond(HttpStatusCode.Created) call.respond(HttpStatusCode.Created, votes)
} }
} }

View File

@@ -444,6 +444,11 @@ paths:
responses: responses:
201: 201:
description: Return only http status 201 on success description: Return only http status 201 on success
content:
application/json:
schema:
$ref: '#/components/schemas/VoteAggregation'
/constitutions/{constitution}/vote: /constitutions/{constitution}/vote:
parameters: parameters:
- name: constitution - name: constitution
@@ -716,6 +721,10 @@ components:
- $ref: '#/components/schemas/CreatedBy' - $ref: '#/components/schemas/CreatedBy'
- $ref: '#/components/schemas/CreatedAt' - $ref: '#/components/schemas/CreatedAt'
- $ref: '#/components/schemas/lastVersion' - $ref: '#/components/schemas/lastVersion'
- properties:
votes:
$ref: '#/components/schemas/VoteAggregation'
ArticleRequest: ArticleRequest:
$ref: '#/components/schemas/ArticleBase' $ref: '#/components/schemas/ArticleBase'
@@ -872,6 +881,23 @@ components:
allOf: allOf:
- $ref: '#/components/schemas/VoteBase' - $ref: '#/components/schemas/VoteBase'
- $ref: '#/components/schemas/Extra' - $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'

View File

@@ -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 language plpgsql as
$$ $$
begin begin
@@ -29,6 +29,8 @@ begin
else else
raise exception '% no implemented', reference::text; raise exception '% no implemented', reference::text;
end if; end if;
select count_vote(reference, _target_id) into resource;
end; end;
$$; $$;