Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/core/Chain/chain.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
var Chain = require('../../models/Chain.js');
var mongoose = require('mongoose');

module.exports = (app)=>{

'use strict';

var getAll = function(req, res, next){
Chain.find().exec().then(function(chain){
res.status(201).send({'chains' : chain});
return next();
}).catch(function(err){
res.status(500).send('Internal Error Server');
return next();
});
}

var addChain = function(req, res, next){
if (!req.body.chain) return res.status(404).send('Missing property chain!');
if (!req.body.chain.name) return res.status(404).send('Missing property name!');
let newChain = new Chain(req.body.chain);
newChain.userId = req.user._id;
newChain.save().then(function(entity){
res.status(201).send(entity);
return next();
}).catch( function(err){
return res.status(500).send('Missing parameters');
});
}

var remove = function (req, res, next){
if(!req.body.chain) return res.status(404).send('Missing property chain!');
if(!req.body.chain._id) return res.status(404).send('Missing property _id');
Chain.findByIdAndRemove({'_id':req.body.chain._id}).exec().then(function (){
res.status(200).send("Chain remove");
return next();
}).catch( function(err){
res.status(500).send("Fail delete chain")
return next();
})
}

var update = function(req, res, next){
if(!req.body.chain) return res.status(404).send('Missing property chain!');
if(!req.body.chain._id) return res.status(404).send('Missing property _id');
Chain.findByIdAndUpdate({'_id':req.body.chain._id}, req.body.chain, {upsert: true}).exec().then(function(chain){
res.status(201).send('Chain updated');
return next();
}).catch(function(err){
res.status(500).send('Fail Update');
return next();
})
}

var getSingleChain = function(req, res, next){
if(!req.body.chain) return res.status(404).send('Missing property chain!');
if(!req.body.chain._id) return res.status(404).send('Missing property _id');
if(!req.body.chain._id) return res.status(404).send('Missing property _id');
Chain.findById(req.body.chain._id).exec().then(function(chain){
res.status(201).send({'chain' : chain});
return next();
}).catch(function(err){
res.status(500).send('Fail Get');
return next();
})
}

return{
getAll : getAll,
addChain : addChain,
remove : remove,
update : update,
getSingleChain : getSingleChain
}
}

//curl -X GET -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain -v
//curl -X POST -H "Content-Type: application/json" -d '{"chain" : {"name":"Brian"}}' -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain -v
//curl -X GET -H "Content-Type: application/json" -d '{"chain" : {"_id":"578a9027fe5e7e10148b0a74"}}' -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain/577690c42a1afed8366c4905 -v
//curl -X PUT -H "Content-Type: application/json" -d '{"chain" : {"_id":"578a9027fe5e7e10148b0a74", "name":"miaolo"}}' -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain/577690c42a1afed8366c4905 -v
//curl -X DELETE -H "Content-Type: application/json" -d '{"chain" : {"_id":"578a9027fe5e7e10148b0a74"}}' -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain/577690c42a1afed8366c4905 -v
4 changes: 2 additions & 2 deletions src/models/Block.js → src/models/Chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var Block = new Schema({
var Chain = new Schema({
userId: {
type: Schema.ObjectId,
required: true,
Expand All @@ -18,4 +18,4 @@ var Block = new Schema({
}
});

module.exports = mongoose.model('Block', Block);
module.exports = mongoose.model('Chain', Chain);
32 changes: 0 additions & 32 deletions src/routing/block.js

This file was deleted.

24 changes: 24 additions & 0 deletions src/routing/chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

var passport = require('passport');
var auth = require('../auth/auth');
var Chain = require('../core/Chain/chain.controller.js')();
module.exports = (app) => {
// CHAIN CRUD ALL
app.route('/v1/chain')
.all( auth.bearer() )

.get( Chain.getAll )

.post( Chain.addChain );

// CHAIN CRUD SINGLE
app.route('/v1/chain/:id')
.all( auth.bearer() )

.get( Chain.getSingleChain )

.put( Chain.update)

.delete( Chain.remove )
}
4 changes: 2 additions & 2 deletions src/routing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var passport = require('passport');
var pkg = require('../../package.json');
var auth = require('../auth/auth');

let block = require('./block');
let chain = require('./chain');

module.exports = (app) => {
app.get('/', (req, res) => {
Expand All @@ -24,5 +24,5 @@ module.exports = (app) => {
});
});

block(app);
chain(app);
}
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var Server = {

app.use(compress());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json({type: 'application/*+json'}));
app.use(bodyParser.json());

app.use(passport.initialize())

Expand Down
Loading