Created users. GET all users and create user (POST) endpoint

pull/1/head
Nishant Arora 8 years ago
parent 62314cd581
commit a6838337eb

@ -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}});
});
});
}

Binary file not shown.

@ -28,6 +28,9 @@ require('./api/articles')(app);
// Importing all endpoints for topics // Importing all endpoints for topics
require('./api/topics')(app); require('./api/topics')(app);
// Importing all endpoints for users
require('./api/users')(app);
app.use(express.static(__dirname + '/client')); app.use(express.static(__dirname + '/client'));
app.listen(5000 || process.env.PORT, function(){ app.listen(5000 || process.env.PORT, function(){

@ -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');
};

@ -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');
})
};

@ -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');
})
};

@ -2,14 +2,18 @@ var bookshelf = require('../bookshelf');
bookshelf.plugin('registry'); bookshelf.plugin('registry');
var Topic = require('./topic'); var Topic = require('./topic');
var Archives = require('./archive'); var Archives = require('./archive');
var User = require('./user')
var Article = bookshelf.Model.extend({ var Article = bookshelf.Model.extend({
tableName: 'articles', tableName: 'articles',
topic: function() { topic: function() {
return this.belongsTo('Topic','topic_id'); return this.belongsTo('Topic','topic_id');
}, },
user: function() {
return this.belongsTo('User','user_id');
},
archives: function(){ archives: function(){
return this.hasMany('Archives') return this.hasMany('Archives');
} }
}); });

@ -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);
Loading…
Cancel
Save