feature #4: create function for article
implement generate_version_number()
This commit is contained in:
18
resources/functions/article/find_article_by_id.sql
Normal file
18
resources/functions/article/find_article_by_id.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
create or replace function find_article_by_id(in id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_json(t)
|
||||
from (
|
||||
select
|
||||
a.*,
|
||||
find_citizen_by_id(a.created_by_id) as created_by
|
||||
into resource
|
||||
from article as a
|
||||
where a.id = _id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_article_by_id(uuid, out json);
|
||||
@@ -0,0 +1,20 @@
|
||||
create or replace function find_last_article_by_version_id(in version_id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_version_id alias for version_id;
|
||||
begin
|
||||
select to_json(t)
|
||||
from (
|
||||
select
|
||||
a.*,
|
||||
find_citizen_by_id(a.created_by_id) as created_by
|
||||
into resource
|
||||
from article as a
|
||||
where a.version_id = _version_id
|
||||
order by a.version_number desc
|
||||
limit 1
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_last_article_by_version_id(uuid, inout json);
|
||||
23
resources/functions/article/upsert_article.sql
Normal file
23
resources/functions/article/upsert_article.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
create or replace procedure upsert_article(inout resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
new_id uuid;
|
||||
begin
|
||||
insert into article (version_id, created_by_id, title, annonymous, content, description, tags)
|
||||
select
|
||||
version_id,
|
||||
(resource#>>'{created_by, id}')::uuid,
|
||||
title,
|
||||
annonymous,
|
||||
content,
|
||||
description,
|
||||
tags
|
||||
from json_populate_record(null::article, resource)
|
||||
returning id into new_id;
|
||||
|
||||
select find_article_by_id(new_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists upsert_article(inout json);
|
||||
@@ -7,7 +7,7 @@ begin
|
||||
from (
|
||||
select
|
||||
z.*,
|
||||
find_user_by_id(z.user_id)
|
||||
find_user_by_id(z.user_id) as "user"
|
||||
from citizen as z
|
||||
where z.id = _id
|
||||
) as t;
|
||||
|
||||
@@ -7,7 +7,7 @@ begin
|
||||
from (
|
||||
select
|
||||
z.*,
|
||||
find_user_by_id(z.user_id)
|
||||
find_user_by_id(z.user_id) as "user"
|
||||
from citizen as z
|
||||
where z.user_id = _user_id
|
||||
) as t;
|
||||
|
||||
@@ -21,10 +21,7 @@ begin
|
||||
follow_annonymous = excluded.follow_annonymous
|
||||
returning id into new_id;
|
||||
|
||||
select to_json(z)
|
||||
into resource
|
||||
from citizen as z
|
||||
where z.id = new_id;
|
||||
select find_citizen_by_id(new_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
|
||||
@@ -9,4 +9,4 @@ begin
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_user_by_id(uuid, inout json);
|
||||
-- drop function if exists find_user_by_id(uuid, out json);
|
||||
@@ -11,9 +11,7 @@ begin
|
||||
from json_populate_record(null::"user", resource)
|
||||
returning id into new_id;
|
||||
|
||||
select to_json(u) into resource
|
||||
from "user" as u
|
||||
where u.id = new_id;
|
||||
select find_user_by_id(new_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user