code improvements
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
var config = {};
|
var config = {};
|
||||||
|
|
||||||
config.dbURL = 'mongodb://localhost:27017/tellall';
|
config.DB_URL = 'mongodb://localhost:27017/tellall';
|
||||||
config.PORT = 5000;
|
config.PORT = 5000;
|
||||||
|
|
||||||
config.TOKEN = 'Atza|IwEBIBe6gDqrrowEEav6N-_6s4NztYeP3oG8PGWmu8ZiZw6lbOh3wNla3TK6pY-VEpT1d8an-dVf_n3kXJzVFsNo_4xBfZyFHGoCTDTFjs3yBRul4PVdBOhwwiH3-sgRLcUofZbe2oE06GmTcbfYtaStfXpQI5dfpldfnsJg_CvhSA6AHb_snJT3F6lyXzbV076d_3cYUMJxFldJGnYcviNHHxjjmuQTD06hhGzCbAxxe9eBmkuopRsNfyedLT2UlKP_ublah9CUGA3AdIX_3Iuke82jMwGnNl9gv7pbaDNEjAbj7IQSl3B08uuREtJq-oTBOjALNXRvFxTJmQjZwXNf9eHC7fSHJDdEPdZQU0AcffRQObAyAkUuL6Jv39OHzhb3Q64-zzoyODqnJyLP5SQZ2JVF53Kc_cTBqjIc9pXljqe7yEVk6JDs7q1zKbBibx_AQm57TO79IzWyLBzBMlYL5HdTsqEfRzLeDw2tws-hGMgkx2HWfdbYnmf5Qb4SyIhzvmmdfPLg3MVKTxjIBu1rx0xf3n0PLZP1EO6jsJPoMRPg77Gm4oit5Zp6s37ek3A3Vxh-ntoASpkrkxGTG9kVtRNt';
|
config.TOKEN = 'Atza|IwEBIBe6gDqrrowEEav6N-_6s4NztYeP3oG8PGWmu8ZiZw6lbOh3wNla3TK6pY-VEpT1d8an-dVf_n3kXJzVFsNo_4xBfZyFHGoCTDTFjs3yBRul4PVdBOhwwiH3-sgRLcUofZbe2oE06GmTcbfYtaStfXpQI5dfpldfnsJg_CvhSA6AHb_snJT3F6lyXzbV076d_3cYUMJxFldJGnYcviNHHxjjmuQTD06hhGzCbAxxe9eBmkuopRsNfyedLT2UlKP_ublah9CUGA3AdIX_3Iuke82jMwGnNl9gv7pbaDNEjAbj7IQSl3B08uuREtJq-oTBOjALNXRvFxTJmQjZwXNf9eHC7fSHJDdEPdZQU0AcffRQObAyAkUuL6Jv39OHzhb3Q64-zzoyODqnJyLP5SQZ2JVF53Kc_cTBqjIc9pXljqe7yEVk6JDs7q1zKbBibx_AQm57TO79IzWyLBzBMlYL5HdTsqEfRzLeDw2tws-hGMgkx2HWfdbYnmf5Qb4SyIhzvmmdfPLg3MVKTxjIBu1rx0xf3n0PLZP1EO6jsJPoMRPg77Gm4oit5Zp6s37ek3A3Vxh-ntoASpkrkxGTG9kVtRNt';
|
||||||
|
|||||||
@@ -1,26 +1,30 @@
|
|||||||
const constants = {};
|
const constants = {};
|
||||||
|
|
||||||
constants.amazonResultCodes = {
|
constants.amazonResultCodes = {
|
||||||
ok:200,
|
OK:200,
|
||||||
accepted:202,
|
ACCEPTED:202,
|
||||||
badRequest:400,
|
BAD_REQUEST:400,
|
||||||
unauthorized:401,
|
UNAUTHORIZED:401,
|
||||||
notFound:404,
|
NOT_FOUND:404,
|
||||||
conflict:409,
|
CONFLICT:409,
|
||||||
payloadTooLarge:413
|
PAYLOAD_TOO_LARGE:413
|
||||||
}
|
}
|
||||||
|
|
||||||
constants.apiResultCodes = {
|
constants.apiResultCodes = {
|
||||||
genericError : -1,
|
GENERIC_ERROR : -1,
|
||||||
ok:0,
|
OK:0,
|
||||||
amazonError:1, //amazon api works, but error is some of the amazonResultCodes
|
AMAZON_ERROR:1, //amazon api works, but error is some of the amazonResultCodes
|
||||||
amazonFail:2, //amazon api doesn't work
|
AMAZON_FAIL:2, //amazon api doesn't work
|
||||||
databaseError:3,
|
DATABASE_ERROR:3,
|
||||||
noSkill:4,
|
NO_SKILL:4,
|
||||||
inconsistentState:5,
|
INCONSISTEN_STATE:5,
|
||||||
}
|
}
|
||||||
|
|
||||||
constants.skillIDLength = 24;
|
constants.HTTPResultCodes = {
|
||||||
|
INTERNAL_SERVER_ERROR : 500,
|
||||||
|
}
|
||||||
|
|
||||||
|
constants.SKILL_ID_LENGTH = 24;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ var alexa = require ('../components/alexa');
|
|||||||
router.get ('/:id', async (req, res, next) => {
|
router.get ('/:id', async (req, res, next) => {
|
||||||
const id = req.params.id;
|
const id = req.params.id;
|
||||||
|
|
||||||
if (id.length !== constants.skillIDLength) {
|
if (id.length !== constants.SKILL_ID_LENGTH) {
|
||||||
res.json ([]);
|
res.json ([]);
|
||||||
} else {
|
} else {
|
||||||
databaseHelper
|
databaseHelper
|
||||||
@@ -31,78 +31,66 @@ router.put ('/:id', bodyParser.json (), async (req, res, next) => {
|
|||||||
delete skill.updateOnAmazon;
|
delete skill.updateOnAmazon;
|
||||||
delete skill._id;
|
delete skill._id;
|
||||||
|
|
||||||
console.log ('id = ' + id);
|
//First get current skill from DB
|
||||||
|
databaseHelper
|
||||||
if (updateOnAmazon) {
|
.getSkill (id)
|
||||||
//First get current skill from DB
|
.then (currentSkillState => {
|
||||||
databaseHelper
|
//Now let's update skill in DB
|
||||||
.getSkill (id)
|
databaseHelper
|
||||||
.then (skillInDB => {
|
.updateSkill (id, skill)
|
||||||
//Now let's update skill in DB
|
.then (() => {
|
||||||
databaseHelper
|
//Ok, done, now update skill on Amazon (if needed)
|
||||||
.updateSkill (id, skill)
|
if (updateOnAmazon) {
|
||||||
.then (() => {
|
|
||||||
//Ok, done, now update skill on Amazon
|
|
||||||
amazonHelper
|
amazonHelper
|
||||||
.updateSkill (skill)
|
.updateSkill (skill)
|
||||||
.then (amazonResult => {
|
.then (amazonResult => {
|
||||||
if (
|
if (
|
||||||
amazonResult === constants.amazonResultCodes.ok ||
|
amazonResult === constants.amazonResultCodes.OK ||
|
||||||
amazonResult === constants.amazonResultCodes.accepted
|
amazonResult === constants.amazonResultCodes.ACCEPTED
|
||||||
) {
|
) {
|
||||||
res.json ({result: constants.apiResultCodes.ok, message: ''});
|
res.json ({result: constants.apiResultCodes.OK, message: ''});
|
||||||
alexa.updateIntentsJSON ();
|
alexa.updateIntentsJSON ();
|
||||||
} else {
|
} else {
|
||||||
res.json ({
|
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
|
||||||
result: constants.apiResultCodes.amazonError,
|
result: constants.apiResultCodes.AMAZON_ERROR,
|
||||||
message: amazonResult,
|
message: amazonResult,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch (e => {
|
.catch (e => {
|
||||||
res.json ({
|
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
|
||||||
result: constants.apiResultCodes.amazonFail,
|
result: constants.apiResultCodes.AMAZON_FAIL,
|
||||||
message: e,
|
message: e,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})
|
}else{
|
||||||
.catch (() => {
|
res.json ({result: constants.apiResultCodes.OK, message: ''});
|
||||||
//Update in database didn't go well, revert changes
|
alexa.updateIntentsJSON ();
|
||||||
databaseHelper
|
}
|
||||||
.updateSkill (id, skillInDB)
|
})
|
||||||
.then (() => {
|
.catch (() => {
|
||||||
res.json ({
|
//Update in database didn't go well, revert changes
|
||||||
result: constants.apiResultCodes.databaseError,
|
databaseHelper
|
||||||
message: '',
|
.updateSkill (id, currentSkillState)
|
||||||
});
|
.then (() => {
|
||||||
})
|
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
|
||||||
.catch (() => {
|
result: constants.apiResultCodes.DATABASE_ERROR,
|
||||||
//This should never happen, something is seriously wrong, like no database connection
|
message: '',
|
||||||
res.json ({
|
|
||||||
result: constants.apiResultCodes.inconsistentState,
|
|
||||||
message: '',
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
})
|
||||||
})
|
.catch (() => {
|
||||||
.catch (e => {
|
//This should never happen, something is seriously wrong, like no database connection
|
||||||
//I don't know why, but something went wrong, possibly ID of skill is wrong, doesn't exist in DB
|
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({
|
||||||
res.json ({result: constants.apiResultCodes.noSkill, message: ''});
|
result: constants.apiResultCodes.INCONSISTEN_STATE,
|
||||||
});
|
message: '',
|
||||||
} else {
|
});
|
||||||
databaseHelper
|
});
|
||||||
.updateSkill (id, skill)
|
|
||||||
.then (result => {
|
|
||||||
res.json ({result: constants.apiResultCodes.ok, message: ''});
|
|
||||||
alexa.updateIntentsJSON ();
|
|
||||||
})
|
|
||||||
.catch (e => {
|
|
||||||
res.json ({
|
|
||||||
result: constants.apiResultCodes.databaseError,
|
|
||||||
message: '',
|
|
||||||
});
|
});
|
||||||
});
|
})
|
||||||
}
|
.catch (e => {
|
||||||
|
//I don't know why, but something went wrong, possibly ID of skill is wrong, doesn't exist in DB
|
||||||
|
res.status(constants.HTTPResultCodes.INTERNAL_SERVER_ERROR).json ({result: constants.apiResultCodes.NO_SKILL, message: ''});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ app.set ('view engine', 'ejs');
|
|||||||
app.use (require ('./middleware')); //common middleware for all requests
|
app.use (require ('./middleware')); //common middleware for all requests
|
||||||
app.use (require ('./controllers')); //all routes
|
app.use (require ('./controllers')); //all routes
|
||||||
|
|
||||||
MongoClient.connect (config.dbURL)
|
MongoClient.connect (config.DB_URL)
|
||||||
.then (database => {
|
.then (database => {
|
||||||
databaseHelper.initModule (database);
|
databaseHelper.initModule (database);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user