diff --git a/src/main/kotlin/fr/dcproject/entity/Article.kt b/src/main/kotlin/fr/dcproject/entity/Article.kt index ee5b863..65a3c98 100644 --- a/src/main/kotlin/fr/dcproject/entity/Article.kt +++ b/src/main/kotlin/fr/dcproject/entity/Article.kt @@ -19,6 +19,9 @@ class Article( EntityCreatedAt by EntityCreatedAtImp(), EntityCreatedBy by EntityCreatedByImp(createdBy), EntityDeletedAt by EntityDeletedAtImp() { + + val votes = VoteAggregation(0,0,0) + init { tags = tags.distinct() } diff --git a/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt b/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt new file mode 100644 index 0000000..6487af7 --- /dev/null +++ b/src/main/kotlin/fr/dcproject/entity/VoteAggregation.kt @@ -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() diff --git a/src/main/resources/sql/functions/article/find_article_by_id.sql b/src/main/resources/sql/functions/article/find_article_by_id.sql index b462a38..2cd4cdd 100644 --- a/src/main/resources/sql/functions/article/find_article_by_id.sql +++ b/src/main/resources/sql/functions/article/find_article_by_id.sql @@ -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 diff --git a/src/main/resources/sql/functions/article/find_articles.sql b/src/main/resources/sql/functions/article/find_articles.sql index e242d14..e6103d8 100644 --- a/src/main/resources/sql/functions/article/find_articles.sql +++ b/src/main/resources/sql/functions/article/find_articles.sql @@ -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 ( diff --git a/src/main/resources/sql/functions/article/find_articles_versions_by_version_id.sql b/src/main/resources/sql/functions/article/find_articles_versions_by_version_id.sql index 5df42bc..aabc062 100644 --- a/src/main/resources/sql/functions/article/find_articles_versions_by_version_id.sql +++ b/src/main/resources/sql/functions/article/find_articles_versions_by_version_id.sql @@ -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 diff --git a/src/main/resources/sql/functions/article/find_last_article_by_version_id.sql b/src/main/resources/sql/functions/article/find_last_article_by_version_id.sql index 6e0c923..59982f9 100644 --- a/src/main/resources/sql/functions/article/find_last_article_by_version_id.sql +++ b/src/main/resources/sql/functions/article/find_last_article_by_version_id.sql @@ -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 diff --git a/src/main/resources/sql/functions/vote/count_vote.sql b/src/main/resources/sql/functions/vote/count_vote.sql new file mode 100644 index 0000000..5a423cf --- /dev/null +++ b/src/main/resources/sql/functions/vote/count_vote.sql @@ -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); \ No newline at end of file diff --git a/src/test/sql/vote.sql b/src/test/sql/vote.sql index 59e5b24..678c49b 100644 --- a/src/test/sql/vote.sql +++ b/src/test/sql/vote.sql @@ -24,6 +24,7 @@ declare ] } $json$; + votes jsonb; begin -- insert user for context select insert_user(created_user) into created_user; @@ -67,6 +68,8 @@ begin exception when check_violation then 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 from vote;