REST api冗余

时间:2015-03-01 17:00:00

标签: node.js api rest

所以我使用RESTFul api

为我的应用创建Node.js

然而,它开始感到abit多余,因此我想知道我是否正确地做了。

所以当我有一个新模型时,我会做以下事情:

首先我创建一个等于数据库中表名的文件(以一个名为team的表为例):

// 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;

};

然后我将以下行添加到server.js

app.use(team_model);

然后我重复下一张桌子的动作。

我是做得对还是有办法优化事物?

0 个答案:

没有答案
相关问题