diff --git a/src/main/kotlin/fr/dcproject/entity/Votable.kt b/src/main/kotlin/fr/dcproject/entity/Votable.kt index 49983e2..c2725f7 100644 --- a/src/main/kotlin/fr/dcproject/entity/Votable.kt +++ b/src/main/kotlin/fr/dcproject/entity/Votable.kt @@ -5,5 +5,5 @@ interface Votable { } class VotableImp : Votable { - override var votes: VoteAggregation = VoteAggregation(0, 0, 0) + override var votes: VoteAggregation = VoteAggregation() } \ No newline at end of file diff --git a/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt b/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt index ca8a0f5..c19bdcb 100644 --- a/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt +++ b/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt @@ -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) +} diff --git a/src/main/resources/openApi.yaml b/src/main/resources/openApi.yaml index 61e7d16..44fadf0 100644 --- a/src/main/resources/openApi.yaml +++ b/src/main/resources/openApi.yaml @@ -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 diff --git a/src/main/resources/sql/functions/vote/count_vote.sql b/src/main/resources/sql/functions/vote/count_vote.sql index 07696a2..001b73f 100644 --- a/src/main/resources/sql/functions/vote/count_vote.sql +++ b/src/main/resources/sql/functions/vote/count_vote.sql @@ -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); \ No newline at end of file +-- drop function if exists count_vote(uuid); + +select * from count_vote('ced1563f-ecf5-4f11-8518-8aeceff3c13a'); \ No newline at end of file