add votes to article
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
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 (
|
||||
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
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
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);
|
||||
Reference in New Issue
Block a user