update postgresjson

This commit is contained in:
2019-08-27 12:33:32 +02:00
parent 67665350eb
commit ff76bd55ef
29 changed files with 139 additions and 119 deletions

View File

@@ -15,6 +15,7 @@ import fr.dcproject.routes.*
import fr.dcproject.security.voter.ArticleVoter import fr.dcproject.security.voter.ArticleVoter
import fr.dcproject.security.voter.AuthorizationVoter import fr.dcproject.security.voter.AuthorizationVoter
import fr.dcproject.security.voter.CitizenVoter import fr.dcproject.security.voter.CitizenVoter
import fr.dcproject.security.voter.CommentVoter
import fr.postgresjson.migration.Migrations import fr.postgresjson.migration.Migrations
import io.ktor.application.Application import io.ktor.application.Application
import io.ktor.application.call import io.ktor.application.call
@@ -104,7 +105,8 @@ fun Application.module() {
install(AuthorizationVoter) { install(AuthorizationVoter) {
voters = mutableListOf( voters = mutableListOf(
ArticleVoter(), ArticleVoter(),
CitizenVoter() CitizenVoter(),
CommentVoter()
) )
} }

View File

@@ -15,4 +15,4 @@ class Article(
UuidEntity(id), UuidEntity(id),
EntityVersioning<UUID, Int> by UuidEntityVersioning(), EntityVersioning<UUID, Int> by UuidEntityVersioning(),
EntityCreatedAt by EntityCreatedAtImp(), EntityCreatedAt by EntityCreatedAtImp(),
CreatedBy<Citizen> by EntityCreatedByImp(createdBy) EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy)

View File

@@ -12,7 +12,7 @@ class Constitution(
): UuidEntity(id), ): UuidEntity(id),
EntityVersioning<UUID, Int> by UuidEntityVersioning(), EntityVersioning<UUID, Int> by UuidEntityVersioning(),
EntityCreatedAt by EntityCreatedAtImp(), EntityCreatedAt by EntityCreatedAtImp(),
CreatedBy<Citizen> by EntityCreatedByImp(createdBy) { EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy) {
init{ init{
titles.forEachIndexed { index, title -> titles.forEachIndexed { index, title ->
@@ -28,5 +28,5 @@ class Constitution(
createdBy: Citizen? = null createdBy: Citizen? = null
): UuidEntity(id), ): UuidEntity(id),
EntityCreatedAt by EntityCreatedAtImp(), EntityCreatedAt by EntityCreatedAtImp(),
CreatedBy<Citizen> by EntityCreatedByImp(createdBy) EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy)
} }

View File

@@ -1,22 +1,20 @@
package fr.dcproject.entity package fr.dcproject.entity
import fr.postgresjson.entity.EntityCreatedAt import fr.postgresjson.entity.*
import fr.postgresjson.entity.EntityCreatedAtImp
import fr.postgresjson.entity.EntityI
import fr.postgresjson.entity.UuidEntity
import java.util.* import java.util.*
interface ExtraI <T: EntityI<UUID>>: interface ExtraI <T: EntityI<UUID>>:
EntityI<UUID>, EntityI<UUID>,
EntityCreatedAt { EntityCreatedAt,
var citizen: Citizen EntityCreatedBy<Citizen>{
var target: T var target: T
} }
abstract class Extra<T: EntityI<UUID>>( abstract class Extra<T: EntityI<UUID>>(
id: UUID? = UUID.randomUUID(), id: UUID? = UUID.randomUUID(),
override var citizen: Citizen createdBy: Citizen
): ):
ExtraI<T>, ExtraI<T>,
UuidEntity(id), UuidEntity(id),
EntityCreatedAt by EntityCreatedAtImp() EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<Citizen> by EntityCreatedByImp(createdBy)

View File

@@ -4,6 +4,6 @@ import java.util.*
class Follow <T: UuidEntity> ( class Follow <T: UuidEntity> (
id: UUID = UUID.randomUUID(), id: UUID = UUID.randomUUID(),
citizen: Citizen, createdBy: Citizen,
override var target: T override var target: T
): Extra<T>(id, citizen) ): Extra<T>(id, createdBy)

View File

@@ -28,7 +28,7 @@ open class Follow <T: UuidEntity>(override var requester: Requester): Repository
return requester.run { return requester.run {
getFunction("find_follows_by_citizen") getFunction("find_follows_by_citizen")
.select(page, limit, .select(page, limit,
"citizen_id" to citizenId "created_by_id" to citizenId
) )
} }
} }
@@ -40,7 +40,7 @@ open class Follow <T: UuidEntity>(override var requester: Requester): Repository
.sendQuery( .sendQuery(
"reference" to reference, "reference" to reference,
"target_id" to follow.target.id, "target_id" to follow.target.id,
"citizen_id" to follow.citizen.id "created_by_id" to follow.createdBy?.id
) )
} }
@@ -51,7 +51,7 @@ open class Follow <T: UuidEntity>(override var requester: Requester): Repository
.sendQuery( .sendQuery(
"reference" to reference, "reference" to reference,
"target_id" to follow.target.id, "target_id" to follow.target.id,
"citizen_id" to follow.citizen.id "created_by_id" to follow.createdBy?.id
) )
} }
} }
@@ -65,7 +65,7 @@ class FollowArticle (requester: Requester): Follow<ArticleEntity>(requester) {
return requester.run { return requester.run {
getFunction("find_follows_article_by_citizen") getFunction("find_follows_article_by_citizen")
.select(page, limit, .select(page, limit,
"citizen_id" to citizenId "created_by_id" to citizenId
) )
} }
} }
@@ -80,7 +80,7 @@ class FollowConstitution (requester: Requester): Follow<ConstitutionEntity>(requ
return requester.run { return requester.run {
getFunction("find_follows_constitution_by_citizen") getFunction("find_follows_constitution_by_citizen")
.select(page, limit, .select(page, limit,
"citizen_id" to citizenId "created_by_id" to citizenId
) )
} }
} }

