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(),
EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy),
EntityDeletedAt by EntityDeletedAtImp() {
val votes = VoteAggregation(0,0,0)
EntityDeletedAt by EntityDeletedAtImp(),
Votable by VotableImp() {
init {
tags = tags.distinct()

View File

@@ -14,4 +14,5 @@ open class Comment <T: UuidEntity> (
val childrenCount: Int? = null
): Extra<T>(id, createdBy, target),
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/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'

View File

@@ -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

View File

@@ -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 (

View File

@@ -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

View File

@@ -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

View File

@@ -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;

View File

@@ -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

View File

@@ -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,

View File

@@ -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,

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
$$
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);
-- drop function if exists count_vote(uuid);

View File

@@ -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;
$$;

View File

@@ -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"