Giter Site home page Giter Site logo

htn-backend-challenge's Introduction

Hack The North Back-end Challenge

This REST API has the basic requirements and maybe more (time permitting)

Built using node and express.js, with sqlite3 as the database.

Getting Started

After cloning the repo, you would need to run:

npm install express
npm install sqlite3
npm install

Scripts

Note that there are multiple script files:

  • database.js - sets up the database
  • reset.js - resets the database
  • init.js - inserts the fake user data into the database
  • server.js - handles API calls/the server

To start get the server working run the commands IN THE EXACT ORDER BELOW:

node database
node init.js
node server.js

Note that the command node init.js may take some time to run, wait for it to finish.

Also, at any point, you can always run node reset.js to reset the database.

Schema

The data is split into two tables (normalization). The chosen id specifier for users was simply an unique integer, or simply their index in the array.

user table:

CREATE TABLE user (
            id INTEGER PRIMARY KEY,
            name TEXT NOT NULL, 
            email TEXT UNIQUE, 
            phone TEXT NOT NULL,
            company TEXT NOT NULL

skill table:

CREATE TABLE skill (
            user_id INTEGER NOT NULL, 
            skill_name TEXT NOT NULL,
            rating INTEGER NOT NULL,
            FOREIGN KEY(user_id) REFERENCES user(id)

TUser Data type:

{
  "name": <string>,
  "company": <string>,
  "email": <string>,
  "phone": <string>,
  "skills": [
    {
      "skill": <string>,
      "rating": <int>
    }
  ]
}

TSkill Data type:

{
    "name": <string>,
    "frequency": <int>
}

API Endpoints

Below are the specifications for each of the endpoints.

Users endpoints

To get all user data, the endpoint is at:

localhost:8000/api/users/

OR, make a request:

GET localhost:8000/api/users/

Which would return an array of TUser, matching the same format as in the fake user data file (user_data.json()).

To get a specific user, the endpoint is at

localhost:8000/api/users/?id=<user_id>

OR, make a request:

GET localhost:8000/api/users/?id=<user_id>

Where <user_id> is to be replaced by the id of the user (an integer).

This request will return a TUser. If an invalid id was given, it will returns 400.

Updating User Data endpoint

To update a user, make a request:

PATCH localhost:8000/api/users/?id=<user_id>

With the following JSON body format:

{
    "<key>": "<value>",
    ...
}

Where <user_id> is to be replaced with a valid integer representing the user id, and <key> and <value> are valid values as specified in the user format. E.g, <key> should be one of ["name", "company", "email", "phone", "skills"]; Returns 400 for any invalid values.

Skills Endpoint

Making a request:

GET localhost:8000/api/skills/?min_frequency=<a>&max_frequency=<b>

Where <a> and <b> are to be replaced with integers specifiying the frequency range.

This request will retrieve and return an array of TSkill with all the skills with frequency (number of users with this skill) in the range [<a>, <b>] (inclusive)

Note that they are optional, i.e you can make a request without the min_frequency and max_frequency, and you will get an array of TSkill representing all the skills.

Returns 400 for any invalid values for min and max frequency.

Remarks

That is it for now, was a really fun project, and I learned a lot, especially from the debugging sessions. There are comments in the code with details about improvements, possible modifications, and of course documentation.

I have a new-found love-hate relationship with sqlite now.

htn-backend-challenge's People

Contributors

magicalsoup avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.