diff --git a/src/main/kotlin/fr/dcproject/entity/Article.kt b/src/main/kotlin/fr/dcproject/entity/Article.kt index 65a3c98..53b061d 100644 --- a/src/main/kotlin/fr/dcproject/entity/Article.kt +++ b/src/main/kotlin/fr/dcproject/entity/Article.kt @@ -18,9 +18,8 @@ class Article( EntityVersioning by UuidEntityVersioning(), EntityCreatedAt by EntityCreatedAtImp(), EntityCreatedBy by EntityCreatedByImp(createdBy), - EntityDeletedAt by EntityDeletedAtImp() { - - val votes = VoteAggregation(0,0,0) + EntityDeletedAt by EntityDeletedAtImp(), + Votable by VotableImp() { init { tags = tags.distinct() diff --git a/src/main/kotlin/fr/dcproject/entity/Comment.kt b/src/main/kotlin/fr/dcproject/entity/Comment.kt index 4d5a2a6..ccdca2b 100644 --- a/src/main/kotlin/fr/dcproject/entity/Comment.kt +++ b/src/main/kotlin/fr/dcproject/entity/Comment.kt @@ -14,4 +14,5 @@ open class Comment ( val childrenCount: Int? = null ): Extra(id, createdBy, target), EntityUpdatedAt by EntityUpdatedAtImp(), - EntityDeletedAt by EntityDeletedAtImp() + EntityDeletedAt by EntityDeletedAtImp(), + Votable by VotableImp() diff --git a/src/main/kotlin/fr/dcproject/entity/Votable.kt b/src/main/kotlin/fr/dcproject/entity/Votable.kt new file mode 100644 index 0000000..ada0ae9 --- /dev/null +++ b/src/main/kotlin/fr/dcproject/entity/Votable.kt @@ -0,0 +1,9 @@ +package fr.dcproject.entity + +interface Votable { + var votes: VoteAggregation +} + +class VotableImp: Votable { + override var votes: VoteAggregation = VoteAggregation(0,0,0) +} \ No newline at end of file diff --git a/src/main/resources/openApi.yaml b/src/main/resources/openApi.yaml index 3f163d6..4621585 100644 --- a/src/main/resources/openApi.yaml +++ b/src/main/resources/openApi.yaml @@ -833,9 +833,7 @@ components: - $ref: '#/components/schemas/CreatedBy' - $ref: '#/components/schemas/CreatedAt' - $ref: '#/components/schemas/lastVersion' - - properties: - votes: - $ref: '#/components/schemas/VoteAggregation' + - $ref: '#/components/schemas/Votable' ArticleRequest: $ref: '#/components/schemas/ArticleBase' @@ -956,6 +954,7 @@ components: - $ref: '#/components/schemas/CommentBase' - $ref: '#/components/schemas/UpdatedAt' - $ref: '#/components/schemas/Extra' + - $ref: '#/components/schemas/Votable' - type: object properties: parents_ids: @@ -1007,7 +1006,11 @@ components: type: number minimum: 0 - $ref: '#/components/schemas/UpdatedAt' - + Votable: + type: object + properties: + votes: + $ref: '#/components/schemas/VoteAggregation' 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 2cd4cdd..01f5964 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 @@ -8,7 +8,7 @@ begin select a.*, find_citizen_by_id(a.created_by_id) as created_by, - count_vote('article', a.id) as votes + count_vote(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 87f91b0..d181806 100644 --- a/src/main/resources/sql/functions/article/find_articles.sql +++ b/src/main/resources/sql/functions/article/find_articles.sql @@ -15,7 +15,7 @@ begin select a.*, find_citizen_by_id(a.created_by_id) as created_by, - count_vote('article', a.id) as votes, + count_vote(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 aabc062..3c16167 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 @@ -15,7 +15,7 @@ begin select a.*, find_citizen_by_id(a.created_by_id) as created_by, - count_vote('article', a.id) as votes + count_vote(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 59982f9..c88ec2f 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 @@ -8,7 +8,7 @@ begin select a.*, find_citizen_by_id(a.created_by_id) as created_by, - count_vote('article', a.id) as votes + count_vote(a.id) as votes into resource from article as a where a.version_id = _version_id diff --git a/src/main/resources/sql/functions/comment/find_comment_by_id.sql b/src/main/resources/sql/functions/comment/find_comment_by_id.sql index 40fe22d..fdbafdc 100644 --- a/src/main/resources/sql/functions/comment/find_comment_by_id.sql +++ b/src/main/resources/sql/functions/comment/find_comment_by_id.sql @@ -10,7 +10,8 @@ begin select com.*, find_reference_by_id(com.target_id, com.target_reference) as target, - find_citizen_by_id(com.created_by_id) as created_by + find_citizen_by_id(com.created_by_id) as created_by, + count_vote(com.id) as votes from "comment" as com where id = _id ) as t; diff --git a/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql b/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql index 40a80b1..900d3d9 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_citizen.sql @@ -21,7 +21,8 @@ begin select com.*, find_reference_by_id(com.target_id, _reference) as target, - find_citizen_by_id(com.created_by_id) as created_by + find_citizen_by_id(com.created_by_id) as created_by, + count_vote(com.id) as votes from "comment" as com diff --git a/src/main/resources/sql/functions/comment/find_comments_by_parent.sql b/src/main/resources/sql/functions/comment/find_comments_by_parent.sql index a842ad3..fca1991 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_parent.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_parent.sql @@ -14,7 +14,8 @@ begin com.*, (select count(*) from "comment" c2 where c2.parents_ids @> array[com.id]) as children_count, find_reference_by_id(com.target_id, com.target_reference) as target, - find_citizen_by_id(com.created_by_id) as created_by + find_citizen_by_id(com.created_by_id) as created_by, + count_vote(com.id) as votes from "comment" as com where parent_id = _parent_id order by created_at asc, diff --git a/src/main/resources/sql/functions/comment/find_comments_by_target.sql b/src/main/resources/sql/functions/comment/find_comments_by_target.sql index 4d28088..5204157 100644 --- a/src/main/resources/sql/functions/comment/find_comments_by_target.sql +++ b/src/main/resources/sql/functions/comment/find_comments_by_target.sql @@ -14,7 +14,8 @@ begin com.*, (select count(*) from "comment" c2 where c2.parents_ids @> array[com.id]) as children_count, find_reference_by_id(com.target_id, com.target_reference) as target, - find_citizen_by_id(com.created_by_id) as created_by + find_citizen_by_id(com.created_by_id) as created_by, + count_vote(com.id) as votes from "comment" as com where com.target_id = _target_id order by created_at asc, diff --git a/src/main/resources/sql/functions/vote/count_vote.sql b/src/main/resources/sql/functions/vote/count_vote.sql index 5a423cf..07696a2 100644 --- a/src/main/resources/sql/functions/vote/count_vote.sql +++ b/src/main/resources/sql/functions/vote/count_vote.sql @@ -1,4 +1,4 @@ -create or replace function count_vote(reference regclass, _target_id uuid, out resource json) +create or replace function count_vote(_target_id uuid, out resource json) language plpgsql as $$ declare @@ -16,8 +16,7 @@ begin when 1 then 'up' end) as label from vote v - where v.target_reference = reference - and v.target_id = _target_id + where v.target_id = _target_id group by v.note order by v.note ) t; @@ -26,4 +25,4 @@ begin end; $$; --- drop function if exists count_vote(regclass,uuid); \ No newline at end of file +-- drop function if exists count_vote(uuid); \ No newline at end of file diff --git a/src/main/resources/sql/functions/vote/vote.sql b/src/main/resources/sql/functions/vote/vote.sql index d0324bf..45ce410 100644 --- a/src/main/resources/sql/functions/vote/vote.sql +++ b/src/main/resources/sql/functions/vote/vote.sql @@ -34,7 +34,7 @@ begin raise exception '% no implemented for vote', reference::text; end if; - select count_vote(reference, _target_id) into resource; + select count_vote(_target_id) into resource; end; $$; diff --git a/src/test/sql/vote.sql b/src/test/sql/vote.sql index 5dccebd..498cbb5 100644 --- a/src/test/sql/vote.sql +++ b/src/test/sql/vote.sql @@ -87,7 +87,7 @@ begin exception when check_violation then end; - select count_vote('article', '933b6a1b-50c9-42b6-989f-c02a57814ef9') into votes; + select count_vote('933b6a1b-50c9-42b6-989f-c02a57814ef9') into votes; assert ((votes->>'up')::int = 0), 'vote.up must be 0'; -- Test "find_votes_by_citizen"