Created users. GET all users and create user (POST) endpoint
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.
@ -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');
|
||||
})
|
||||
};
|
@ -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…
Reference in New Issue