Skip to content
This repository has been archived by the owner on Jul 9, 2019. It is now read-only.

Commit

Permalink
Use Promises instead of legacy sequelize functions.
Browse files Browse the repository at this point in the history
complete() can be replaced by then() and catch(). This is also required
for sequelize >= 2.1.0 (where complete() was dropped).
Probably fixes #3.
  • Loading branch information
xdarklight committed May 9, 2015
1 parent 11988c8 commit 4108180
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions cm-update-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ models.sequelize.sync().then(function() {
}

server.get('/changelog/:romId', function (req, res, next) {
models.Rom.find(req.params.romId).complete(function(err, rom) {
if (err) {
res.send(500);
return next();
}

models.Rom.find(req.params.romId).then(function(rom) {
if (!rom) {
res.send(404);
return next();
Expand All @@ -61,12 +56,10 @@ models.sequelize.sync().then(function() {
}

var findParentRomHandler = function(childRom, resultHandler) {
childRom.getParentRom().complete(function(err, parentRom) {
if (err) {
resultHandler(null);
} else {
resultHandler(parentRom);
}
childRom.getParentRom().then(function(parentRom) {
resultHandler(parentRom);
}).catch(function(err) {
resultHandler(null);
});
}

Expand All @@ -80,6 +73,9 @@ models.sequelize.sync().then(function() {

return next();
});
}).catch(function(err) {
res.send(500);
return next();
});
});

Expand All @@ -93,10 +89,8 @@ models.sequelize.sync().then(function() {
where: {
id: req.params.romId
}
}).complete(function(err, rom) {
if (err) {
res.send(500);
} else if (!rom) {
}).then(function(rom) {
if (!rom) {
res.send(404);
} else if (!rom.isActive) {
res.send(410);
Expand All @@ -109,11 +103,14 @@ models.sequelize.sync().then(function() {
models.Download.build({
RomId: rom.id,
userAgent: req.headers['user-agent'],
}).save().error(function (err) {
}).save().catch(function (err) {
// Ignoring errors here since those have no impact for the user.
});
}

return next();
}).catch(function(err) {
res.send(500);
return next();
});
});
Expand All @@ -128,10 +125,8 @@ models.sequelize.sync().then(function() {
where: {
id: req.params.incrementalId
}
}).complete(function(err, incremental) {
if (err) {
res.send(500);
} else if (!incremental) {
}).then(function(incremental) {
if (!incremental) {
res.send(404);
} else if (!incremental.isActive) {
res.send(410);
Expand All @@ -144,11 +139,14 @@ models.sequelize.sync().then(function() {
models.Download.build({
IncrementalId: incremental.id,
userAgent: req.headers['user-agent'],
}).save().error(function (err) {
}).save().catch(function (err) {
// Ignoring errors here since those have no impact for the user.
});
}

return next();
}).catch(function(err) {
res.send(500);
return next();
});
});
Expand Down Expand Up @@ -180,15 +178,16 @@ models.sequelize.sync().then(function() {
{
model: models.RomVariant,
},
]}).complete(function(err, incremental) {
if (err) {
res.send(500, ResultConverter.convertIncrementalErrors('Database error.'));
} else if (!incremental || !incremental.isActive) {
]}).then(function(incremental) {
if (!incremental || !incremental.isActive) {
res.send(200, ResultConverter.convertIncrementalErrors("No matching incremental update found!"));
} else {
res.send(200, ResultConverter.convertIncremental(incremental));
}

return next();
}).catch(function(err) {
res.send(500, ResultConverter.convertIncrementalErrors('Database error.'));
return next();
});
});
Expand Down Expand Up @@ -223,13 +222,11 @@ models.sequelize.sync().then(function() {
updateChannel: requestParameters.params.channels,
isActive: true,
},
}).complete(function(err, roms) {
if (err) {
res.send(500, ResultConverter.convertRomListError(responseId, 'Database error.'));
} else {
res.send(200, ResultConverter.convertRomList(responseId, roms));
}

}).then(function(roms) {
res.send(200, ResultConverter.convertRomList(responseId, roms));
return next();
}).catch(function(err) {
res.send(500, ResultConverter.convertRomListError(responseId, 'Database error.'));
return next();
});
});
Expand Down

0 comments on commit 4108180

Please sign in to comment.