View File

@@ -30,12 +30,12 @@ object FollowArticlePaths {
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
fun Route.followArticle(repo: FollowArticleRepository) { fun Route.followArticle(repo: FollowArticleRepository) {
post<FollowArticlePaths.ArticleFollowRequest> { post<FollowArticlePaths.ArticleFollowRequest> {
repo.follow(FollowEntity(target = it.article, citizen = currentCitizen)) repo.follow(FollowEntity(target = it.article, createdBy = currentCitizen))
call.respond(HttpStatusCode.Created) call.respond(HttpStatusCode.Created)
} }
delete<FollowArticlePaths.ArticleFollowRequest> { delete<FollowArticlePaths.ArticleFollowRequest> {
repo.unfollow(FollowEntity(target = it.article, citizen = currentCitizen)) repo.unfollow(FollowEntity(target = it.article, createdBy = currentCitizen))
call.respond(HttpStatusCode.NoContent) call.respond(HttpStatusCode.NoContent)
} }

View File

@@ -30,12 +30,12 @@ object FollowConstitutionPaths {
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
fun Route.followConstitution(repo: FollowConstitutionRepository) { fun Route.followConstitution(repo: FollowConstitutionRepository) {
post<FollowConstitutionPaths.ConstitutionFollowRequest> { post<FollowConstitutionPaths.ConstitutionFollowRequest> {
repo.follow(FollowEntity(target = it.constitution, citizen = currentCitizen2)) repo.follow(FollowEntity(target = it.constitution, createdBy = currentCitizen2))
call.respond(HttpStatusCode.Created) call.respond(HttpStatusCode.Created)
} }
delete<FollowConstitutionPaths.ConstitutionFollowRequest> { delete<FollowConstitutionPaths.ConstitutionFollowRequest> {
repo.unfollow(FollowEntity(target = it.constitution, citizen = currentCitizen2)) repo.unfollow(FollowEntity(target = it.constitution, createdBy = currentCitizen2))
call.respond(HttpStatusCode.NoContent) call.respond(HttpStatusCode.NoContent)
} }

View File

@@ -5,7 +5,7 @@ declare
begin begin
delete from follow; delete from follow;
insert into follow_article (id, citizen_id, target_id) insert into follow_article (id, created_by_id, target_id)
select select
uuid_in(md5('follow_article'||row_number() over ())::cstring), uuid_in(md5('follow_article'||row_number() over ())::cstring),
z.id, z.id,
@@ -13,7 +13,7 @@ begin
from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5)) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5)) z
join (select *, row_number() over () rn from article) a using (rn); join (select *, row_number() over () rn from article) a using (rn);
insert into follow_constitution (id, citizen_id, target_id) insert into follow_constitution (id, created_by_id, target_id)
select select
uuid_in(md5('follow_constitution'||row_number() over ())::cstring), uuid_in(md5('follow_constitution'||row_number() over ())::cstring),
z.id, z.id,
@@ -21,7 +21,7 @@ begin
from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5)) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5)) z
join (select *, row_number() over () rn from constitution) a using (rn); join (select *, row_number() over () rn from constitution) a using (rn);
insert into follow_citizen (id, citizen_id, target_id) insert into follow_citizen (id, created_by_id, target_id)
select select
uuid_in(md5('follow_citizen'||row_number() over ())::cstring), uuid_in(md5('follow_citizen'||row_number() over ())::cstring),
z.id, z.id,

