Skip to content

ChihHaoChen/Learch_REST_API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PROJECT

TrainingBuddy API

The API implementation for TrainingBuddy APP

INSTALL | FLOWCHART | USAGE | Database | AUTHOR | CONTRIBUTE | LICENSE | SUPPORT

MOTIVATION

This private repository is used to develope the back-end API for the TrainingBudday APP.

GETTING STARTED

The codes have been migrated from Heroku to DigitalOcean, and can be accessed through the HTTP address:

http://159.203.33.255:3000, where 3000 is the default port number for this API.

In the following usage guide, the keyword url will be referred to this HTTP address. Currently there are three models implemented in this back-end development:

  • users,
  • facebookusers,
  • tb_events.

All models support CRUB operations with Express and MongoDB. The difference between users and facebookusers is the field of password, since facebook login does not need users' passwords.

Routes about users:

  • POST /users
  • GET /users/me
  • DELETE /users/me/token
  • POST /users/login
  • PATCH /user/profile/:userId

Routes about facebookusers:

  • GET /users/facebook/auth
  • GET /users/me
  • DELETE /users/facebook/logout
  • PATCH /users/profile/:userId

Routes about tb_events:

  • POST /tb_events
  • GET /tb_events
  • GET /tb_events/:id
  • GET /tb_events/users/:userId
  • DELETE /tb_events/:id
  • PATCH /tb_events/:id
  • PATCH /tb_events/join/:id
  • PATCH /tb_events/drop/:id

Routes about some other features:

  • GET /chats/:id
  • GET /api/images/upload

Public routes can be invokded without tokens, while private ones require tokens to execute the operations. Therefore, users can check the tb_events created by other users, but they can only delete and update their own tb_events. All tb_events will be created with users' token as one of the key value.

Regarding the security of users' data, all passwords that users provided will be hashed before being stored in our database. In current stage, the authentication only allows new users to sign up with their email/password. There are some other tasks are on-going for TrainingBuddy APP.

  • Integration of Facebook authentication.
  • Introduction of new feature - chatting.
  • To upload pictures or videos of users/tb_events.
  • Push Notifications.
  • Other features may also be supported in the future.

INSTALLATION

All the required libraries can be found in package.json.

npm install

FLOWCHART

Optional Text

USAGE

  • POST /users => To allow new users to sign up.
var http = require("http");

var options = {
  "method": "POST",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "users"
  ],
  "headers": {
    "Content-Type": "application/json"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({ email: 'chao0716_8@gmail.com', password: 'test123' }));
req.end();
  • GET /users/me => This route can be used to fetch user profiles, and supports both models of user and facebookusers if token x-auth is provided in the header.
var http = require("http");

