Add SQL query for insert citizen with user
This commit is contained in:
@@ -11,9 +11,10 @@ import fr.dcproject.entity.Citizen as CitizenEntity
|
|||||||
class Citizen(override var requester: Requester) : RepositoryI<CitizenEntity> {
|
class Citizen(override var requester: Requester) : RepositoryI<CitizenEntity> {
|
||||||
override val entityName = CitizenEntity::class
|
override val entityName = CitizenEntity::class
|
||||||
|
|
||||||
fun findById(id: UUID): CitizenEntity? {
|
fun findById(id: UUID, withUser: Boolean = false): CitizenEntity? {
|
||||||
val function = requester.getFunction("find_citizen_by_id")
|
return requester
|
||||||
return function.selectOne("id" to id)
|
.getFunction(if (withUser) "find_citizen_by_id_with_user" else "find_citizen_by_id")
|
||||||
|
.selectOne("id" to id)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun find(
|
fun find(
|
||||||
@@ -38,4 +39,10 @@ class Citizen(override var requester: Requester) : RepositoryI<CitizenEntity> {
|
|||||||
.getFunction("upsert_citizen")
|
.getFunction("upsert_citizen")
|
||||||
.selectOne("resource" to citizen)
|
.selectOne("resource" to citizen)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createWithUser(citizen: CitizenEntity): CitizenEntity? {
|
||||||
|
return requester
|
||||||
|
.getFunction("insert_citizen_with_user")
|
||||||
|
.selectOne("resource" to citizen)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
create or replace function find_citizen_by_id_with_user(in id uuid, out resource json) language plpgsql as
|
||||||
|
$$
|
||||||
|
declare
|
||||||
|
_id alias for id;
|
||||||
|
begin
|
||||||
|
select to_json(t) into resource
|
||||||
|
from (
|
||||||
|
select
|
||||||
|
z.*,
|
||||||
|
u as "user"
|
||||||
|
from citizen as z
|
||||||
|
join "user" u on z.user_id = u.id
|
||||||
|
where z.id = _id
|
||||||
|
) as t;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop function if exists find_citizen_by_id_with_user(uuid, inout json);
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
create or replace function insert_citizen_with_user(inout resource json)
|
||||||
|
language plpgsql as
|
||||||
|
$$
|
||||||
|
declare
|
||||||
|
new_id uuid;
|
||||||
|
inserted_user json;
|
||||||
|
begin
|
||||||
|
select insert_user(resource->'user') into inserted_user;
|
||||||
|
|
||||||
|
insert into citizen (id, name, birthday, user_id, vote_annonymous, follow_annonymous)
|
||||||
|
select
|
||||||
|
coalesce(id, uuid_generate_v4()),
|
||||||
|
name,
|
||||||
|
birthday,
|
||||||
|
(inserted_user->>'id')::uuid,
|
||||||
|
coalesce(vote_annonymous, true),
|
||||||
|
coalesce(follow_annonymous, true)
|
||||||
|
from json_populate_record(null::citizen, resource)
|
||||||
|
on conflict (id) do update set
|
||||||
|
name = excluded.name,
|
||||||
|
birthday = excluded.birthday,
|
||||||
|
user_id = excluded.user_id,
|
||||||
|
vote_annonymous = excluded.vote_annonymous,
|
||||||
|
follow_annonymous = excluded.follow_annonymous
|
||||||
|
returning id into new_id;
|
||||||
|
|
||||||
|
select find_citizen_by_id_with_user(new_id) into resource;
|
||||||
|
end;
|
||||||
|
$$;
|
||||||
|
|
||||||
|
-- drop function if exists create_citizen_with_user(inout json);
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
do
|
do
|
||||||
$$
|
$$
|
||||||
declare
|
declare
|
||||||
wrong_citizen json;
|
wrong_citizen json;
|
||||||
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
created_user json := '{"username": "george", "plain_password": "azerty", "roles": ["ROLE_USER"]}';
|
||||||
_user_id uuid;
|
_user_id uuid;
|
||||||
created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01"}';
|
created_citizen json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01"}';
|
||||||
selected_citizen json;
|
created_citizen_with_user json := '{"name": {"first_name":"George", "last_name":"MICHEL"}, "birthday": "2001-01-01", "user":{"username": "george junior", "plain_password": "azerty", "roles": ["ROLE_USER"]}}';
|
||||||
|
selected_citizen json;
|
||||||
begin
|
begin
|
||||||
-- insert user for context
|
-- insert user for context
|
||||||
select insert_user(created_user) into created_user;
|
select insert_user(created_user) into created_user;
|
||||||
@@ -17,6 +18,11 @@ begin
|
|||||||
select upsert_citizen(created_citizen) into created_citizen;
|
select upsert_citizen(created_citizen) into created_citizen;
|
||||||
assert created_citizen->>'birthday' = '2001-01-01'::text, format('birthday of inserted citizen must be the same of the original object, %s != %s', created_citizen->>'birthday', '2001-01-01'::text);
|
assert created_citizen->>'birthday' = '2001-01-01'::text, format('birthday of inserted citizen must be the same of the original object, %s != %s', created_citizen->>'birthday', '2001-01-01'::text);
|
||||||
|
|
||||||
|
-- insert new citizen
|
||||||
|
select insert_citizen_with_user(created_citizen_with_user) into created_citizen_with_user;
|
||||||
|
assert created_citizen_with_user->>'birthday' = '2001-01-01'::text, format('birthday of inserted citizen must be the same of the original object, %s != %s', created_citizen->>'birthday', '2001-01-01'::text);
|
||||||
|
assert created_citizen_with_user#>>'{user, username}' = 'george junior', 'username must be george';
|
||||||
|
|
||||||
-- insert citizen without first name and test if throw exception
|
-- insert citizen without first name and test if throw exception
|
||||||
wrong_citizen := (created_citizen::jsonb - '{name, first_name}'::text[])::json;
|
wrong_citizen := (created_citizen::jsonb - '{name, first_name}'::text[])::json;
|
||||||
begin
|
begin
|
||||||
@@ -34,8 +40,8 @@ begin
|
|||||||
assert selected_citizen#>>'{name, first_name}' = 'George', format('first name must be George, %s', selected_citizen#>>'{name, first_name}');
|
assert selected_citizen#>>'{name, first_name}' = 'George', format('first name must be George, %s', selected_citizen#>>'{name, first_name}');
|
||||||
|
|
||||||
-- delete citizen
|
-- delete citizen
|
||||||
delete from citizen where user_id = _user_id;
|
delete from citizen;
|
||||||
delete from "user" where username = 'george';
|
delete from "user";
|
||||||
|
|
||||||
-- check if fint by id return null if citizen not exist
|
-- check if fint by id return null if citizen not exist
|
||||||
select find_citizen_by_user_id((created_citizen->>'user_id')::uuid) into selected_citizen;
|
select find_citizen_by_user_id((created_citizen->>'user_id')::uuid) into selected_citizen;
|
||||||
|
|||||||
Reference in New Issue
Block a user