return votes after vote
This commit is contained in:
@@ -3,8 +3,8 @@ package fr.dcproject.entity
|
||||
import fr.postgresjson.entity.*
|
||||
import java.util.*
|
||||
|
||||
interface ExtraI <T: EntityI<UUID>>:
|
||||
EntityI<UUID>,
|
||||
interface ExtraI <T: EntityI>:
|
||||
EntityI,
|
||||
EntityCreatedAt,
|
||||
EntityCreatedBy<Citizen>{
|
||||
var target: T
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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 <T: UuidEntity>(override var requester: Requester): RepositoryI<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 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
|
||||
)
|
||||
)!!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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'
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
$$;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user