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(), EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy), EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy),
EntityDeletedAt by EntityDeletedAtImp() { EntityDeletedAt by EntityDeletedAtImp() {
val votes = VoteAggregation(0,0,0)
init { init {
tags = tags.distinct() 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 ( from (
select select
a.*, 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 into resource
from article as a from article as a
where a.id = _id where a.id = _id

View File

@@ -15,6 +15,7 @@ begin
select select
a.*, 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,
zdb.score(a.ctid) _score zdb.score(a.ctid) _score
from article as a from article as a
where ( where (

View File

@@ -14,7 +14,8 @@ begin
from ( from (
select select
a.*, 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 from article as a
where a.version_id = _version_id where a.version_id = _version_id
order by a.version_number desc order by a.version_number desc

View File

@@ -7,7 +7,8 @@ begin
from ( from (
select select
a.*, 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 into resource
from article as a from article as a
where a.version_id = _version_id 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);

View File

@@ -24,6 +24,7 @@ declare
] ]
} }
$json$; $json$;
votes jsonb;
begin begin
-- insert user for context -- insert user for context
select insert_user(created_user) into created_user; select insert_user(created_user) into created_user;
@@ -67,6 +68,8 @@ begin
exception when check_violation then exception when check_violation then
end; end;
select count_vote('article', '933b6a1b-50c9-42b6-989f-c02a57814ef9') into votes;
assert ((votes->>'up')::int = 0), 'vote.up must be 0';
-- delete vote and context -- delete vote and context
delete from vote; delete from vote;