Skip to content

malmike/pop-api

 
 

Repository files navigation

CircleCI Maintainability Test Coverage

Population Management API

The Population Management API is a Restful API for managing population records of locations. Locations can be nested in other locations.

The API is set up in such a way that if a location has sub locations, it's Male, Female and Total are equal to the sum of sublocations for the different locations. If a location doesn't have any sublocations, its population is equivalent to what was passed when creating it.

Considering that locations can be nested in other locations. We have limited updating a sublocation's parent. We advise the user to delete the location and create a new one.

Deleting a parent location deletes all it's children / sublocations.

This API has been developed and tested on macOS High Sierra Version 10.13.6

Installation

  • Install Nodejs 8 or above
  • Install Mongo DB (guide here)
  • Clone the repo onto your computer
  • Update the env file with your environment variables
  • Install packages npm install
  • Run ./run.sh to start the application

Features

  • Location - creation, update, deletion, getting single location and all locations.
  • Nested / Sub Location - creation, update, deleting, and retrieving sublocations

Endpoints

Type API Endpoint Description
POST /locations Creates a new parent location. Requires male, female as number payloads and name as string

Sample Payload
{
  "name": "Jinja",
  "male": 10,
  "female": 10
}

Sample Response
{
  "id": "5c85654dfad9b0761e73a266",
  "name": "Jinja",
  "male": 10,
  "female": "10",
  "total": 20,
  "createdAt": "2019-03-10T19:28:13.874Z",
  "updatedAt": "2019-03-10T19:28:13.874Z",
  "subLocations": [],
  "parentLocationId": null
}
POST /locations Creates a new nested / sublocation location. Requires male, female as number payloads and name and parent location as string

Sample Payload
{
  "name": "Jinja",
  "male": 10,
  "female": 10,
  "parent": "5c85654dfad9b0761e73a266"
}

Sample Response
{
  "id": "5c838eb1eac8a9f683c10187",
  "name": "Jinja",
  "male": 10,
  "female": "10",
  "total": 20,
  "parent": [],
  "createdAt": "2019-03-10T19:28:13.874Z",
  "updatedAt": "2019-03-10T19:28:13.874Z",
  "subLocations": [],
  "parentLocationId": "5c85654dfad9b0761e73a266"
}
GET /locations Gets all locations. Returns parent and nested locations.

Sample Response
[
   {
     "id": "5c838e83eac8a9f683c10185",
     "name": "Jinja",
     "parentLocationId": null,
     "male": 1101,
     "female": 2206,
     "total": 3307,
     "subLocations": [
       {
         "id": "5c838eb1eac8a9f683c10187",
         "name": "Butembe"
       },
       {
         "id": "5c8416b416a2fc6bc4be2111",
         "name": "Jinja Municipality"
       }
     ],
     "createdAt": "2019-03-09T09:59:31.791Z",
     "updatedAt": "2019-03-10T14:36:50.920Z"
   },
   {
     "id": "5c838eb1eac8a9f683c10187",
     "name": "Butembe",
     "female": 200,
     "male": 100,
     "total": 300,
     "createdAt": "2019-03-09T10:00:17.772Z",
     "updatedAt": "2019-03-09T10:00:17.772Z",
     "subLocations": [],
     "parentLocationId": "5c838e83eac8a9f683c10185"
   },
   {
     "id": "5c8416b416a2fc6bc4be2111",
     "name": "Jinja Municipality",
     "female": 2006,
     "male": 1001,
     "total": 3007,
     "createdAt": "2019-03-09T10:00:17.772Z",
     "updatedAt": "2019-03-09T10:00:17.772Z",
     "subLocations": [],
     "parentLocationId": "5c838e83eac8a9f683c10185"
   }
]
GET /locations/:id Get Single Location by id

Sample Response
{
     "id": "5c838e83eac8a9f683c10185",
     "name": "Jinja",
     "parentLocationId": null,
     "male": 1101,
     "female": 2206,
     "total": 3007,
     "subLocations": [
         {
            "id": "5c838eb1eac8a9f683c10187",
            "name": "Butembe"
         },
         {
            "id": "5c8416b416a2fc6bc4be2111",
            "name": "Jinja Municipality"
         }
       ],
       "createdAt": "2019-03-09T09:59:31.791Z",
       "updatedAt": "2019-03-10T14:36:50.920Z"
   }
PUT /locations/:id Update location name, female and male population

Sample Payload

{
   "name": "Butembe County",
   "famale": 200,
   "male": 199,
}

Sample Response

{
  "message": "Location updated succesfully"
}
DELETE /locations/:id Delete location. Replace location id in the url with the actually ID of the location.

Sample Response

{
  "message": "Location deleted succesfully"
}

Testing

The Application tests can be executed by running the command below on your console.

npm test

About

Population Management API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 99.5%
  • Shell 0.5%