BEGIN TRANSACTION;
SET search_path = public;

DROP SCHEMA IF EXISTS api CASCADE;
CREATE SCHEMA api;

DROP SCHEMA IF EXISTS _api CASCADE;
CREATE SCHEMA _api;

GRANT USAGE ON SCHEMA admin TO rest_anon, rest_user;
GRANT USAGE ON SCHEMA sys TO rest_anon, rest_user;

GRANT USAGE ON SCHEMA api TO rest_anon, rest_user;
GRANT USAGE ON SCHEMA _api TO rest_anon, rest_user;

-- util
\i /db/rest/util/_api_trim.sql;
\i /db/rest/util/_api_serve_media.sql;
\i /db/rest/util/_api_raise.sql;
\i /db/rest/util/_api_raise_null.sql;
\i /db/rest/util/_api_raise_unique.sql;
\i /db/rest/util/_api_validate_text.sql;
\i /db/rest/util/_api_get_user_id.sql;

-- user
\i /db/rest/user/api_user.sql;
\i /db/rest/user/api_user_insert.sql;
\i /db/rest/user/api_user_update.sql;
\i /db/rest/user/api_user_delete.sql;

-- post
\i /db/rest/post/api_post.sql;
\i /db/rest/post/api_post_insert.sql;
\i /db/rest/post/api_post_update.sql;
\i /db/rest/post/api_post_delete.sql;

-- comment
\i /db/rest/comment/api_comment.sql;
\i /db/rest/comment/api_comment_insert.sql;
\i /db/rest/comment/api_comment_update.sql;
\i /db/rest/comment/api_comment_delete.sql;

-- like
\i /db/rest/like/api_like.sql;
\i /db/rest/like/api_like_insert.sql;
\i /db/rest/like/api_like_update.sql;
\i /db/rest/like/api_like_delete.sql;

-- media
\i /db/rest/media/api_profile_avatar.sql;
\i /db/rest/media/api_profile_banner.sql;

-- login
\i /db/rest/login/_api_sign_jwt.sql;
\i /db/rest/login/_api_verify_jwt.sql;
\i /db/rest/login/_api_validate_role.sql;
\i /db/rest/login/api_login.sql;

COMMIT TRANSACTION;
