mirror of
https://github.com/wassname/HackFlowy.git
synced 2026-07-19 11:19:50 +08:00
Initial Sequelize integration.
Added orm module to abstract Sequelize usage to some degree. ORM module handles the configuration and instantiation of Sequelize. Models are defined and instantiated from models/* Tested only against SQLite. Faily new to nodejs so I probably messed up with something.
This commit is contained in:
@@ -3,18 +3,11 @@ var application_root = __dirname,
|
||||
app = express(),
|
||||
path = require('path'),
|
||||
config = require('./config'),
|
||||
mysql = require('mysql'),
|
||||
orm = require('./orm').configure(config.db),
|
||||
Tasks = require('./models/task').instance(orm),
|
||||
server = require('http').createServer(app),
|
||||
io = require('socket.io').listen(server);
|
||||
|
||||
var client = mysql.createConnection({
|
||||
host: 'localhost',
|
||||
user: config.db_user,
|
||||
password: config.db_password
|
||||
});
|
||||
|
||||
client.query('USE '+config.db_name);
|
||||
|
||||
app.configure(function() {
|
||||
app.use(express.bodyParser());
|
||||
app.use(express.methodOverride());
|
||||
@@ -29,32 +22,37 @@ server.listen(port, function() {
|
||||
});
|
||||
|
||||
app.get('/tasks', function(req,res){
|
||||
client.query("SELECT * FROM tasks", function select(err,tasks){
|
||||
Tasks.all().success(function(tasks){
|
||||
res.send(tasks);
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/tasks', function(req,res){
|
||||
var timestamp = Math.round((new Date()).getTime()/1000);
|
||||
client.query("INSERT INTO tasks (content, timestamp, parent_id) VALUES (?,?,?)", [req.body.content,timestamp,req.body.parent_id]);
|
||||
client.query("SELECT * FROM tasks WHERE content = ?", [req.body.content], function select(err,task){
|
||||
req.body.id = task[0].id;
|
||||
res.send(req.body);
|
||||
Tasks.create({
|
||||
content: req.body.content,
|
||||
parent: req.body.parent_id,
|
||||
is_completed: false,
|
||||
}).success(function(task){
|
||||
res.send(task);
|
||||
});
|
||||
});
|
||||
|
||||
app.put('/tasks/:id', function(req,res){
|
||||
console.log(req.body.is_completed);
|
||||
var timestamp = Math.round((new Date()).getTime()/1000);
|
||||
client.query("UPDATE tasks SET content = ?, timestamp = ?, is_completed = ? WHERE id = ?", [req.body.content, timestamp, req.body.is_completed, req.body.id], function(err, task){
|
||||
req.body.timestamp = timestamp;
|
||||
res.send(req.body);
|
||||
Tasks.find(req.params.id).success(function(task){
|
||||
task.content = req.body.content;
|
||||
task.is_completed = req.body.is_completed;
|
||||
task.save().success(function(task){
|
||||
res.send(task);
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
app.delete('/tasks/:id', function(req,res){
|
||||
client.query("DELETE FROM tasks WHERE id = ?", [req.params.id], function(err, task){
|
||||
res.send('');
|
||||
Tasks.find(req.params.id).success(function(task){
|
||||
task.destroy().success(function(){
|
||||
res.send('');
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user