From 3f9a872c90fafa16f49783f176a8b9dcfaad6c01 Mon Sep 17 00:00:00 2001 From: Liran Tal Date: Mon, 26 Dec 2016 20:55:30 +0200 Subject: [PATCH] feat(tasks): new tasks module to use sequelizejs (#1693) [v2] MEAN.JS API - feat(tasks): new tasks module to use sequelizejs --- .../controllers/tasks.server.controller.js | 98 +++++++++++++++++++ .../sequelize/task.server.model.sequelize.js | 0 .../sequelize/user.server.model.sequelize.js | 5 +- .../server/routes/tasks.server.routes.js | 14 +++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 server/modules/tasks/server/controllers/tasks.server.controller.js rename server/modules/{users => tasks}/server/models/sequelize/task.server.model.sequelize.js (100%) rename server/modules/{users => tasks}/server/models/sequelize/user.server.model.sequelize.js (76%) create mode 100644 server/modules/tasks/server/routes/tasks.server.routes.js diff --git a/server/modules/tasks/server/controllers/tasks.server.controller.js b/server/modules/tasks/server/controllers/tasks.server.controller.js new file mode 100644 index 000000000..f06d16ecb --- /dev/null +++ b/server/modules/tasks/server/controllers/tasks.server.controller.js @@ -0,0 +1,98 @@ +'use strict'; + +/** + * Module dependencies + */ +var path = require('path'), + orm = require(path.resolve('./config/lib/sequelize')); + + +exports.getAllTasks = function (req, res) { + + orm.Task.findAll().then(function (tasks) { + res.status(200).send(tasks); + }).catch(function (error) { + res.status(500).send(error); + }); + +}; + +exports.addTask = function (req, res) { + + // Reject the request if no title field is provided + if (!req.body.title) { + return res.status(400).send({ + message: 'Missing title field' + }); + } + + // Coerce the title field to string + let title = '' + req.body.title; + + let username = req.session.user.username; + + orm.Task.create({ + title: title, + UserUsername: username + }).then(function (tasks) { + res.status(200).send(tasks); + }).catch(function (error) { + res.status(500).send(error); + }); + +}; + +exports.updateTask = function (req, res) { + + orm.Task.update(req.body, { + where: { + id: req.body.id + } + }).then(function (tasks) { + res.status(200).send(tasks); + }).catch(function (error) { + res.status(500).send(error); + }); + +}; + +exports.deleteTask = function (req, res) { + + orm.Task.destroy({ + where: { + id: req.body.id + } + }).then(function (tasks) { + res.status(200).send(tasks); + }).catch(function (error) { + res.status(500).send(error); + }); + +}; + +exports.getMyTasks = function (req, res) { + + orm.Task.findAll({ + where: { + UserUsername: req.session.user.username + } + }).then(function (tasks) { + res.status(200).send(tasks); + }).catch(function (error) { + res.status(500).send(error); + }); + +}; + + +// Helper method to validate a valid session for dependent APIs +exports.validateSessionUser = function (req, res, next) { + // Reject the request if no user exists on the session + if (!res.session || !req.session.user) { + return res.status(400).send({ + message: 'No session user' + }); + } + + return next(); +}; diff --git a/server/modules/users/server/models/sequelize/task.server.model.sequelize.js b/server/modules/tasks/server/models/sequelize/task.server.model.sequelize.js similarity index 100% rename from server/modules/users/server/models/sequelize/task.server.model.sequelize.js rename to server/modules/tasks/server/models/sequelize/task.server.model.sequelize.js diff --git a/server/modules/users/server/models/sequelize/user.server.model.sequelize.js b/server/modules/tasks/server/models/sequelize/user.server.model.sequelize.js similarity index 76% rename from server/modules/users/server/models/sequelize/user.server.model.sequelize.js rename to server/modules/tasks/server/models/sequelize/user.server.model.sequelize.js index 700cc7ba4..f64b4f2ba 100644 --- a/server/modules/users/server/models/sequelize/user.server.model.sequelize.js +++ b/server/modules/tasks/server/models/sequelize/user.server.model.sequelize.js @@ -2,7 +2,10 @@ module.exports = function(sequelize, DataTypes) { var User = sequelize.define('User', { - username: DataTypes.STRING + username: { + type: DataTypes.STRING, + primaryKey: true + } }, { classMethods: { associate: function(models) { diff --git a/server/modules/tasks/server/routes/tasks.server.routes.js b/server/modules/tasks/server/routes/tasks.server.routes.js new file mode 100644 index 000000000..15c53e96e --- /dev/null +++ b/server/modules/tasks/server/routes/tasks.server.routes.js @@ -0,0 +1,14 @@ +'use strict'; + +module.exports = function (app) { + // Tasks controller + var tasks = require('../controllers/tasks.server.controller'); + + // Setting up the models APIs profile api + app.route('/api/tasks').get(tasks.getAllTasks); + app.route('/api/tasks/me').get(tasks.validateSessionUser, tasks.getMyTasks); + app.route('/api/tasks/me').put(tasks.validateSessionUser, tasks.deleteTask); + app.route('/api/tasks/me').delete(tasks.validateSessionUser, tasks.updateTask); + app.route('/api/tasks').post(tasks.validateSessionUser, tasks.addTask); + +};