VoteAggregation return total votes and the score

This commit is contained in:
2019-10-16 16:27:06 +02:00
parent 54393000b3
commit 32417d3276
4 changed files with 26 additions and 6 deletions

View File

@@ -5,5 +5,5 @@ interface Votable {
}
class VotableImp : Votable {
override var votes: VoteAggregation = VoteAggregation(0, 0, 0)
override var votes: VoteAggregation = VoteAggregation()
}

View File

@@ -7,6 +7,10 @@ import fr.postgresjson.entity.EntityUpdatedAtImp
open class VoteAggregation(
val up: Int,
val neutral: Int,
val down: Int
val down: Int,
val total: Int,
val score: Int
) : EntityI,
EntityUpdatedAt by EntityUpdatedAtImp()
EntityUpdatedAt by EntityUpdatedAtImp() {
constructor() : this(0, 0, 0, 0, 0)
}

View File

@@ -1153,6 +1153,11 @@ components:
down:
type: number
minimum: 0
total:
type: number
minimum: 0
score:
type: number
- $ref: '#/components/schemas/UpdatedAt'
Votable:
type: object

View File

@@ -3,7 +3,9 @@ create or replace function count_vote(_target_id uuid, out resource json)
$$
declare
agg jsonb;
empty jsonb = '{"down":0,"neutral":0,"up":0}'::jsonb;
empty jsonb = '{"down":0,"neutral":0,"up":0,"total":0,"score":0}'::jsonb;
score int;
total int;
begin
select jsonb_object_agg(t.label, t.total)
into agg
@@ -21,8 +23,17 @@ begin
order by v.note
) t;
resource = coalesce(agg, empty) || jsonb_build_object('updated_at', now());
agg = empty || coalesce(agg, empty);
score = ((agg->>'up')::int - (agg->>'down')::int);
total = ((agg->>'up')::int + (agg->>'down')::int + (agg->>'neutral')::int);
resource = agg ||
jsonb_build_object('updated_at', now()) ||
jsonb_build_object('total', total) ||
jsonb_build_object('score', score);
end;
$$;
-- drop function if exists count_vote(uuid);
select * from count_vote('ced1563f-ecf5-4f11-8518-8aeceff3c13a');