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 c7340f8..5a8137c 100644 Binary files a/db/matterwiki.sqlite and b/db/matterwiki.sqlite differ 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);