add votes to article
This commit is contained in:
@@ -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()
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt
Normal file
10
src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt
Normal 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()
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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 (
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
29
src/main/resources/sql/functions/vote/count_vote.sql
Normal file
29
src/main/resources/sql/functions/vote/count_vote.sql
Normal 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);
|
||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user