Skip to content

Commit

Permalink
feat(tasks): new tasks module to use sequelizejs (#1693)
Browse files Browse the repository at this point in the history
[v2] MEAN.JS API - feat(tasks): new tasks module to use sequelizejs
  • Loading branch information
lirantal committed Dec 28, 2016
1 parent 008ec75 commit 3f9a872
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
98 changes: 98 additions & 0 deletions server/modules/tasks/server/controllers/tasks.server.controller.js
Original file line number Diff line number Diff line change
@@ -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();
};
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions server/modules/tasks/server/routes/tasks.server.routes.js
Original file line number Diff line number Diff line change
@@ -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);

};

0 comments on commit 3f9a872

Please sign in to comment.