#29 Implement Workgroup members query
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
create or replace function add_workgroup_members(in _id uuid, inout resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
begin
|
||||
insert into citizen_in_workgroup (citizen_id, workgroup_id)
|
||||
select
|
||||
(z->>'id')::uuid,
|
||||
_id::uuid
|
||||
from json_array_elements(resource) z
|
||||
where (z->>'id') is not null
|
||||
on conflict do nothing;
|
||||
|
||||
select find_workgroup_members(_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists update_workgroup_members(in uuid, inout json);
|
||||
@@ -0,0 +1,16 @@
|
||||
create or replace function find_workgroup_members(in _id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
begin
|
||||
select json_agg(t) into resource
|
||||
from (
|
||||
select
|
||||
z.*,
|
||||
find_user_by_id(z.user_id) as "user"
|
||||
from citizen_in_workgroup as ciw
|
||||
join citizen z on z.id = ciw.citizen_id
|
||||
where ciw.workgroup_id = _id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_workgroup_members(uuid, out json);
|
||||
@@ -0,0 +1,17 @@
|
||||
create or replace function remove_workgroup_members(in _id uuid, inout resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
begin
|
||||
delete from citizen_in_workgroup
|
||||
where workgroup_id = _id
|
||||
and citizen_id in (
|
||||
select
|
||||
(z->>'id')::uuid
|
||||
from json_array_elements(resource) z
|
||||
);
|
||||
|
||||
select find_workgroup_members(_id) into resource;
|
||||
end
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists remove_workgroup_members(in uuid, inout json);
|
||||
@@ -0,0 +1,26 @@
|
||||
create or replace function update_workgroup_members(in _id uuid, inout resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
begin
|
||||
insert into citizen_in_workgroup (citizen_id, workgroup_id)
|
||||
select
|
||||
(z->>'id')::uuid,
|
||||
_id::uuid
|
||||
from json_array_elements(resource) z
|
||||
where (z->>'id') is not null
|
||||
on conflict do nothing;
|
||||
|
||||
delete from citizen_in_workgroup
|
||||
where workgroup_id = _id
|
||||
and citizen_id not in(
|
||||
select
|
||||
(z->>'id')::uuid
|
||||
from json_array_elements(resource) z
|
||||
where (z->>'id') is not null
|
||||
);
|
||||
|
||||
select find_workgroup_members(_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists update_workgroup_members(in uuid, inout json);
|
||||
@@ -21,6 +21,11 @@ begin
|
||||
logo = excluded.logo,
|
||||
owner_id = excluded.owner_id;
|
||||
|
||||
-- insert into citizen_in_workgroup (citizen_id, workgroup_id)
|
||||
-- select
|
||||
-- (resource->>'id')::uuid,
|
||||
-- new_id::uuid
|
||||
-- from json_populate_recordset(null::workgroup, resource->'members');
|
||||
|
||||
select find_workgroup_by_id(new_id) into resource;
|
||||
end;
|
||||
|
||||
Reference in New Issue
Block a user