var options = {
  "method": "GET",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "users",
    "me"
  ],
  "headers": {
    "x-auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTg4YmFmMDc3Yzk1YTY5OGI5ZjFhNWYiLCJhY2Nlc3MiOiJhdXRoIiwiaWF0IjoxNTE4OTExNjA5fQ.KCYOqMQu4jLCWnGkWahxegpJ-0UZt6q-LCtURPKquak"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.end();
  • DELETE /users/me/token => This private route allows users to log out, and to delete the tokens attached to the users.
var http = require("http");

var options = {
  "method": "DELETE",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "users",
    "me",
    "token"
  ],
  "headers": {
    "x-auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTg4YmFmMDc3Yzk1YTY5OGI5ZjFhNWYiLCJhY2Nlc3MiOiJhdXRoIiwiaWF0IjoxNTE4OTExNjA5fQ.KCYOqMQu4jLCWnGkWahxegpJ-0UZt6q-LCtURPKquak"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.end();
  • POST /users/login => To allow a user who has signed on before to log on, and to give him/her a new token for private routes.
var http = require("http");

var options = {
  "method": "POST",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "users",
    "login"
  ],
  "headers": {
    "Content-Type": "application/json"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({ email: 'chao0716_111@gmail.com', password: 'test123' }));
req.end();
  • PATCH /user/profile/:userId => This private routes allow users to update their profiles, and supports both the models of users and facebookusers.
var http = require("http");

var options = {
  "method": "PATCH",
  "hostname": [
    "{{url}}"
  ],
  "path": [
    "users",
    "profile",
    "{{userId}}"
  ],
  "headers": {
    "Content-Type": "application/json",
    "x-auth": "{{x-auth}}"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({ gender: 'female',
  name:
   [ { firstName: 'Catherine',
       middleName: 'MiddleName',
       lastName: 'Kate',
       userName: 'CK' } ],
  address:
   [ { street: 'Monkland 4410',
       city: 'Montreal',
       province: 'Quebec',
       state: 'NA',
       country: 'Canada',
       postCode: 'H4B 2S9' } ],
  title: 'Mrs.',
  birthOfDate: [ { year: '1971', month: 'Jan', date: '10' } ],
  phone: '514-872-8588',
  occupation: 'Trainer',
  rate: 4 }));
req.end();
  • GET /users/facebook/auth => This route will bring Facebook authentication page. A callback route will be triggered after successfully log in their profiles. In the meantime, this callback route will be redirected to another route to send x-auth token in the header, which can be used to access other private routes offered in this API.

  • DELETE /users/facebook/logout => This route will log users out, and remove the token stored in database. Strongly recommended to be executed when users have no activities in session in addition to the common log-out function.

var http = require("http");

var options = {
  "method": "DELETE",
  "hostname": [
    "{{url}}"
  ],
  "path": [
    "users",
    "facebook",
    "logout"
  ],
  "headers": {
    "x-auth": "EAAIFJPAxW3wBAK2cMBj7VrzZAJrPDyij547w9KXe2VHl6uzjRXXyV5m0svVgDvPtn7Nb20zKMlfZBZAbDRwtEZBDxOZBrhZAlomHJ7Llj9aU0IWKfepAbOM9ipw6ObIF5bMRq3aSZB5vgdVDTQakBbTSdbrOxDnEIPkosdE5d0pqnAeb8f3ke3T"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.end();
  • POST /tb_events => This private route allows a user to post tb_events.
var http = require("http");

var options = {
  "method": "POST",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "tb_events"
  ],
  "headers": {
    "Content-Type": "application/json",
    "x-auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTg4YmFmMDc3Yzk1YTY5OGI5ZjFhNWYiLCJhY2Nlc3MiOiJhdXRoIiwiaWF0IjoxNTE4OTExNjA5fQ.KCYOqMQu4jLCWnGkWahxegpJ-0UZt6q-LCtURPKquak"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({ name: 'Body Building',
  activityPicked: 'Training',
  date: [ { dateFrom: '2018-02-01', dateTo: '2018-03-01' } ],
  place: [ { geo_lat: 20, geo_lng: 50 } ],
  age_suggest: [ { start: 18, end: 50 } ],
  num_people: 80,
  time_duration: [ { time: '02:00:00', timeFrom: '18:00:00', timeTo: '20:00:00' } ],
  level: 'beginner',
  description: [ { comments: 'For muscles', rating: 4.7 } ] }));
req.end();
  • GET /tb_events => This route allows users to see all available tb_events.
var http = require("http");

var options = {
  "method": "GET",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "tb_events",
    ""
  ],
  "headers": {}
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.end();
  • GET /tb_events/users/:userId => This private route allows users to fetch tb_events posted by a specific user according to his/her userID.
var http = require("http");

var options = {
 "method": "GET",
 "hostname": [
   "159.203.33.255:3000"
 ],
 "path": [
   "tb_events",
   "users",
   "5a88b9f277c95a698b9f1a50"
 ],
 "headers": {
   "x-auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTg4YmFmMDc3Yzk1YTY5OGI5ZjFhNWYiLCJhY2Nlc3MiOiJhdXRoIiwiaWF0IjoxNTE4OTExNjA5fQ.KCYOqMQu4jLCWnGkWahxegpJ-0UZt6q-LCtURPKquak"
 }
};

var req = http.request(options, function (res) {
 var chunks = [];

 res.on("data", function (chunk) {
   chunks.push(chunk);
 });

 res.on("end", function () {
   var body = Buffer.concat(chunks);
   console.log(body.toString());
 });
});

req.end();
  • DELETE /tb_events/:id => This private route gives users to delete tb_events they posted.
var http = require("http");

var options = {
 "method": "DELETE",
 "hostname": [
   "159.203.33.255:3000"
 ],
 "path": [
   "tb_events",
   "5a88bd9177c95a698b9f1a78"
 ],
 "headers": {
   "x-auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTg4YmFmMDc3Yzk1YTY5OGI5ZjFhNWYiLCJhY2Nlc3MiOiJhdXRoIiwiaWF0IjoxNTE4OTExNjA5fQ.KCYOqMQu4jLCWnGkWahxegpJ-0UZt6q-LCtURPKquak"
 }
};

var req = http.request(options, function (res) {
 var chunks = [];

 res.on("data", function (chunk) {
   chunks.push(chunk);
 });

 res.on("end", function () {
   var body = Buffer.concat(chunks);
   console.log(body.toString());
 });
});

req.end();
  • PATCH /tb_events/join/:id => This private route gives users to join events.
var http = require("http");

var options = {
  "method": "PATCH",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "tb_events",
    "join",
    "5aa70bbb58b72c16c45a8a7c"
  ],
  "headers": {
    "Content-Type": "application/json",
    "x-auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTk2ZDk4NWQxNDA5NzZhZWMwN2I1ODAiLCJhY2Nlc3MiOiJhdXRoIiwiaWF0IjoxNTIxNDg0OTE2fQ.tOKSZeZfBwTqSr7DraULBahPlB9gNAtCpKFLWgVXqB4"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({ gender: 'female',
  name: 
   [ { firstName: 'Catherine',
       middleName: 'MiddleName',
       lastName: 'Kate',
       userName: 'CK' } ],
  address: 
   [ { street: 'Monkland 4410',
       city: 'Montreal',
       province: 'Quebec',
       state: 'NA',
       country: 'Canada',
       postCode: 'H4B 2S9' } ],
  title: 'Mrs.',
  birthOfDate: [ { year: '1971', month: 'Jan', date: '10' } ],
  phone: '514-872-8588',
  occupation: 'Trainer',
  rate: 4 }));
req.end();
  • PATCH /tb_events/drop/:id => This private route gives users to drop themselves from events.
var http = require("http");

var options = {
  "method": "PATCH",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "tb_events",
    "drop",
    "5aa70bbb58b72c16c45a8a7c"
  ],
  "headers": {
    "Content-Type": "application/json",
    "x-auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTk2ZDk4NWQxNDA5NzZhZWMwN2I1ODAiLCJhY2Nlc3MiOiJhdXRoIiwiaWF0IjoxNTIxNDg0OTE2fQ.tOKSZeZfBwTqSr7DraULBahPlB9gNAtCpKFLWgVXqB4"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.write(JSON.stringify({ gender: 'female',
  name: 
   [ { firstName: 'Catherine',
       middleName: 'MiddleName',
       lastName: 'Kate',
       userName: 'CK' } ],
  address: 
   [ { street: 'Monkland 4410',
       city: 'Montreal',
       province: 'Quebec',
       state: 'NA',
       country: 'Canada',
       postCode: 'H4B 2S9' } ],
  title: 'Mrs.',
  birthOfDate: [ { year: '1971', month: 'Jan', date: '10' } ],
  phone: '514-872-8588',
  occupation: 'Trainer',
  rate: 4 }));
req.end();
  • GET /chats/:id => This private route used to set up the channel for chatting associated to a specific event.
var http = require("http");

var options = {
  "method": "GET",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "chats",
    "5aa576547ee1da097beb76b0"
  ],
  "headers": {
    "Content-Type": "application/json",
    "x-auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTk2ZDk4NWQxNDA5NzZhZWMwN2I1ODAiLCJhY2Nlc3MiOiJhdXRoIiwiaWF0IjoxNTIxNDg0OTE2fQ.tOKSZeZfBwTqSr7DraULBahPlB9gNAtCpKFLWgVXqB4"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.end();
  • GET /api/images/upload => This private route allows users to upload images either about their profiles and tb_event posts. Please refer to the following diagram for the detailed step to upload images. Optional Text
var http = require("http");

var options = {
  "method": "GET",
  "hostname": [
    "159.203.33.255:3000"
  ],
  "path": [
    "api",
    "images",
    "upload"
  ],
  "headers": {
    "x-auth": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YTk2ZDk4NWQxNDA5NzZhZWMwN2I1ODAiLCJhY2Nlc3MiOiJhdXRoIiwiaWF0IjoxNTIxNDg0OTE2fQ.tOKSZeZfBwTqSr7DraULBahPlB9gNAtCpKFLWgVXqB4"
  }
};

var req = http.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });

  res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log(body.toString());
  });
});

req.end();

All the requests can also be found in POSTMAN Documentation. In addition, a test file has been uploaded to run self-test, which will be helpful about how to send reqeusts and fetch responses through this API.

Database

The database employed in this back-end API is installed in DigitalOcean, a platform of Database-as-a-Service for MongoDB.

AUTHOR

You can just email me.

ACKNOWLEDGMENTS

List of people and project that inspired creation of this one:

CONTRIBUTION & LICENSE

Check out (if any) contribution guide or license for more details.

PRODUCTION STATUS & SUPPORT

You should be aware that this project is supported solely by me and provided as is.
If you want to become a patron or offer me a support please follow here.


Go back to the project description

Copyright © 2017 Davronov Alexander

About

The API implementation for TrainingBuddy APP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published