diff --git a/src/main/kotlin/fr/dcproject/Application.kt b/src/main/kotlin/fr/dcproject/Application.kt index 9c9e88f..7411d89 100644 --- a/src/main/kotlin/fr/dcproject/Application.kt +++ b/src/main/kotlin/fr/dcproject/Application.kt @@ -15,6 +15,7 @@ import fr.dcproject.routes.* import fr.dcproject.security.voter.ArticleVoter import fr.dcproject.security.voter.AuthorizationVoter import fr.dcproject.security.voter.CitizenVoter +import fr.dcproject.security.voter.CommentVoter import fr.postgresjson.migration.Migrations import io.ktor.application.Application import io.ktor.application.call @@ -104,7 +105,8 @@ fun Application.module() { install(AuthorizationVoter) { voters = mutableListOf( ArticleVoter(), - CitizenVoter() + CitizenVoter(), + CommentVoter() ) } diff --git a/src/main/kotlin/fr/dcproject/entity/Article.kt b/src/main/kotlin/fr/dcproject/entity/Article.kt index 2036298..e3f7ee9 100644 --- a/src/main/kotlin/fr/dcproject/entity/Article.kt +++ b/src/main/kotlin/fr/dcproject/entity/Article.kt @@ -15,4 +15,4 @@ class Article( UuidEntity(id), EntityVersioning by UuidEntityVersioning(), EntityCreatedAt by EntityCreatedAtImp(), - CreatedBy by EntityCreatedByImp(createdBy) \ No newline at end of file + EntityCreatedBy by EntityCreatedByImp(createdBy) \ No newline at end of file diff --git a/src/main/kotlin/fr/dcproject/entity/Constitution.kt b/src/main/kotlin/fr/dcproject/entity/Constitution.kt index 91bf89c..a7371b1 100644 --- a/src/main/kotlin/fr/dcproject/entity/Constitution.kt +++ b/src/main/kotlin/fr/dcproject/entity/Constitution.kt @@ -12,7 +12,7 @@ class Constitution( ): UuidEntity(id), EntityVersioning by UuidEntityVersioning(), EntityCreatedAt by EntityCreatedAtImp(), - CreatedBy by EntityCreatedByImp(createdBy) { + EntityCreatedBy by EntityCreatedByImp(createdBy) { init{ titles.forEachIndexed { index, title -> @@ -28,5 +28,5 @@ class Constitution( createdBy: Citizen? = null ): UuidEntity(id), EntityCreatedAt by EntityCreatedAtImp(), - CreatedBy by EntityCreatedByImp(createdBy) + EntityCreatedBy by EntityCreatedByImp(createdBy) } diff --git a/src/main/kotlin/fr/dcproject/entity/Extra.kt b/src/main/kotlin/fr/dcproject/entity/Extra.kt index 3b83477..9d5bd25 100644 --- a/src/main/kotlin/fr/dcproject/entity/Extra.kt +++ b/src/main/kotlin/fr/dcproject/entity/Extra.kt @@ -1,22 +1,20 @@ package fr.dcproject.entity -import fr.postgresjson.entity.EntityCreatedAt -import fr.postgresjson.entity.EntityCreatedAtImp -import fr.postgresjson.entity.EntityI -import fr.postgresjson.entity.UuidEntity +import fr.postgresjson.entity.* import java.util.* interface ExtraI >: EntityI, - EntityCreatedAt { - var citizen: Citizen + EntityCreatedAt, + EntityCreatedBy{ var target: T } abstract class Extra>( id: UUID? = UUID.randomUUID(), - override var citizen: Citizen + createdBy: Citizen ): ExtraI, UuidEntity(id), - EntityCreatedAt by EntityCreatedAtImp() \ No newline at end of file + EntityCreatedAt by EntityCreatedAtImp(), + EntityCreatedBy by EntityCreatedByImp(createdBy) \ No newline at end of file diff --git a/src/main/kotlin/fr/dcproject/entity/Follow.kt b/src/main/kotlin/fr/dcproject/entity/Follow.kt index 0f674a4..69ed3f2 100644 --- a/src/main/kotlin/fr/dcproject/entity/Follow.kt +++ b/src/main/kotlin/fr/dcproject/entity/Follow.kt @@ -4,6 +4,6 @@ import java.util.* class Follow ( id: UUID = UUID.randomUUID(), - citizen: Citizen, + createdBy: Citizen, override var target: T -): Extra(id, citizen) +): Extra(id, createdBy) diff --git a/src/main/kotlin/fr/dcproject/repository/Follow.kt b/src/main/kotlin/fr/dcproject/repository/Follow.kt index ab42fe0..058eb6d 100644 --- a/src/main/kotlin/fr/dcproject/repository/Follow.kt +++ b/src/main/kotlin/fr/dcproject/repository/Follow.kt @@ -28,7 +28,7 @@ open class Follow (override var requester: Requester): Repository return requester.run { getFunction("find_follows_by_citizen") .select(page, limit, - "citizen_id" to citizenId + "created_by_id" to citizenId ) } } @@ -40,7 +40,7 @@ open class Follow (override var requester: Requester): Repository .sendQuery( "reference" to reference, "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 (override var requester: Requester): Repository .sendQuery( "reference" to reference, "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(requester) { return requester.run { getFunction("find_follows_article_by_citizen") .select(page, limit, - "citizen_id" to citizenId + "created_by_id" to citizenId ) } } @@ -80,7 +80,7 @@ class FollowConstitution (requester: Requester): Follow(requ return requester.run { getFunction("find_follows_constitution_by_citizen") .select(page, limit, - "citizen_id" to citizenId + "created_by_id" to citizenId ) } } diff --git a/src/main/kotlin/fr/dcproject/routes/FollowArticle.kt b/src/main/kotlin/fr/dcproject/routes/FollowArticle.kt index 95b567e..cbc4be8 100644 --- a/src/main/kotlin/fr/dcproject/routes/FollowArticle.kt +++ b/src/main/kotlin/fr/dcproject/routes/FollowArticle.kt @@ -30,12 +30,12 @@ object FollowArticlePaths { @KtorExperimentalLocationsAPI fun Route.followArticle(repo: FollowArticleRepository) { post { - repo.follow(FollowEntity(target = it.article, citizen = currentCitizen)) + repo.follow(FollowEntity(target = it.article, createdBy = currentCitizen)) call.respond(HttpStatusCode.Created) } delete { - repo.unfollow(FollowEntity(target = it.article, citizen = currentCitizen)) + repo.unfollow(FollowEntity(target = it.article, createdBy = currentCitizen)) call.respond(HttpStatusCode.NoContent) } diff --git a/src/main/kotlin/fr/dcproject/routes/FollowConstitution.kt b/src/main/kotlin/fr/dcproject/routes/FollowConstitution.kt index ddf76f2..07f0330 100644 --- a/src/main/kotlin/fr/dcproject/routes/FollowConstitution.kt +++ b/src/main/kotlin/fr/dcproject/routes/FollowConstitution.kt @@ -30,12 +30,12 @@ object FollowConstitutionPaths { @KtorExperimentalLocationsAPI fun Route.followConstitution(repo: FollowConstitutionRepository) { post { - repo.follow(FollowEntity(target = it.constitution, citizen = currentCitizen2)) + repo.follow(FollowEntity(target = it.constitution, createdBy = currentCitizen2)) call.respond(HttpStatusCode.Created) } delete { - repo.unfollow(FollowEntity(target = it.constitution, citizen = currentCitizen2)) + repo.unfollow(FollowEntity(target = it.constitution, createdBy = currentCitizen2)) call.respond(HttpStatusCode.NoContent) } diff --git a/src/main/resources/sql/fixtures/06-follow.sql b/src/main/resources/sql/fixtures/06-follow.sql index 095ec88..3043cc4 100644 --- a/src/main/resources/sql/fixtures/06-follow.sql +++ b/src/main/resources/sql/fixtures/06-follow.sql @@ -5,7 +5,7 @@ declare begin delete from follow; - insert into follow_article (id, citizen_id, target_id) + insert into follow_article (id, created_by_id, target_id) select uuid_in(md5('follow_article'||row_number() over ())::cstring), z.id, @@ -13,7 +13,7 @@ begin 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); - insert into follow_constitution (id, citizen_id, target_id) + insert into follow_constitution (id, created_by_id, target_id) select uuid_in(md5('follow_constitution'||row_number() over ())::cstring), z.id, @@ -21,7 +21,7 @@ begin 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); - insert into follow_citizen (id, citizen_id, target_id) + insert into follow_citizen (id, created_by_id, target_id) select uuid_in(md5('follow_citizen'||row_number() over ())::cstring), z.id, diff --git a/src/main/resources/sql/fixtures/07-comment.sql b/src/main/resources/sql/fixtures/07-comment.sql index eabe546..5b5f79c 100644 --- a/src/main/resources/sql/fixtures/07-comment.sql +++ b/src/main/resources/sql/fixtures/07-comment.sql @@ -5,7 +5,7 @@ declare begin 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 uuid_in(md5('comment_on_article'||row_number() over ())::cstring), 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 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 uuid_in(md5('comment_on_article_2'||row_number() over ())::cstring), 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 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 uuid_in(md5('comment_on_article_3'||row_number() over ())::cstring), 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 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 uuid_in(md5('comment_on_constitution'||row_number() over ())::cstring), z.id, diff --git a/src/main/resources/sql/fixtures/08-vote.sql b/src/main/resources/sql/fixtures/08-vote.sql index bdd512e..ba1252d 100644 --- a/src/main/resources/sql/fixtures/08-vote.sql +++ b/src/main/resources/sql/fixtures/08-vote.sql @@ -8,7 +8,7 @@ begin delete from vote_for_comment_on_article; delete from vote_for_comment_on_constitution; 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 uuid_in(md5('vote_for_article'||row_number() over ())::cstring), 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 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 uuid_in(md5('vote_for_constitution'||row_number() over ())::cstring), 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 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 uuid_in(md5('vote_for_comment_on_article'||row_number() over ())::cstring), 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 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 uuid_in(md5('vote_for_comment_on_constitution'||row_number() over ())::cstring), z.id, diff --git a/src/main/resources/sql/functions/comment/comment.sql b/src/main/resources/sql/functions/comment/comment.sql index a5b45b6..8796484 100644 --- a/src/main/resources/sql/functions/comment/comment.sql +++ b/src/main/resources/sql/functions/comment/comment.sql @@ -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 $$ declare - _citizen_id alias for citizen_id; + _created_by_id alias for created_by_id; _target_id alias for target_id; _content alias for content; _parent_id alias for parent_id; _id alias for id; begin if reference = 'article'::regclass then - insert into comment_on_article (citizen_id, target_id, content, parent_id) - values (_citizen_id, _target_id, _content, _parent_id) + insert into comment_on_article (created_by_id, target_id, content, parent_id) + values (_created_by_id, _target_id, _content, _parent_id) returning comment_on_article.id into _id; elseif reference = 'constitution'::regclass then - insert into comment_on_constitution (citizen_id, target_id, content, parent_id) - values (_citizen_id, _target_id, _content, _parent_id) + insert into comment_on_constitution (created_by_id, target_id, content, parent_id) + values (_created_by_id, _target_id, _content, _parent_id) returning comment_on_constitution.id into _id; else raise exception 'comment with target as "%", is no implemented', reference::text; 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 new file mode 100644 index 0000000..7bc481b --- /dev/null +++ b/src/main/resources/sql/functions/comment/find_comment_by_id.sql @@ -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); diff --git a/src/main/resources/sql/functions/comment/find_comments_article_by_citizen.sql b/src/main/resources/sql/functions/comment/find_comments_article_by_citizen.sql index 37a564c..4b81f80 100644 --- a/src/main/resources/sql/functions/comment/find_comments_article_by_citizen.sql +++ b/src/main/resources/sql/functions/comment/find_comments_article_by_citizen.sql @@ -1,5 +1,5 @@ create or replace function find_comments_article_by_citizen( - _citizen_id uuid, + _created_by_id uuid, "limit" int default 50, "offset" int default 0, out resource json, @@ -13,9 +13,9 @@ begin select com.*, 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 - where citizen_id = _citizen_id + where created_by_id = _created_by_id and target_reference = 'article'::regclass order by created_at desc, com.created_at desc 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 fb55048..2c3ae04 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 @@ -1,5 +1,5 @@ create or replace function find_comments_by_citizen( - _citizen_id uuid, + _created_by_id uuid, "limit" int default 50, "offset" int default 0, out resource json, @@ -7,15 +7,15 @@ create or replace function find_comments_by_citizen( ) language plpgsql as $$ 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 from ( select com.*, 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 - where citizen_id = _citizen_id + where created_by_id = _created_by_id order by created_at desc, com.created_at desc limit "limit" offset "offset" 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 b419995..d2eac4f 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 @@ -13,7 +13,7 @@ begin select com.*, 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 where com.parents_ids @> array[_parent_id] order by com.parents_ids nulls first, created_at desc, 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 a49c341..453b50e 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 @@ -13,7 +13,7 @@ begin select com.*, 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 where com.target_id = _target_id order by com.parents_ids nulls first, created_at desc, diff --git a/src/main/resources/sql/functions/comment/find_comments_constitution_by_citizen.sql b/src/main/resources/sql/functions/comment/find_comments_constitution_by_citizen.sql index d9829fc..f883212 100644 --- a/src/main/resources/sql/functions/comment/find_comments_constitution_by_citizen.sql +++ b/src/main/resources/sql/functions/comment/find_comments_constitution_by_citizen.sql @@ -1,5 +1,5 @@ create or replace function find_comments_constitution_by_citizen( - _citizen_id uuid, + _created_by_id uuid, "limit" int default 50, "offset" int default 0, out resource json, @@ -13,9 +13,9 @@ begin select com.*, 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 - where citizen_id = _citizen_id + where created_by_id = _created_by_id and target_reference = 'constitution'::regclass order by created_at desc, com.created_at desc diff --git a/src/main/resources/sql/functions/follow/find_follows_article_by_citizen.sql b/src/main/resources/sql/functions/follow/find_follows_article_by_citizen.sql index 36bab21..1d7f3d5 100644 --- a/src/main/resources/sql/functions/follow/find_follows_article_by_citizen.sql +++ b/src/main/resources/sql/functions/follow/find_follows_article_by_citizen.sql @@ -1,5 +1,5 @@ create or replace function find_follows_article_by_citizen( - _citizen_id uuid, + _created_by_id uuid, "limit" int default 50, "offset" int default 0, out resource json, @@ -13,9 +13,9 @@ begin select f.*, 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 - where citizen_id = _citizen_id + where created_by_id = _created_by_id order by created_at desc, f.created_at desc limit "limit" offset "offset" diff --git a/src/main/resources/sql/functions/follow/find_follows_by_citizen.sql b/src/main/resources/sql/functions/follow/find_follows_by_citizen.sql index f9d1896..5022b4a 100644 --- a/src/main/resources/sql/functions/follow/find_follows_by_citizen.sql +++ b/src/main/resources/sql/functions/follow/find_follows_by_citizen.sql @@ -1,5 +1,5 @@ create or replace function find_follows_by_citizen( - _citizen_id uuid, + _created_by_id uuid, "limit" int default 50, "offset" int default 0, out resource json, @@ -13,9 +13,9 @@ begin select f.*, 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 - where citizen_id = _citizen_id + where created_by_id = _created_by_id order by created_at desc, f.created_at desc limit "limit" offset "offset" diff --git a/src/main/resources/sql/functions/follow/find_follows_constitution_by_citizen.sql b/src/main/resources/sql/functions/follow/find_follows_constitution_by_citizen.sql index 75c507f..e4d86a8 100644 --- a/src/main/resources/sql/functions/follow/find_follows_constitution_by_citizen.sql +++ b/src/main/resources/sql/functions/follow/find_follows_constitution_by_citizen.sql @@ -1,5 +1,5 @@ create or replace function find_follows_constitution_by_citizen( - _citizen_id uuid, + _created_by_id uuid, "limit" int default 50, "offset" int default 0, out resource json, @@ -13,9 +13,9 @@ begin select f.*, 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 - where citizen_id = _citizen_id + where created_by_id = _created_by_id order by created_at desc, f.created_at desc limit "limit" offset "offset" diff --git a/src/main/resources/sql/functions/follow/follow.sql b/src/main/resources/sql/functions/follow/follow.sql index a025d2d..e415103 100644 --- a/src/main/resources/sql/functions/follow/follow.sql +++ b/src/main/resources/sql/functions/follow/follow.sql @@ -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 $$ begin if reference = 'article'::regclass then - insert into follow_article (citizen_id, target_id) - values (_citizen_id, _target_id) - on conflict (citizen_id, target_id) do nothing; + insert into follow_article (created_by_id, target_id) + values (_created_by_id, _target_id) + on conflict (created_by_id, target_id) do nothing; elseif reference = 'constitution'::regclass then - insert into follow_constitution (citizen_id, target_id) - values (_citizen_id, _target_id) - on conflict (citizen_id, target_id) do nothing; + insert into follow_constitution (created_by_id, target_id) + values (_created_by_id, _target_id) + on conflict (created_by_id, target_id) do nothing; elseif reference = 'citizen'::regclass then - insert into follow_citizen (citizen_id, target_id) - values (_citizen_id, _target_id) - on conflict (citizen_id, target_id) do nothing; + insert into follow_citizen (created_by_id, target_id) + values (_created_by_id, _target_id) + on conflict (created_by_id, target_id) do nothing; else raise exception '% no implemented', reference::text; end if; diff --git a/src/main/resources/sql/functions/follow/unfollow.sql b/src/main/resources/sql/functions/follow/unfollow.sql index b6d8315..63fc934 100644 --- a/src/main/resources/sql/functions/follow/unfollow.sql +++ b/src/main/resources/sql/functions/follow/unfollow.sql @@ -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 $$ begin delete 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_reference = reference; end; diff --git a/src/main/resources/sql/functions/vote/vote.sql b/src/main/resources/sql/functions/vote/vote.sql index 3a11f9e..2f9a22f 100644 --- a/src/main/resources/sql/functions/vote/vote.sql +++ b/src/main/resources/sql/functions/vote/vote.sql @@ -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 $$ begin if reference = 'article'::regclass then - insert into vote_for_article (citizen_id, target_id, note, anonymous) - values (_citizen_id, _target_id, _note, _anonymous) - on conflict (citizen_id, target_id) do update set + insert into vote_for_article (created_by_id, target_id, note, anonymous) + values (_created_by_id, _target_id, _note, _anonymous) + on conflict (created_by_id, target_id) do update set note = excluded.note, anonymous = excluded.anonymous; elseif reference = 'constitution'::regclass then - insert into vote_for_constitution (citizen_id, target_id, note, anonymous) - values (_citizen_id, _target_id, _note, _anonymous) - on conflict (citizen_id, target_id) do update set + insert into vote_for_constitution (created_by_id, target_id, note, anonymous) + values (_created_by_id, _target_id, _note, _anonymous) + on conflict (created_by_id, target_id) do update set note = excluded.note, anonymous = excluded.anonymous; elseif reference = 'comment_on_article'::regclass then - insert into vote_for_comment_on_article (citizen_id, target_id, note, anonymous) - values (_citizen_id, _target_id, _note, _anonymous) - on conflict (citizen_id, target_id) do update set + insert into vote_for_comment_on_article (created_by_id, target_id, note, anonymous) + values (_created_by_id, _target_id, _note, _anonymous) + on conflict (created_by_id, target_id) do update set note = excluded.note, anonymous = excluded.anonymous; elseif reference = 'comment_on_constitution'::regclass then - insert into vote_for_comment_on_constitution (citizen_id, target_id, note, anonymous) - values (_citizen_id, _target_id, _note, _anonymous) - on conflict (citizen_id, target_id) do update set + insert into vote_for_comment_on_constitution (created_by_id, target_id, note, anonymous) + values (_created_by_id, _target_id, _note, _anonymous) + on conflict (created_by_id, target_id) do update set note = excluded.note, anonymous = excluded.anonymous; else diff --git a/src/main/resources/sql/migrations/0000-init_schema.up.sql b/src/main/resources/sql/migrations/0000-init_schema.up.sql index d4462c3..19772ea 100644 --- a/src/main/resources/sql/migrations/0000-init_schema.up.sql +++ b/src/main/resources/sql/migrations/0000-init_schema.up.sql @@ -186,43 +186,43 @@ create table extra ( id uuid default uuid_generate_v4() not null primary key, 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_reference regclass not null ); create table follow ( - foreign key (citizen_id) references citizen (id), + foreign key (created_by_id) references citizen (id), primary key (id), - unique (citizen_id, target_id) + unique (created_by_id, target_id) ) inherits (extra); create table follow_article ( 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), primary key (id), - unique (citizen_id, target_id) + unique (created_by_id, target_id) ) inherits (follow); create table follow_constitution ( 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), primary key (id), - unique (citizen_id, target_id) + unique (created_by_id, target_id) ) inherits (follow); create table follow_citizen ( 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), primary key (id), - unique (citizen_id, target_id) + unique (created_by_id, target_id) ) inherits (follow); @@ -231,9 +231,9 @@ create table comment ( updated_at timestamptz default now() not null check ( updated_at >= created_at ), "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[], - foreign key (citizen_id) references citizen (id), + foreign key (created_by_id) references citizen (id), primary key (id) ) inherits (extra); @@ -244,7 +244,7 @@ create or replace function set_comment_parents_ids() returns trigger language plpgsql as $$ begin - if(new.parent_id is not null) then + if (new.parent_id is not null) then new.parents_ids = ( select com.parents_ids || com.id from "comment" com @@ -267,7 +267,7 @@ execute procedure set_comment_parents_ids(); create table comment_on_article ( 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 (parent_id) references comment_on_article (id), primary key (id) @@ -285,7 +285,7 @@ execute procedure set_comment_parents_ids(); create table comment_on_constitution ( 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 (parent_id) references comment_on_constitution (id), primary key (id) @@ -306,45 +306,45 @@ create table vote ( anonymous boolean default true not null, 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), - unique (citizen_id, target_id) + unique (created_by_id, target_id) ) inherits (extra); create table vote_for_article ( target_reference regclass default 'article'::regclass not null, 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), - unique (citizen_id, target_id) + unique (created_by_id, target_id) ) inherits (vote); create table vote_for_constitution ( target_reference regclass default 'constitution'::regclass not null, 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), - unique (citizen_id, target_id) + unique (created_by_id, target_id) ) inherits (vote); create table vote_for_comment_on_article ( target_reference regclass default 'comment_on_article'::regclass not null, 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), - unique (citizen_id, target_id) + unique (created_by_id, target_id) ) inherits (vote); create table vote_for_comment_on_constitution ( target_reference regclass default 'comment_on_constitution'::regclass not null, 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), - unique (citizen_id, target_id) + unique (created_by_id, target_id) ) inherits (vote); -- Stats diff --git a/src/test/kotlin/FollowTest.kt b/src/test/kotlin/FollowTest.kt index 8589a85..5951a77 100644 --- a/src/test/kotlin/FollowTest.kt +++ b/src/test/kotlin/FollowTest.kt @@ -21,7 +21,7 @@ class FollowTest { @Language("JSON") private val followJson: String = """{ "id":"bae81585-d985-4d7a-9b58-3a13e911688a", - "citizen":{ + "created_by":{ "id":"4a87ad24-187a-46a8-97ab-00b30a24e561", "name":{ "first_name":"Jaque", @@ -76,7 +76,7 @@ class FollowTest { createdBy = citizen ) val follow = Follow( - citizen = citizen, + createdBy = citizen, target = article ) follow.serialize().contains("""Hello world!""") shouldBe true diff --git a/src/test/resources/feature/auth.feature b/src/test/resources/feature/auth.feature index a00a766..5df7f2c 100644 --- a/src/test/resources/feature/auth.feature +++ b/src/test/resources/feature/auth.feature @@ -36,7 +36,7 @@ Feature: Auth routes When I send a POST request to "/login" with body: """ { - "name": "username1", + "name": "username-1", "password": "azerty" } """ diff --git a/src/test/sql/comment.sql b/src/test/sql/comment.sql index 257ae62..b29c35b 100644 --- a/src/test/sql/comment.sql +++ b/src/test/sql/comment.sql @@ -50,7 +50,7 @@ begin select "comment"( reference => 'article'::regclass, target_id => (created_article->>'id')::uuid, - citizen_id => _citizen_id, + created_by_id => _citizen_id, content => 'Ho my god !'::text ) into _comment_id; assert (select count(*) = 1 from "comment"), 'comment must be inserted'; @@ -86,7 +86,7 @@ begin select "comment"( reference => 'article'::regclass, target_id => (created_article->>'id')::uuid, - citizen_id => _citizen_id, + created_by_id => _citizen_id, content => 'God not exist'::text, parent_id => _comment_id::uuid ) into _comment_id_response; @@ -94,7 +94,7 @@ begin select "comment"( reference => 'article'::regclass, target_id => (created_article->>'id')::uuid, - citizen_id => _citizen_id, + created_by_id => _citizen_id, content => 'are you really sure ?'::text, parent_id => _comment_id_response::uuid ) into _comment_id_response2; diff --git a/src/test/sql/vote.sql b/src/test/sql/vote.sql index ee0af7e..46b4959 100644 --- a/src/test/sql/vote.sql +++ b/src/test/sql/vote.sql @@ -41,7 +41,7 @@ begin perform vote( reference => 'article'::regclass, _target_id => (created_article->>'id')::uuid, - _citizen_id => _citizen_id, + _created_by_id => _citizen_id, _note => 1 ); assert (select count(*) = 1 from vote_for_article), 'vote must be inserted'; @@ -50,7 +50,7 @@ begin perform vote( reference => 'article'::regclass, _target_id => (created_article->>'id')::uuid, - _citizen_id => _citizen_id, + _created_by_id => _citizen_id, _note => -1 ); assert (select count(*) = 1 from vote_for_article), 'vote must be inserted'; @@ -60,7 +60,7 @@ begin perform vote( reference => 'article'::regclass, _target_id => (created_article->>'id')::uuid, - _citizen_id => _citizen_id, + _created_by_id => _citizen_id, _note => -10 ); assert false, 'vote must be throw exception if note is not -1, 0 or 1';