add votes to article

This commit is contained in:
2019-09-18 01:06:40 +02:00
parent a5b55c2d87
commit acc6ecf114
8 changed files with 52 additions and 3 deletions

View File

@@ -19,6 +19,9 @@ class Article(
EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy),
EntityDeletedAt by EntityDeletedAtImp() {
val votes = VoteAggregation(0,0,0)
init {
tags = tags.distinct()
}

View File

@@ -0,0 +1,10 @@
package fr.dcproject.entity
import fr.postgresjson.entity.EntityUpdatedAt
import fr.postgresjson.entity.EntityUpdatedAtImp
open class VoteAggregation (
val up: Int,
val neutral: Int,
val down: Int
): EntityUpdatedAt by EntityUpdatedAtImp()

View File

@@ -7,7 +7,8 @@ begin
from (
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by
find_citizen_by_id(a.created_by_id) as created_by,
count_vote('article', a.id) as votes
into resource
from article as a
where a.id = _id

View File

@@ -15,6 +15,7 @@ begin
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by,
count_vote('article', a.id) as votes,
zdb.score(a.ctid) _score
from article as a
where (

View File

@@ -14,7 +14,8 @@ begin
from (
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by
find_citizen_by_id(a.created_by_id) as created_by,
count_vote('article', a.id) as votes
from article as a
where a.version_id = _version_id
order by a.version_number desc

View File

@@ -7,7 +7,8 @@ begin
from (
select
a.*,
find_citizen_by_id(a.created_by_id) as created_by
find_citizen_by_id(a.created_by_id) as created_by,
count_vote('article', a.id) as votes
into resource
from article as a
where a.version_id = _version_id

View File

@@ -0,0 +1,29 @@
create or replace function count_vote(reference regclass, _target_id uuid, out resource json)
language plpgsql as
$$
declare
agg jsonb;
empty jsonb = '{"down":0,"neutral":0,"up":0}'::jsonb;
begin
select jsonb_object_agg(t.label, t.total)
into agg
from (
select
count(v.note) as total,
(case v.note
when -1 then 'down'
when 0 then 'neutral'
when 1 then 'up'
end) as label
from vote v
where v.target_reference = reference
and v.target_id = _target_id
group by v.note
order by v.note
) t;
resource = coalesce(agg, empty) || jsonb_build_object('updated_at', now());
end;
$$;
-- drop function if exists count_vote(regclass,uuid);