View File

@@ -5,7 +5,7 @@ declare
begin begin
delete from comment; delete from comment;
insert into comment_on_article (id, citizen_id, target_id, content) insert into comment_on_article (id, created_by_id, target_id, content)
select select
uuid_in(md5('comment_on_article'||row_number() over ())::cstring), uuid_in(md5('comment_on_article'||row_number() over ())::cstring),
z.id, z.id,
@@ -14,7 +14,7 @@ begin
from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from article) a using (rn); join (select *, row_number() over () rn from article) a using (rn);
insert into comment_on_article (id, citizen_id, target_id, content, parent_id) insert into comment_on_article (id, created_by_id, target_id, content, parent_id)
select select
uuid_in(md5('comment_on_article_2'||row_number() over ())::cstring), uuid_in(md5('comment_on_article_2'||row_number() over ())::cstring),
z.id, z.id,
@@ -24,7 +24,7 @@ begin
from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from comment_on_article) a using (rn); join (select *, row_number() over () rn from comment_on_article) a using (rn);
insert into comment_on_article (id, citizen_id, target_id, content, parent_id) insert into comment_on_article (id, created_by_id, target_id, content, parent_id)
select select
uuid_in(md5('comment_on_article_3'||row_number() over ())::cstring), uuid_in(md5('comment_on_article_3'||row_number() over ())::cstring),
z.id, z.id,
@@ -34,7 +34,7 @@ begin
from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from comment_on_article where parent_id is not null) a using (rn); join (select *, row_number() over () rn from comment_on_article where parent_id is not null) a using (rn);
insert into comment_on_constitution (id, citizen_id, target_id, content) insert into comment_on_constitution (id, created_by_id, target_id, content)
select select
uuid_in(md5('comment_on_constitution'||row_number() over ())::cstring), uuid_in(md5('comment_on_constitution'||row_number() over ())::cstring),
z.id, z.id,

View File

@@ -8,7 +8,7 @@ begin
delete from vote_for_comment_on_article; delete from vote_for_comment_on_article;
delete from vote_for_comment_on_constitution; delete from vote_for_comment_on_constitution;
raise notice '%', article_count; raise notice '%', article_count;
insert into vote_for_article (id, citizen_id, target_id, note, anonymous) insert into vote_for_article (id, created_by_id, target_id, note, anonymous)
select select
uuid_in(md5('vote_for_article'||row_number() over ())::cstring), uuid_in(md5('vote_for_article'||row_number() over ())::cstring),
z.id, z.id,
@@ -18,7 +18,7 @@ raise notice '%', article_count;
from (select *, row_number() over ()+g % (article_count+7) rn, g from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over ()+g % (article_count+7) rn, g from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from article) a using (rn); join (select *, row_number() over () rn from article) a using (rn);
insert into vote_for_constitution (id, citizen_id, target_id, note, anonymous) insert into vote_for_constitution (id, created_by_id, target_id, note, anonymous)
select select
uuid_in(md5('vote_for_constitution'||row_number() over ())::cstring), uuid_in(md5('vote_for_constitution'||row_number() over ())::cstring),
z.id, z.id,
@@ -28,7 +28,7 @@ raise notice '%', article_count;
from (select *, row_number() over () % (article_count+7) rn, g from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over () % (article_count+7) rn, g from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from constitution) a using (rn); join (select *, row_number() over () rn from constitution) a using (rn);
insert into vote_for_comment_on_article (id, citizen_id, target_id, note, anonymous) insert into vote_for_comment_on_article (id, created_by_id, target_id, note, anonymous)
select select
uuid_in(md5('vote_for_comment_on_article'||row_number() over ())::cstring), uuid_in(md5('vote_for_comment_on_article'||row_number() over ())::cstring),
z.id, z.id,
@@ -38,7 +38,7 @@ raise notice '%', article_count;
from (select *, row_number() over () % (article_count+7) rn, g from citizen, lateral generate_series(1, 3) g) z from (select *, row_number() over () % (article_count+7) rn, g from citizen, lateral generate_series(1, 3) g) z
join (select *, row_number() over () rn from comment_on_article) a using (rn); join (select *, row_number() over () rn from comment_on_article) a using (rn);
insert into vote_for_comment_on_constitution (id, citizen_id, target_id, note, anonymous) insert into vote_for_comment_on_constitution (id, created_by_id, target_id, note, anonymous)
select select
uuid_in(md5('vote_for_comment_on_constitution'||row_number() over ())::cstring), uuid_in(md5('vote_for_comment_on_constitution'||row_number() over ())::cstring),
z.id, z.id,

