Skip to content
This repository was archived by the owner on Apr 16, 2022. It is now read-only.

Using the API

Ruslan Talpa edited this page Aug 28, 2018 · 6 revisions

!!! This documentation is outdated. New version here.

REST

Let's just check a few examples:

Get the projects of a specific client and all the active tasks for them

curl -s -G -X GET \
-H "Authorization: Bearer $JWT_TOKEN" \
http://localhost:8080/rest/projects \
--data-urlencode select="id,name,tasks(id,name)" \
--data-urlencode client_id="eq.1" \
--data-urlencode tasks.completed="eq.false"  | \
python -mjson.tool

Add a client and return it's new id and created_on field

curl -s -X POST \
-H "Authorization: Bearer $JWT_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.pgrst.object+json" \
-H 'Prefer: return=representation' \
-d '{"name":"Google","address":"Mountain View, California, United States"}'  \
http://localhost:8080/rest/clients?select=id,created_on | \
python -mjson.tool

Update a projects's name

curl -s -X PATCH \
-H "Authorization: Bearer $JWT_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.pgrst.object+json" \
-H 'Prefer: return=representation' \
-d '{"name":"Updated name"}'  \
"http://localhost:8080/rest/projects?select=id,name,created_on,updated_on&id=eq.1" | \
python -mjson.tool

You probably just noticed that after the operation above, updated_on field was still null. We forgot to give it a value. Easy fix, add this to db/src/data/tables.sql file

-- ...
create or replace function set_updated_on() returns trigger as $$
begin
  new.updated_on = now();
  return new;
end
$$ language plpgsql;

create trigger client_set_updated_on
before update on "client"
for each row execute procedure set_updated_on();


create trigger project_set_updated_on
before update on "project"
for each row execute procedure set_updated_on();

create trigger task_set_updated_on
before update on "task"
for each row execute procedure set_updated_on();

create trigger task_comment_set_updated_on
before update on "task_comment"
for each row execute procedure set_updated_on();

create trigger project_comment_set_updated_on
before update on "project_comment"
for each row execute procedure set_updated_on();

If you try that update again, you will see the property change each time.

Delete a task

curl -s -X DELETE \
-H "Authorization: Bearer $JWT_TOKEN" \
-H 'Prefer: return=representation' \
"http://localhost:8080/rest/tasks?select=id,created_on&id=eq.2" | \
python -mjson.tool

!!! info If you try to delete task #1, you will get an error telling you:

`Key is still referenced from table "task_comment"`

This is another example of a safety net the database gives you when you leverage its features, it will not let you corrupt your data by accident. We will have to be specific using ON DELETE what happens to related comments when a task is deleted. It might seem annoying at first but this strictness will save you a lot of headake in the long run.

Clone this wiki locally