SQL functions find_comments_* now return VotesAggregates

create Votable Interface
remove usless "resourceTarget" argument for SQL function "count_vote"
This commit is contained in:
2019-10-08 00:26:41 +02:00
parent f20964878f
commit c156e2a7b1
15 changed files with 37 additions and 22 deletions

View File

@@ -18,9 +18,8 @@ class Article(
EntityVersioning<UUID, Int> by UuidEntityVersioning(), EntityVersioning<UUID, Int> by UuidEntityVersioning(),
EntityCreatedAt by EntityCreatedAtImp(), EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy), EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy),
EntityDeletedAt by EntityDeletedAtImp() { EntityDeletedAt by EntityDeletedAtImp(),
Votable by VotableImp() {
val votes = VoteAggregation(0,0,0)
init { init {
tags = tags.distinct() tags = tags.distinct()

View File

@@ -14,4 +14,5 @@ open class Comment <T: UuidEntity> (
val childrenCount: Int? = null val childrenCount: Int? = null
): Extra<T>(id, createdBy, target), ): Extra<T>(id, createdBy, target),
EntityUpdatedAt by EntityUpdatedAtImp(), EntityUpdatedAt by EntityUpdatedAtImp(),
EntityDeletedAt by EntityDeletedAtImp() EntityDeletedAt by EntityDeletedAtImp(),
Votable by VotableImp()

View File

@@ -0,0 +1,9 @@
package fr.dcproject.entity
interface Votable {
var votes: VoteAggregation
}
class VotableImp: Votable {
override var votes: VoteAggregation = VoteAggregation(0,0,0)
}

View File

@@ -833,9 +833,7 @@ components:
- $ref: '#/components/schemas/CreatedBy' - $ref: '#/components/schemas/CreatedBy'
- $ref: '#/components/schemas/CreatedAt' - $ref: '#/components/schemas/CreatedAt'
- $ref: '#/components/schemas/lastVersion' - $ref: '#/components/schemas/lastVersion'
- properties: - $ref: '#/components/schemas/Votable'
votes:
$ref: '#/components/schemas/VoteAggregation'
ArticleRequest: ArticleRequest:
$ref: '#/components/schemas/ArticleBase' $ref: '#/components/schemas/ArticleBase'
@@ -956,6 +954,7 @@ components:
- $ref: '#/components/schemas/CommentBase' - $ref: '#/components/schemas/CommentBase'
- $ref: '#/components/schemas/UpdatedAt' - $ref: '#/components/schemas/UpdatedAt'
- $ref: '#/components/schemas/Extra' - $ref: '#/components/schemas/Extra'
- $ref: '#/components/schemas/Votable'
- type: object - type: object
properties: properties:
parents_ids: parents_ids:
@@ -1007,7 +1006,11 @@ components:
type: number type: number
minimum: 0 minimum: 0
- $ref: '#/components/schemas/UpdatedAt' - $ref: '#/components/schemas/UpdatedAt'
Votable:
type: object
properties:
votes:
$ref: '#/components/schemas/VoteAggregation'

View File

@@ -8,7 +8,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 count_vote(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,7 +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, count_vote(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

@@ -15,7 +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 count_vote(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

@@ -8,7 +8,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 count_vote(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

@@ -10,7 +10,8 @@ begin
select select
com.*, com.*,
find_reference_by_id(com.target_id, com.target_reference) as target, 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 from "comment" as com
where id = _id where id = _id
) as t; ) as t;

View File

@@ -21,7 +21,8 @@ begin
select select
com.*, com.*,
find_reference_by_id(com.target_id, _reference) as target, 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 from "comment" as com

View File

@@ -14,7 +14,8 @@ begin
com.*, com.*,
(select count(*) from "comment" c2 where c2.parents_ids @> array[com.id]) as children_count, (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_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 from "comment" as com
where parent_id = _parent_id where parent_id = _parent_id
order by created_at asc, order by created_at asc,

View File

@@ -14,7 +14,8 @@ begin
com.*, com.*,
(select count(*) from "comment" c2 where c2.parents_ids @> array[com.id]) as children_count, (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_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 from "comment" as com
where com.target_id = _target_id where com.target_id = _target_id
order by created_at asc, order by created_at asc,

View File

@@ -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 language plpgsql as
$$ $$
declare declare
@@ -16,8 +16,7 @@ begin
when 1 then 'up' when 1 then 'up'
end) as label end) as label
from vote v from vote v
where v.target_reference = reference where v.target_id = _target_id
and v.target_id = _target_id
group by v.note group by v.note
order by v.note order by v.note
) t; ) t;
@@ -26,4 +25,4 @@ begin
end; end;
$$; $$;
-- drop function if exists count_vote(regclass,uuid); -- drop function if exists count_vote(uuid);

View File

@@ -34,7 +34,7 @@ begin
raise exception '% no implemented for vote', reference::text; raise exception '% no implemented for vote', reference::text;
end if; end if;
select count_vote(reference, _target_id) into resource; select count_vote(_target_id) into resource;
end; end;
$$; $$;

View File

@@ -87,7 +87,7 @@ begin
exception when check_violation then exception when check_violation then
end; 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'; assert ((votes->>'up')::int = 0), 'vote.up must be 0';
-- Test "find_votes_by_citizen" -- Test "find_votes_by_citizen"