View File

@@ -1,20 +1,20 @@
create or replace function comment(reference regclass, target_id uuid, citizen_id uuid, content text, parent_id uuid default null, out id uuid) create or replace function comment(reference regclass, target_id uuid, created_by_id uuid, content text, parent_id uuid default null, out id uuid)
language plpgsql as language plpgsql as
$$ $$
declare declare
_citizen_id alias for citizen_id; _created_by_id alias for created_by_id;
_target_id alias for target_id; _target_id alias for target_id;
_content alias for content; _content alias for content;
_parent_id alias for parent_id; _parent_id alias for parent_id;
_id alias for id; _id alias for id;
begin begin
if reference = 'article'::regclass then if reference = 'article'::regclass then
insert into comment_on_article (citizen_id, target_id, content, parent_id) insert into comment_on_article (created_by_id, target_id, content, parent_id)
values (_citizen_id, _target_id, _content, _parent_id) values (_created_by_id, _target_id, _content, _parent_id)
returning comment_on_article.id into _id; returning comment_on_article.id into _id;
elseif reference = 'constitution'::regclass then elseif reference = 'constitution'::regclass then
insert into comment_on_constitution (citizen_id, target_id, content, parent_id) insert into comment_on_constitution (created_by_id, target_id, content, parent_id)
values (_citizen_id, _target_id, _content, _parent_id) values (_created_by_id, _target_id, _content, _parent_id)
returning comment_on_constitution.id into _id; returning comment_on_constitution.id into _id;
else else
raise exception 'comment with target as "%", is no implemented', reference::text; raise exception 'comment with target as "%", is no implemented', reference::text;

View File

@@ -0,0 +1,20 @@
create or replace function find_comment_by_id(
_id uuid,
out resource json
) language plpgsql as
$$
begin
select to_json(t)
into resource
from (
select
com.*,
json_build_object('id', com.target_id) as target,
find_citizen_by_id(com.created_by_id) as created_by
from "comment" as com
where id = _id
) as t;
end;
$$;
-- drop function if exists find_comment_by_id(uuid, out json);

View File

