Skip to content

A REST API webapp for Santa in Haskell using Spock and postgresql-simple

License

Notifications You must be signed in to change notification settings

Naveenaidu/santapi

Repository files navigation

A WebApp for Santa :3

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 😉)

Packages/Frameworks

  1. Spock - A web framework
  2. PostgreSQL-Simple

Overview

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.

Pre-requisite

Postgres

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

Databse Schema


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                  |           |          | 

Usage

Build the app

$ 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/

REST API's

Fetch All Locations

Endpoint: http://localhost:8080/locations

Output:

[
  {
    "locLong": -61.7879,
    "locLat": 42.5462
  },
  {
    "locLong": 114.7253,
    "locLat": 35.234
  },
  {
    "locLong": 908.3912,
    "locLat": 45.2131
  },

]

Fetch All Presents

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"
  }

]

Fetch All Children

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"
    }
  },

]

Fetch A present with particular ID

Endpoint: http://localhost:8080/present/

Fetch A location with particular ID

Endpoint: http://localhost:8080/location/

POST a present entry into table

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 

POST a location entry into table

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

TODO

  • 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

About

A REST API webapp for Santa in Haskell using Spock and postgresql-simple

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published