-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tasks): new tasks module to use sequelizejs (#1693)
[v2] MEAN.JS API - feat(tasks): new tasks module to use sequelizejs
- Loading branch information
Showing
4 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
server/modules/tasks/server/controllers/tasks.server.controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
}; |