From a6838337eb920d6d167b65ee313ffb648d3355d2 Mon Sep 17 00:00:00 2001 From: Nishant Arora Date: Mon, 19 Sep 2016 11:42:43 +0530 Subject: [PATCH] Created users. GET all users and create user (POST) endpoint --- api/users.js | 50 ++++++++++++++++++ db/matterwiki.sqlite | Bin 9216 -> 10240 bytes index.js | 3 ++ migrations/20160919091337_users.js | 16 ++++++ .../20160919091811_add_users_to_articles.js | 12 +++++ .../20160919091818_add_users_to_archives.js | 12 +++++ models/article.js | 6 ++- models/user.js | 13 +++++ 8 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 migrations/20160919091337_users.js create mode 100644 migrations/20160919091811_add_users_to_articles.js create mode 100644 migrations/20160919091818_add_users_to_archives.js create mode 100644 models/user.js diff --git a/api/users.js b/api/users.js index e69de29..a4d5b11 100644 --- a/api/users.js +++ b/api/users.js @@ -0,0 +1,50 @@ +/* +This file contains all the endpoints related to topics. +For the method we use to categorize endpoints in file please read the top +comment in the articles.js (same directory). +*/ + + +// Importing the topics model +var Users = require('../models/user.js'); + +var db = require('../db.js'); //this file contains the knex file import. it's equal to knex=require('knex') + +module.exports = function(app) { + + + app.post('/api/users',function(req,res){ + /* + This is a POST endpoint which takes the user name, email, password, and about to create + a new user profile. + It responds with the created user object in the data key. + the error key in the returning object is a boolen which is false if there is no error and true otherwise + */ + Users.forge() + .save({name: req.body.name, email: req.body.email, password: req.body.password, about: req.body.about}) + .then(function (collection) { + res.json({error: false, data: collection.toJSON()}); + }) + .catch(function (err) { + res.status(500).json({error: true, data: {message: err.message}}); + }); + }); + + + app.get('/api/users',function(req,res){ + /* + This is a GET endpoint that responds with the list of all the topics in the topics table + the topics are present in the data object in the returning object. + the error key in the returning object is a boolen which is false if there is no error and true otherwise + */ + Users.forge() + .fetchAll() + .then(function (collection) { + res.json({error: false, data: collection.toJSON()}); + }) + .catch(function (err) { + res.status(500).json({error: true, data: {message: err.message}}); + }); + }); + +} diff --git a/db/matterwiki.sqlite b/db/matterwiki.sqlite index c7340f874472dbe926cd07d8fd91d4006eb6ab00..5a8137cc8e8b45280d59b11fb88e982655ccc7ca 100644 GIT binary patch delta 684 zcmZqhXb6}fCB(?hz`(!-#4sQ*QOAghk$Yppd}eWWUZ5yD({Tpoo6MV;Ynj8C4VnHh z9p5a-vXQCYn3I(unvvDWz|hRV($Er!EetK<6H`*+ON&#BisMW2;}eULGcwCki}kXK zSsB9}rhm{h=D^fsh_0z5GdTyL={5lGOYrJ&AQ2g zEc2MzIN2s|RM^4QWXA!~??3r~!aG5*=J?DMC56nqlGOCnqRDF%EwzI=*~P`h8CzLP z5|eUL!Oj8`T+TtRjv=lJA&yQyt`LhSFO)Q!cvq-iAuqo~A+Izi2k4;G+{DZrC519j z&?XjX7@3-CB1;z}78jT27Xj_Wr6MsYzqACmbaGK@Vo7RBd?G>(E7+FF2b9z%uTe7R z)&wOAM&@}8%=3WZJB2wZfsunj+Fn-C*f=kd`f@)HSqJFfg$)Ft9Q-!6F0zl?cOw delta 199 zcmZn&Xz-XICB*QDfq{V&h+%+tqK*+0!=H@_^O*%%c!8oU%wY`7H<>px*D{A~R^)JF zo*c%$j)#T$Faz^z=8MdCnGbIk3pISeTbF%v61*nj|N>c%oIMy@777Itx2S;pql&CwE^jGODFT3NZ68MZMn{a}2; gux+v;%Zka{6gE$mQF5JJqGZk~$d01CT#}O!0B5)_7ytkO diff --git a/index.js b/index.js index f657e0a..6825b2d 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,9 @@ require('./api/articles')(app); // Importing all endpoints for topics require('./api/topics')(app); +// Importing all endpoints for users +require('./api/users')(app); + app.use(express.static(__dirname + '/client')); app.listen(5000 || process.env.PORT, function(){ diff --git a/migrations/20160919091337_users.js b/migrations/20160919091337_users.js new file mode 100644 index 0000000..8c850f6 --- /dev/null +++ b/migrations/20160919091337_users.js @@ -0,0 +1,16 @@ + +exports.up = function(knex, Promise) { + return knex.schema.createTable('users', function (table) { + table.increments().primary(); + table.string('name').notNullable(); + table.string('email').notNullable(); + table.string('password').notNullable(); + table.string('about').notNullable(); + table.string('created_at').notNullable().defaultTo(knex.raw('CURRENT_TIMESTAMP')); + table.timestamp('updated_at').notNullable().defaultTo(knex.raw('CURRENT_TIMESTAMP')); + }) +}; + +exports.down = function(knex, Promise) { + return knex.schema.dropTable('users'); +}; diff --git a/migrations/20160919091811_add_users_to_articles.js b/migrations/20160919091811_add_users_to_articles.js new file mode 100644 index 0000000..f730145 --- /dev/null +++ b/migrations/20160919091811_add_users_to_articles.js @@ -0,0 +1,12 @@ + +exports.up = function(knex, Promise) { + return knex.schema.table('articles', function (table) { + table.integer('user_id').references('users.id'); + }) +}; + +exports.down = function(knex, Promise) { + return knex.schema.table('articles', function (table) { + table.dropColumn('user_id'); + }) +}; diff --git a/migrations/20160919091818_add_users_to_archives.js b/migrations/20160919091818_add_users_to_archives.js new file mode 100644 index 0000000..794822d --- /dev/null +++ b/migrations/20160919091818_add_users_to_archives.js @@ -0,0 +1,12 @@ + +exports.up = function(knex, Promise) { + return knex.schema.table('archives', function (table) { + table.integer('user_id').references('users.id'); + }) +}; + +exports.down = function(knex, Promise) { + return knex.schema.table('archives', function (table) { + table.dropColumn('user_id'); + }) +}; diff --git a/models/article.js b/models/article.js index 3de8753..aa0dbd3 100644 --- a/models/article.js +++ b/models/article.js @@ -2,14 +2,18 @@ var bookshelf = require('../bookshelf'); bookshelf.plugin('registry'); var Topic = require('./topic'); var Archives = require('./archive'); +var User = require('./user') var Article = bookshelf.Model.extend({ tableName: 'articles', topic: function() { return this.belongsTo('Topic','topic_id'); }, + user: function() { + return this.belongsTo('User','user_id'); + }, archives: function(){ - return this.hasMany('Archives') + return this.hasMany('Archives'); } }); diff --git a/models/user.js b/models/user.js new file mode 100644 index 0000000..e9efbda --- /dev/null +++ b/models/user.js @@ -0,0 +1,13 @@ +var bookshelf = require('../bookshelf'); +bookshelf.plugin('registry'); + +var Articles = require('./article.js'); + +var User = bookshelf.Model.extend({ + tableName: 'users', + articles: function(){ + return this.hasMany('Articles'); + } +}); + +module.exports = bookshelf.model('User',User);