@@ -1,5 +1,5 @@
create or replace function find_comments_article_by_citizen( create or replace function find_comments_article_by_citizen(
_citizen_id uuid, _created_by_id uuid,
"limit" int default 50, "limit" int default 50,
"offset" int default 0, "offset" int default 0,
out resource json, out resource json,
@@ -13,9 +13,9 @@ begin
select select
com.*, com.*,
find_article_by_id(com.target_id) as target, find_article_by_id(com.target_id) as target,
find_citizen_by_id(com.citizen_id) as citizen find_citizen_by_id(com.created_by_id) as created_by
from comment as com from comment as com
where citizen_id = _citizen_id where created_by_id = _created_by_id
and target_reference = 'article'::regclass and target_reference = 'article'::regclass
order by created_at desc, order by created_at desc,
com.created_at desc com.created_at desc

View File

@@ -1,5 +1,5 @@
create or replace function find_comments_by_citizen( create or replace function find_comments_by_citizen(
_citizen_id uuid, _created_by_id uuid,
"limit" int default 50, "limit" int default 50,
"offset" int default 0, "offset" int default 0,
out resource json, out resource json,
@@ -7,15 +7,15 @@ create or replace function find_comments_by_citizen(
) language plpgsql as ) language plpgsql as
$$ $$
begin begin
select json_agg(t), (select count(id) from "comment" where citizen_id = _citizen_id) select json_agg(t), (select count(id) from "comment" where created_by_id = _created_by_id)
into resource, total into resource, total
from ( from (
select select
com.*, com.*,
json_build_object('id', com.target_id) as target, json_build_object('id', com.target_id) as target,
find_citizen_by_id(com.citizen_id) as citizen find_citizen_by_id(com.created_by_id) as created_by
from "comment" as com from "comment" as com
where citizen_id = _citizen_id where created_by_id = _created_by_id
order by created_at desc, order by created_at desc,
com.created_at desc com.created_at desc
limit "limit" offset "offset" limit "limit" offset "offset"

View File

@@ -13,7 +13,7 @@ begin
select select
com.*, com.*,
json_build_object('id', com.target_id) as target, json_build_object('id', com.target_id) as target,
find_citizen_by_id(com.citizen_id) as citizen find_citizen_by_id(com.created_by_id) as created_by
from "comment" as com from "comment" as com
where com.parents_ids @> array[_parent_id] where com.parents_ids @> array[_parent_id]
order by com.parents_ids nulls first, created_at desc, order by com.parents_ids nulls first, created_at desc,

View File

@@ -13,7 +13,7 @@ begin
select select
com.*, com.*,
json_build_object('id', com.target_id) as target, json_build_object('id', com.target_id) as target,
find_citizen_by_id(com.citizen_id) as citizen find_citizen_by_id(com.created_by_id) as created_by
from "comment" as com from "comment" as com
where com.target_id = _target_id where com.target_id = _target_id
order by com.parents_ids nulls first, created_at desc, order by com.parents_ids nulls first, created_at desc,

View File

@@ -1,5 +1,5 @@
create or replace function find_comments_constitution_by_citizen( create or replace function find_comments_constitution_by_citizen(
_citizen_id uuid, _created_by_id uuid,
"limit" int default 50, "limit" int default 50,
"offset" int default 0, "offset" int default 0,
out resource json, out resource json,
@@ -13,9 +13,9 @@ begin
select select
com.*, com.*,
find_constitution_by_id(com.target_id) as target, find_constitution_by_id(com.target_id) as target,
find_citizen_by_id(com.citizen_id) as citizen find_citizen_by_id(com.created_by_id) as created_by
from comment as com from comment as com
where citizen_id = _citizen_id where created_by_id = _created_by_id
and target_reference = 'constitution'::regclass and target_reference = 'constitution'::regclass
order by created_at desc, order by created_at desc,
com.created_at desc com.created_at desc

View File

@@ -1,5 +1,5 @@
create or replace function find_follows_article_by_citizen( create or replace function find_follows_article_by_citizen(
_citizen_id uuid, _created_by_id uuid,
"limit" int default 50, "limit" int default 50,
"offset" int default 0, "offset" int default 0,
out resource json, out resource json,
@@ -13,9 +13,9 @@ begin
select select
f.*, f.*,
find_article_by_id(f.target_id) as target, find_article_by_id(f.target_id) as target,
find_citizen_by_id(f.citizen_id) as citizen find_citizen_by_id(f.created_by_id) as created_by
from follow as f from follow as f
where citizen_id = _citizen_id where created_by_id = _created_by_id
order by created_at desc, order by created_at desc,
f.created_at desc f.created_at desc
limit "limit" offset "offset" limit "limit" offset "offset"

View File

@@ -1,5 +1,5 @@
create or replace function find_follows_by_citizen( create or replace function find_follows_by_citizen(
_citizen_id uuid, _created_by_id uuid,
"limit" int default 50, "limit" int default 50,
"offset" int default 0, "offset" int default 0,
out resource json, out resource json,
@@ -13,9 +13,9 @@ begin
select select
f.*, f.*,
json_build_object('id', f.target_id) as target, json_build_object('id', f.target_id) as target,
find_citizen_by_id(f.citizen_id) as citizen find_citizen_by_id(f.created_by_id) as created_by
from follow as f from follow as f
where citizen_id = _citizen_id where created_by_id = _created_by_id
order by created_at desc, order by created_at desc,
f.created_at desc f.created_at desc
limit "limit" offset "offset" limit "limit" offset "offset"

View File

@@ -1,5 +1,5 @@
create or replace function find_follows_constitution_by_citizen( create or replace function find_follows_constitution_by_citizen(
_citizen_id uuid, _created_by_id uuid,
"limit" int default 50, "limit" int default 50,
"offset" int default 0, "offset" int default 0,
out resource json, out resource json,
@@ -13,9 +13,9 @@ begin
select select
f.*, f.*,
find_constitution_by_id(f.target_id) as target, find_constitution_by_id(f.target_id) as target,
find_citizen_by_id(f.citizen_id) as citizen find_citizen_by_id(f.created_by_id) as created_by
from follow as f from follow as f
where citizen_id = _citizen_id where created_by_id = _created_by_id
order by created_at desc, order by created_at desc,
f.created_at desc f.created_at desc
limit "limit" offset "offset" limit "limit" offset "offset"

View File

@@ -1,19 +1,19 @@
create or replace function follow(reference regclass, _target_id uuid, _citizen_id uuid) returns void create or replace function follow(reference regclass, _target_id uuid, _created_by_id uuid) returns void
language plpgsql as language plpgsql as
$$ $$
begin begin
if reference = 'article'::regclass then if reference = 'article'::regclass then
insert into follow_article (citizen_id, target_id) insert into follow_article (created_by_id, target_id)
values (_citizen_id, _target_id) values (_created_by_id, _target_id)
on conflict (citizen_id, target_id) do nothing; on conflict (created_by_id, target_id) do nothing;
elseif reference = 'constitution'::regclass then elseif reference = 'constitution'::regclass then
insert into follow_constitution (citizen_id, target_id) insert into follow_constitution (created_by_id, target_id)
values (_citizen_id, _target_id) values (_created_by_id, _target_id)
on conflict (citizen_id, target_id) do nothing; on conflict (created_by_id, target_id) do nothing;
elseif reference = 'citizen'::regclass then elseif reference = 'citizen'::regclass then
insert into follow_citizen (citizen_id, target_id) insert into follow_citizen (created_by_id, target_id)
values (_citizen_id, _target_id) values (_created_by_id, _target_id)
on conflict (citizen_id, target_id) do nothing; on conflict (created_by_id, target_id) do nothing;
else else
raise exception '% no implemented', reference::text; raise exception '% no implemented', reference::text;
end if; end if;

View File

@@ -1,10 +1,10 @@
create or replace function unfollow(reference regclass, _target_id uuid, _citizen_id uuid) returns void create or replace function unfollow(reference regclass, _target_id uuid, _created_by_id uuid) returns void
language plpgsql as language plpgsql as
$$ $$
begin begin
delete delete
from follow f from follow f
where f.citizen_id = _citizen_id where f.created_by_id = _created_by_id
and f.target_id = _target_id and f.target_id = _target_id
and f.target_reference = reference; and f.target_reference = reference;
end; end;

View File

@@ -1,29 +1,29 @@
create or replace function vote(reference regclass, _target_id uuid, _citizen_id uuid, _note int, _anonymous bool default true) returns void create or replace function vote(reference regclass, _target_id uuid, _created_by_id uuid, _note int, _anonymous bool default true) returns void
language plpgsql as language plpgsql as
$$ $$
begin begin
if reference = 'article'::regclass then if reference = 'article'::regclass then
insert into vote_for_article (citizen_id, target_id, note, anonymous) insert into vote_for_article (created_by_id, target_id, note, anonymous)
values (_citizen_id, _target_id, _note, _anonymous) values (_created_by_id, _target_id, _note, _anonymous)
on conflict (citizen_id, target_id) do update set on conflict (created_by_id, target_id) do update set
note = excluded.note, note = excluded.note,
anonymous = excluded.anonymous; anonymous = excluded.anonymous;
elseif reference = 'constitution'::regclass then elseif reference = 'constitution'::regclass then
insert into vote_for_constitution (citizen_id, target_id, note, anonymous) insert into vote_for_constitution (created_by_id, target_id, note, anonymous)
values (_citizen_id, _target_id, _note, _anonymous) values (_created_by_id, _target_id, _note, _anonymous)
on conflict (citizen_id, target_id) do update set on conflict (created_by_id, target_id) do update set
note = excluded.note, note = excluded.note,
anonymous = excluded.anonymous; anonymous = excluded.anonymous;
elseif reference = 'comment_on_article'::regclass then elseif reference = 'comment_on_article'::regclass then
insert into vote_for_comment_on_article (citizen_id, target_id, note, anonymous) insert into vote_for_comment_on_article (created_by_id, target_id, note, anonymous)
values (_citizen_id, _target_id, _note, _anonymous) values (_created_by_id, _target_id, _note, _anonymous)
on conflict (citizen_id, target_id) do update set on conflict (created_by_id, target_id) do update set
note = excluded.note, note = excluded.note,
anonymous = excluded.anonymous; anonymous = excluded.anonymous;
elseif reference = 'comment_on_constitution'::regclass then elseif reference = 'comment_on_constitution'::regclass then
insert into vote_for_comment_on_constitution (citizen_id, target_id, note, anonymous) insert into vote_for_comment_on_constitution (created_by_id, target_id, note, anonymous)
values (_citizen_id, _target_id, _note, _anonymous) values (_created_by_id, _target_id, _note, _anonymous)
on conflict (citizen_id, target_id) do update set on conflict (created_by_id, target_id) do update set
note = excluded.note, note = excluded.note,
anonymous = excluded.anonymous; anonymous = excluded.anonymous;
else else

View File

@@ -186,43 +186,43 @@ create table extra
( (
id uuid default uuid_generate_v4() not null primary key, id uuid default uuid_generate_v4() not null primary key,
created_at timestamptz default now() not null, created_at timestamptz default now() not null,
citizen_id uuid not null references citizen (id), created_by_id uuid not null references citizen (id),
target_id uuid not null, target_id uuid not null,
target_reference regclass not null target_reference regclass not null
); );
create table follow create table follow
( (
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
primary key (id), primary key (id),
unique (citizen_id, target_id) unique (created_by_id, target_id)
) inherits (extra); ) inherits (extra);
create table follow_article create table follow_article
( (
target_reference regclass default 'article'::regclass not null, target_reference regclass default 'article'::regclass not null,
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
foreign key (target_id) references article (id), foreign key (target_id) references article (id),
primary key (id), primary key (id),
unique (citizen_id, target_id) unique (created_by_id, target_id)
) inherits (follow); ) inherits (follow);
create table follow_constitution create table follow_constitution
( (
target_reference regclass default 'constitution'::regclass not null, target_reference regclass default 'constitution'::regclass not null,
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
foreign key (target_id) references constitution (id), foreign key (target_id) references constitution (id),
primary key (id), primary key (id),
unique (citizen_id, target_id) unique (created_by_id, target_id)
) inherits (follow); ) inherits (follow);
create table follow_citizen create table follow_citizen
( (
target_reference regclass default 'citizen'::regclass not null, target_reference regclass default 'citizen'::regclass not null,
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
foreign key (target_id) references citizen (id), foreign key (target_id) references citizen (id),
primary key (id), primary key (id),
unique (citizen_id, target_id) unique (created_by_id, target_id)
) inherits (follow); ) inherits (follow);
@@ -231,9 +231,9 @@ create table comment
( (
updated_at timestamptz default now() not null check ( updated_at >= created_at ), updated_at timestamptz default now() not null check ( updated_at >= created_at ),
"content" text not null check ( content != '' and length(content) < 4096), "content" text not null check ( content != '' and length(content) < 4096),
parent_id uuid references comment (id), parent_id uuid references comment (id),
parents_ids uuid[], parents_ids uuid[],
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
primary key (id) primary key (id)
) inherits (extra); ) inherits (extra);
@@ -244,7 +244,7 @@ create or replace function set_comment_parents_ids() returns trigger
language plpgsql as language plpgsql as
$$ $$
begin begin
if(new.parent_id is not null) then if (new.parent_id is not null) then
new.parents_ids = ( new.parents_ids = (
select com.parents_ids || com.id select com.parents_ids || com.id
from "comment" com from "comment" com
@@ -267,7 +267,7 @@ execute procedure set_comment_parents_ids();
create table comment_on_article create table comment_on_article
( (
target_reference regclass default 'article'::regclass not null, target_reference regclass default 'article'::regclass not null,
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
foreign key (target_id) references article (id), foreign key (target_id) references article (id),
foreign key (parent_id) references comment_on_article (id), foreign key (parent_id) references comment_on_article (id),
primary key (id) primary key (id)
@@ -285,7 +285,7 @@ execute procedure set_comment_parents_ids();
create table comment_on_constitution create table comment_on_constitution
( (
target_reference regclass default 'constitution'::regclass not null, target_reference regclass default 'constitution'::regclass not null,
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
foreign key (target_id) references constitution (id), foreign key (target_id) references constitution (id),
foreign key (parent_id) references comment_on_constitution (id), foreign key (parent_id) references comment_on_constitution (id),
primary key (id) primary key (id)
@@ -306,45 +306,45 @@ create table vote
( (
anonymous boolean default true not null, anonymous boolean default true not null,
note int not null check ( note >= -1 and note <= 1 ), note int not null check ( note >= -1 and note <= 1 ),
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
primary key (id), primary key (id),
unique (citizen_id, target_id) unique (created_by_id, target_id)
) inherits (extra); ) inherits (extra);
create table vote_for_article create table vote_for_article
( (
target_reference regclass default 'article'::regclass not null, target_reference regclass default 'article'::regclass not null,
foreign key (target_id) references article (id), foreign key (target_id) references article (id),
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
primary key (id), primary key (id),
unique (citizen_id, target_id) unique (created_by_id, target_id)
) inherits (vote); ) inherits (vote);
create table vote_for_constitution create table vote_for_constitution
( (
target_reference regclass default 'constitution'::regclass not null, target_reference regclass default 'constitution'::regclass not null,
foreign key (target_id) references constitution (id), foreign key (target_id) references constitution (id),
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
primary key (id), primary key (id),
unique (citizen_id, target_id) unique (created_by_id, target_id)
) inherits (vote); ) inherits (vote);
create table vote_for_comment_on_article create table vote_for_comment_on_article
( (
target_reference regclass default 'comment_on_article'::regclass not null, target_reference regclass default 'comment_on_article'::regclass not null,
foreign key (target_id) references comment_on_article (id), foreign key (target_id) references comment_on_article (id),
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
primary key (id), primary key (id),
unique (citizen_id, target_id) unique (created_by_id, target_id)
) inherits (vote); ) inherits (vote);
create table vote_for_comment_on_constitution create table vote_for_comment_on_constitution
( (
target_reference regclass default 'comment_on_constitution'::regclass not null, target_reference regclass default 'comment_on_constitution'::regclass not null,
foreign key (target_id) references comment_on_constitution (id), foreign key (target_id) references comment_on_constitution (id),
foreign key (citizen_id) references citizen (id), foreign key (created_by_id) references citizen (id),
primary key (id), primary key (id),
unique (citizen_id, target_id) unique (created_by_id, target_id)
) inherits (vote); ) inherits (vote);
-- Stats -- Stats

View File

@@ -21,7 +21,7 @@ class FollowTest {
@Language("JSON") @Language("JSON")
private val followJson: String = """{ private val followJson: String = """{
"id":"bae81585-d985-4d7a-9b58-3a13e911688a", "id":"bae81585-d985-4d7a-9b58-3a13e911688a",
"citizen":{ "created_by":{
"id":"4a87ad24-187a-46a8-97ab-00b30a24e561", "id":"4a87ad24-187a-46a8-97ab-00b30a24e561",
"name":{ "name":{
"first_name":"Jaque", "first_name":"Jaque",
@@ -76,7 +76,7 @@ class FollowTest {
createdBy = citizen createdBy = citizen
) )
val follow = Follow( val follow = Follow(
citizen = citizen, createdBy = citizen,
target = article target = article
) )
follow.serialize().contains("""Hello world!""") shouldBe true follow.serialize().contains("""Hello world!""") shouldBe true

View File

@@ -36,7 +36,7 @@ Feature: Auth routes
When I send a POST request to "/login" with body: When I send a POST request to "/login" with body:
""" """
{ {
"name": "username1", "name": "username-1",
"password": "azerty" "password": "azerty"
} }
""" """

View File

@@ -50,7 +50,7 @@ begin
select "comment"( select "comment"(
reference => 'article'::regclass, reference => 'article'::regclass,
target_id => (created_article->>'id')::uuid, target_id => (created_article->>'id')::uuid,
citizen_id => _citizen_id, created_by_id => _citizen_id,
content => 'Ho my god !'::text content => 'Ho my god !'::text
) into _comment_id; ) into _comment_id;
assert (select count(*) = 1 from "comment"), 'comment must be inserted'; assert (select count(*) = 1 from "comment"), 'comment must be inserted';
@@ -86,7 +86,7 @@ begin
select "comment"( select "comment"(
reference => 'article'::regclass, reference => 'article'::regclass,
target_id => (created_article->>'id')::uuid, target_id => (created_article->>'id')::uuid,
citizen_id => _citizen_id, created_by_id => _citizen_id,
content => 'God not exist'::text, content => 'God not exist'::text,
parent_id => _comment_id::uuid parent_id => _comment_id::uuid
) into _comment_id_response; ) into _comment_id_response;
@@ -94,7 +94,7 @@ begin
select "comment"( select "comment"(
reference => 'article'::regclass, reference => 'article'::regclass,
target_id => (created_article->>'id')::uuid, target_id => (created_article->>'id')::uuid,
citizen_id => _citizen_id, created_by_id => _citizen_id,
content => 'are you really sure ?'::text, content => 'are you really sure ?'::text,
parent_id => _comment_id_response::uuid parent_id => _comment_id_response::uuid
) into _comment_id_response2; ) into _comment_id_response2;

View File

@@ -41,7 +41,7 @@ begin
perform vote( perform vote(
reference => 'article'::regclass, reference => 'article'::regclass,
_target_id => (created_article->>'id')::uuid, _target_id => (created_article->>'id')::uuid,
_citizen_id => _citizen_id, _created_by_id => _citizen_id,
_note => 1 _note => 1
); );
assert (select count(*) = 1 from vote_for_article), 'vote must be inserted'; assert (select count(*) = 1 from vote_for_article), 'vote must be inserted';
@@ -50,7 +50,7 @@ begin
perform vote( perform vote(
reference => 'article'::regclass, reference => 'article'::regclass,
_target_id => (created_article->>'id')::uuid, _target_id => (created_article->>'id')::uuid,
_citizen_id => _citizen_id, _created_by_id => _citizen_id,
_note => -1 _note => -1
); );
assert (select count(*) = 1 from vote_for_article), 'vote must be inserted'; assert (select count(*) = 1 from vote_for_article), 'vote must be inserted';
@@ -60,7 +60,7 @@ begin
perform vote( perform vote(
reference => 'article'::regclass, reference => 'article'::regclass,
_target_id => (created_article->>'id')::uuid, _target_id => (created_article->>'id')::uuid,
_citizen_id => _citizen_id, _created_by_id => _citizen_id,
_note => -10 _note => -10
); );
assert false, 'vote must be throw exception if note is not -1, 0 or 1'; assert false, 'vote must be throw exception if note is not -1, 0 or 1';