This repo is an attempt of mine to learn more about building a REST API in haskell and also to help Santa deliver presents on Christmas (In more brash words.. This is my bribe to Santa 😉)
The main code is present at app/Main.hs
. The REST routes are provided using the Spock framework. And the data is fetched from the Postgres Database.
Install postgres
Connect to postgres
$ sudo -i -u postgres
Restore the Database You can create your own if you hate my entires :P
postgres@santa:~$ createdb -T template0 restored_database
postgres@santa:~$ psql christmas < christmas_db.bak
postgres@santa:~$ psql
Connect to christmas DB
postgres=# \c christmas
Present
Column | Type | Nullable |
--------+-----------------------+-----------+
id | integer | not null |
name | character varying(50) | not null |
info | text | not null |
Indexes:
PRIMARY KEY -> (id)
Location
Column | Type | Nullable |
-----------+---------------+-----------+----------+
id | integer | not null |
latitude | numeric(10,5) | not null |
longitude | numeric(10,5) | not null |
Indexes:
PRIMARY KEY -> (id)
Child
Column | Type | Nullable |
-------------+-----------------------+-----------
id | integer | not null |
name | character varying(50) | not null |
naughty | smallint | |
location_id | integer | |
present_id | integer | |
Indexes:
PRIMARY KEY -> id
Foreign-key constraints:
FOREIGN KEY (location_id) REFERENCES location(id)
FOREIGN KEY (present_id) REFERENCES present(id)
Child_info A view to flatten the child table and get the info from foreign key
Column | Type | Collation | Nullable | Default
--------------+-----------------------+-----------+----------+---------
child_name | character varying(50) | | |
naughty | smallint | | |
latitude | numeric(10,5) | | |
longitude | numeric(10,5) | | |
present_name | character varying(50) | | |
info | text | | |
$ cd santapi
$ stack build --fast && stack exec Database-conn-exe
Once the app is built, you will be able to access it at http://localhost:8080/
Endpoint: http://localhost:8080/locations
Output:
[
{
"locLong": -61.7879,
"locLat": 42.5462
},
{
"locLong": 114.7253,
"locLat": 35.234
},
{
"locLong": 908.3912,
"locLat": 45.2131
},
]
Endpoint: http://localhost:8080/presents
Output:
[
{
"presentName": "Bike",
"presentInfo": "A GTX sports bike limited edition"
},
{
"presentName": "PS4",
"presentInfo": "Last of us Mega Edition"
},
{
"presentName": "Toy Train",
"presentInfo": "Cheap ass toy train, Ran out of budget"
}
]
Endpoint: http://localhost:8080/children
Output:
[
{
"childLocation": {
"locLong": 908.3912,
"locLat": 45.2131
},
"childNaughty": 10,
"childName": "Elend Venture",
"childPresent": {
"presentName": "Bike",
"presentInfo": "A GTX sports bike limited edition"
}
},
{
"childLocation": {
"locLong": -61.7879,
"locLat": 42.5462
},
"childNaughty": 80,
"childName": "Vin Misty",
"childPresent": {
"presentName": "Telescope",
"presentInfo": "Super magnified telescope"
}
},
]
Endpoint: http://localhost:8080/present/
Endpoint: http://localhost:8080/location/
Endpoint: http://localhost:8080/addPresent/
We'll use curl for POST request
$ curl -H "Content-Type: application/json" -d '{"presentName": "Radio", "presentInf": "HamRadio set"}' localhost:8080/addPresent
Endpoint: http://localhost:8080/addLocation/
We'll use curl for POST request
$ curl -H "Content-Type: application/json" -d '{"locLat": 87.32, "locLong": -16.34}' localhost:8080/addLocation
- Implement a POST request for Child table (Might require changes to the Datatype)
- Implement a GET request for Child with particular id
- Write a Blog Post explaining this