From 26b9ae22724862fac562052d254e0a2f42fadca5 Mon Sep 17 00:00:00 2001 From: vantezzen Date: Mon, 26 Aug 2019 21:26:59 +0200 Subject: Improve recipe creation --- .env.example | 2 + app/Controllers/Http/RecipeController.js | 12 ++++- app/Exceptions/Handler.js | 45 +++++++++++++++++ package-lock.json | 87 ++++++++++++++++++++++++++++++++ public/new.html | 2 +- 5 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 app/Exceptions/Handler.js diff --git a/.env.example b/.env.example index de9f030..91e92a1 100644 --- a/.env.example +++ b/.env.example @@ -17,3 +17,5 @@ DB_PASSWORD= DB_DATABASE=adonis HASH_DRIVER=bcrypt + +IS_CREATION_ENABLED=true \ No newline at end of file diff --git a/app/Controllers/Http/RecipeController.js b/app/Controllers/Http/RecipeController.js index 11938b6..ec0cde9 100644 --- a/app/Controllers/Http/RecipeController.js +++ b/app/Controllers/Http/RecipeController.js @@ -6,6 +6,7 @@ const Drive = use('Drive') const { validateAll } = use('Validator'); +const Env = use('Env') const fetch = require('node-fetch'); const targz = require('targz'); @@ -53,18 +54,23 @@ class RecipeController { request, response }) { + // Check if recipe creation is enabled + if (Env.get('IS_CREATION_ENABLED') == 'false') { + return response.send('This server doesn\'t allow the creation of new recipes.'); + } + // Validate user input const validation = await validateAll(request.all(), { name: 'required|alpha', - recipeId: 'required|unique:recipes,recipeId', + id: 'required|unique:recipes,recipeId', author: 'required|accepted', png: 'required|url', svg: 'required|url', - files: 'required', }); if (validation.fails()) { return response.status(401).send({ "message": "Invalid POST arguments", + "messages": validation.messages(), "status": 401 }) } @@ -126,6 +132,7 @@ class RecipeController { if (validation.fails()) { return response.status(401).send({ "message": "Please provide a needle", + "messages": validation.messages(), "status": 401 }) } @@ -162,6 +169,7 @@ class RecipeController { if (validation.fails()) { return response.status(401).send({ "message": "Please provide a recipe ID", + "messages": validation.messages(), "status": 401 }) } diff --git a/app/Exceptions/Handler.js b/app/Exceptions/Handler.js new file mode 100644 index 0000000..94d7246 --- /dev/null +++ b/app/Exceptions/Handler.js @@ -0,0 +1,45 @@ +'use strict' + +const BaseExceptionHandler = use('BaseExceptionHandler') + +/** + * This class handles all exceptions thrown during + * the HTTP request lifecycle. + * + * @class ExceptionHandler + */ +class ExceptionHandler extends BaseExceptionHandler { + /** + * Handle exception thrown during the HTTP lifecycle + * + * @method handle + * + * @param {Object} error + * @param {Object} options.request + * @param {Object} options.response + * + * @return {void} + */ + async handle (error, { request, response }) { + if (error.name === 'ValidationException') { + return response.status(400).send('Invalid arguments') + } + + response.status(error.status).send(error.message) + } + + /** + * Report exception for logging or debugging. + * + * @method report + * + * @param {Object} error + * @param {Object} options.request + * + * @return {void} + */ + async report (error, { request }) { + } +} + +module.exports = ExceptionHandler diff --git a/package-lock.json b/package-lock.json index 9b70b2d..b6ccb17 100644 --- a/package-lock.json +++ b/package-lock.json @@ -796,6 +796,15 @@ "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", "integrity": "sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms=" }, + "bl": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/bl/-/bl-1.2.2.tgz", + "integrity": "sha512-e8tQYnZodmebYDWGH7KMRvtzKXaJHx3BbilrgZCfvyLUYdKpK1t5PSPmpkny/SgiTSCnjfLW7v5rlONXVFkQEA==", + "requires": { + "readable-stream": "^2.3.5", + "safe-buffer": "^5.1.1" + } + }, "bluebird": { "version": "3.5.5", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.5.tgz", @@ -827,11 +836,30 @@ "to-regex": "^3.0.1" } }, + "buffer-alloc": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/buffer-alloc/-/buffer-alloc-1.2.0.tgz", + "integrity": "sha512-CFsHQgjtW1UChdXgbyJGtnm+O/uLQeZdtbDo8mfUgYXCHSM1wgrVxXm6bSyrUuErEb+4sYVGCzASBRot7zyrow==", + "requires": { + "buffer-alloc-unsafe": "^1.1.0", + "buffer-fill": "^1.0.0" + } + }, + "buffer-alloc-unsafe": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/buffer-alloc-unsafe/-/buffer-alloc-unsafe-1.1.0.tgz", + "integrity": "sha512-TEM2iMIEQdJ2yjPJoSIsldnleVaAk1oW3DBVUykyOLsEsFmEc9kn+SFFPz+gl54KQNxlDnAwCXosOS9Okx2xAg==" + }, "buffer-equal-constant-time": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=" }, + "buffer-fill": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/buffer-fill/-/buffer-fill-1.0.0.tgz", + "integrity": "sha1-+PeLdniYiO858gXNY39o5wISKyw=" + }, "buffer-writer": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", @@ -1648,6 +1676,11 @@ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, + "fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" + }, "fs-extra": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz", @@ -4310,6 +4343,50 @@ "yallist": "^3.0.3" } }, + "tar-fs": { + "version": "1.16.3", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-1.16.3.tgz", + "integrity": "sha512-NvCeXpYx7OsmOh8zIOP/ebG55zZmxLE0etfWRbWok+q2Qo8x/vOR/IJT1taADXPe+jsiu9axDb3X4B+iIgNlKw==", + "requires": { + "chownr": "^1.0.1", + "mkdirp": "^0.5.1", + "pump": "^1.0.0", + "tar-stream": "^1.1.2" + }, + "dependencies": { + "pump": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/pump/-/pump-1.0.3.tgz", + "integrity": "sha512-8k0JupWme55+9tCVE+FS5ULT3K6AbgqrGa58lTT49RpyfwwcGedHqaC5LlQNdEAumn/wFsu6aPwkuPMioy8kqw==", + "requires": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + } + } + }, + "tar-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-1.6.2.tgz", + "integrity": "sha512-rzS0heiNf8Xn7/mpdSVVSMAWAoy9bfb1WOTYC78Z0UQKeKa/CWS8FOq0lKGNa8DWKAn9gxjCvMLYc5PGXYlK2A==", + "requires": { + "bl": "^1.0.0", + "buffer-alloc": "^1.2.0", + "end-of-stream": "^1.0.0", + "fs-constants": "^1.0.0", + "readable-stream": "^2.3.0", + "to-buffer": "^1.1.1", + "xtend": "^4.0.0" + } + }, + "targz": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/targz/-/targz-1.0.1.tgz", + "integrity": "sha1-j3alI2lM3t+7XWCkB2/27uzFOY8=", + "requires": { + "tar-fs": "^1.8.1" + } + }, "tarn": { "version": "1.1.5", "resolved": "https://registry.npmjs.org/tarn/-/tarn-1.1.5.tgz", @@ -4356,6 +4433,11 @@ "os-tmpdir": "~1.0.2" } }, + "to-buffer": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/to-buffer/-/to-buffer-1.1.1.tgz", + "integrity": "sha512-lx9B5iv7msuFYE3dytT+KE5tap+rNYw+K4jVkb9R/asAb+pbBSM17jtunHplhBe6RRJdZx3Pn2Jph24O32mOVg==" + }, "to-object-path": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/to-object-path/-/to-object-path-0.3.0.tgz", @@ -4601,6 +4683,11 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, + "uuid": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.3.tgz", + "integrity": "sha512-pW0No1RGHgzlpHJO1nsVrHKpOEIxkGg1xB+v0ZmdNH5OAeAwzAVrCnI2/6Mtx+Uys6iaylxa+D3g4j63IKKjSQ==" + }, "v8flags": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/v8flags/-/v8flags-3.1.3.tgz", diff --git a/public/new.html b/public/new.html index b0e4b41..c11da47 100644 --- a/public/new.html +++ b/public/new.html @@ -104,7 +104,7 @@

-

+

-- cgit v1.2.3-54-g00ecf