/Speer-API

REST API for Speer back end assessment

Primary LanguageJavaScript

Speer-API

Backend REST API for:

  1. User login/registration system
  2. Private chat system between two users
  3. Tweet post create/retreive/update/delete system

API Endpoints

Logs in user (requires username and password in request body):

POST /login

Registers user (requires username and password in request body):

POST /register

Retrieves private chat associated with another user:

GET /chat/{receiver_username}

Send private chat to another user (requires message in request body):

POST /chat/{receiver_username}

Retreives a tweet post:

GET /tweet/{tweet_id}

Creates a new tweet post (requires message in request body):

POST /tweet

Updates an existing tweet (requires message in request body):

PATCH /tweet/{tweet_id}

Deletes an existing tweet:

DELETE /tweet/{tweet_id}

Setting Up Database

This project depends on external MySQL server running. After configuring .env file at the root of this project hierarchy and creating a new user in MySQL server, run the following queries in MySQL console to create necessary data:

CREATE DATABASE Speer_API;
Use Speer;

CREATE TABLE Users (
        id INT AUTO_INCREMENT,
        username VARCHAR(100),
        password VARCHAR(255),
        PRIMARY KEY (id)
);

CREATE TABLE Chats (
        id INT AUTO_INCREMENT,
        sender_id INT,
        receiver_id INT,
        message VARCHAR(4096),
        PRIMARY KEY (id),
        FOREIGN KEY (sender_id) REFERENCES Users(id),
        FOREIGN KEY (receiver_id) REFERENCES Users(id)
);

CREATE TABLE Tweets (
        id INT AUTO_INCREMENT,
        user_id INT,
        tweet VARCHAR(4096),
        PRIMARY KEY (id),
        FOREIGN KEY (user_id) REFERENCES Users(id)
);

Unit Testing

Test assumes the DB already has following 3 users (username:password):

  • admin:admin
  • eric:eric
  • sarah:sarah

Test assumes the DB already has a tweet (with id 5) in Tweets table.

Make sure to add these entries to the correct tables prior to unit testing!

Unit testing is available with Mocha framework. Simply run following line on console:

npm test

image