dimanche 1 mars 2015

REST api redundancy

So i am creating a RESTFul api for my application using Node.js


However it is starting to feel abit redundant and therefore i am wondering if i am doing it correctly.


So when ever i have a new model i do the following:


First i create a file equal to the table name in the database (lets take a table called team as an example):



// IMPORT ROUTES
// =============================================================================
module.exports = function (express, sequelize, router) {
router.route('/team');
var DataTypes = require("sequelize");
var crypto = require('crypto');


// on routes that end in /Teams
// ----------------------------------------------------
router.route('/api/team')
// create a Team (accessed at POST http://localhost:8080/api/Teams)
.post(function (req, res) {

var name = req.body.name; //bodyParser does the magic
var academy_id = req.body.academy_id;

var team = Team.build({name: name, academy_id: academy_id});

team.add(function (success) {
res.json({message: 'Team created!'});
},
function (err) {
res.status(err).send(err);
});
})

// get all the Teams (accessed at GET http://localhost:8080/api/Teams)
.get(function (req, res) {
var team = Team.build();

team.retrieveAll(function (Teams) {
if (Teams) {
res.json(Teams);
} else {
res.status(401).send("Team not found");
}
}, function (error) {
res.status("Team not found").send('Team not found');
});
});


var Team = sequelize.define('team', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
academy_id: DataTypes.INTEGER

}, {
freezeTableName: true,
instanceMethods: {
retrieveAll: function (onSuccess, onError) {
Team.findAll({}, {raw: true})
.ok(onSuccess).error(onError);
},
retrieveById: function (Team_id, onSuccess, onError) {
Team.find({where: {id: Team_id}}, {raw: true})
.success(onSuccess).error(onError);
},
add: function (onSuccess, onError) {
var Teamname = this.name;
var password = this.password;

var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');

Team.build({name: Teamname, name: password})
.save().ok(onSuccess).error(onError);
},
updateById: function (Team_id, onSuccess, onError) {
var id = Team_id;
var Teamname = this.Teamname;
var password = this.password;

var shasum = crypto.createHash('sha1');
shasum.update(password);
password = shasum.digest('hex');

Team.update({Teamname: Teamname, password: password}, {where: {id: id}})
.success(onSuccess).error(onError);
},
removeById: function (Team_id, onSuccess, onError) {
Team.destroy({where: {id: Team_id}}).success(onSuccess).error(onError);
},
retrieveByAcademyId: function(academy_id, onSuccess, onError)
{
Team.findAll({where: {academy_id: academy_id}}, {raw: true})
.ok(onSuccess).error(onError);
}
}
}
),
academy = sequelize.define('academy', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
organization_id: DataTypes.INTEGER,
status_id: DataTypes.INTEGER
});
Team.belongsTo(academy,{foreignKey: 'academy_id'});


// on routes that end in /Teams/:Team_id
// ----------------------------------------------------
router.route('/api/team/:team_id')

// update a Team (accessed at PUT http://localhost:8080/api/Teams/:Team_id)
.put(function (req, res) {
var team = Team.build();
team.name = req.body.name;
team.academy_id = req.body.academy_id;
team.updateById(req.params.id, function (success) {
console.log(success);
if (success) {
res.json({message: 'Team updated!'});
} else {
res.send(401, "Team not found");
}
}, function (error) {
res.send("Team not found");
});
})

// get a Team by id(accessed at GET http://localhost:8080/api/Teams/:Team_id)
.get(function (req, res) {
var team = Team.build();

team.retrieveById(req.params.team_id, function (teams) {
if (teams) {
res.json(teams);
} else {
res.status(401).send("Team not found");
}
}, function (error) {
res.send("Team not found");
});
})

// delete a Team by id (accessed at DELETE http://localhost:8080/api/Teams/:Team_id)
.delete(function (req, res) {
var team = Team.build();

team.removeById(req.params.id, function (teams) {
if (teams) {
res.json({message: 'Team removed!'});
} else {
res.status(401).send("Team not found");
}
}, function (error) {
res.send("Team not found");
});
});

router.route('/api/academyTeam/:academy_id')
.get(function (req, res) {
var team = Team.build();

team.retrieveByAcademyId(req.params.academy_id, function (teams) {
if (teams) {
res.json(teams);
} else {
res.status(401).send("Team not found");
}
}, function (error) {
res.send("Team not found");
});
});

return router;

};


Then afterwards i add the following line to my server.js:



app.use(team_model);


then i repeat the action for the next table.


Am i doing it right or is there a way to optimize things?


Aucun commentaire:

Enregistrer un commentaire