aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorLibravatar vantezzen <properly@protonmail.com>2019-08-26 21:26:59 +0200
committerLibravatar vantezzen <properly@protonmail.com>2019-08-26 21:26:59 +0200
commit26b9ae22724862fac562052d254e0a2f42fadca5 (patch)
treedc65c4b7a1ee22896de0a6c84abe277de7708790 /app
parentAdd validations (diff)
downloadferdium-server-26b9ae22724862fac562052d254e0a2f42fadca5.tar.gz
ferdium-server-26b9ae22724862fac562052d254e0a2f42fadca5.tar.zst
ferdium-server-26b9ae22724862fac562052d254e0a2f42fadca5.zip
Improve recipe creation
Diffstat (limited to 'app')
-rw-r--r--app/Controllers/Http/RecipeController.js12
-rw-r--r--app/Exceptions/Handler.js45
2 files changed, 55 insertions, 2 deletions
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')
6const { 6const {
7 validateAll 7 validateAll
8} = use('Validator'); 8} = use('Validator');
9const Env = use('Env')
9 10
10const fetch = require('node-fetch'); 11const fetch = require('node-fetch');
11const targz = require('targz'); 12const targz = require('targz');
@@ -53,18 +54,23 @@ class RecipeController {
53 request, 54 request,
54 response 55 response
55 }) { 56 }) {
57 // Check if recipe creation is enabled
58 if (Env.get('IS_CREATION_ENABLED') == 'false') {
59 return response.send('This server doesn\'t allow the creation of new recipes.');
60 }
61
56 // Validate user input 62 // Validate user input
57 const validation = await validateAll(request.all(), { 63 const validation = await validateAll(request.all(), {
58 name: 'required|alpha', 64 name: 'required|alpha',
59 recipeId: 'required|unique:recipes,recipeId', 65 id: 'required|unique:recipes,recipeId',
60 author: 'required|accepted', 66 author: 'required|accepted',
61 png: 'required|url', 67 png: 'required|url',
62 svg: 'required|url', 68 svg: 'required|url',
63 files: 'required',
64 }); 69 });
65 if (validation.fails()) { 70 if (validation.fails()) {
66 return response.status(401).send({ 71 return response.status(401).send({
67 "message": "Invalid POST arguments", 72 "message": "Invalid POST arguments",
73 "messages": validation.messages(),
68 "status": 401 74 "status": 401
69 }) 75 })
70 } 76 }
@@ -126,6 +132,7 @@ class RecipeController {
126 if (validation.fails()) { 132 if (validation.fails()) {
127 return response.status(401).send({ 133 return response.status(401).send({
128 "message": "Please provide a needle", 134 "message": "Please provide a needle",
135 "messages": validation.messages(),
129 "status": 401 136 "status": 401
130 }) 137 })
131 } 138 }
@@ -162,6 +169,7 @@ class RecipeController {
162 if (validation.fails()) { 169 if (validation.fails()) {
163 return response.status(401).send({ 170 return response.status(401).send({
164 "message": "Please provide a recipe ID", 171 "message": "Please provide a recipe ID",
172 "messages": validation.messages(),
165 "status": 401 173 "status": 401
166 }) 174 })
167 } 175 }
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 @@
1'use strict'
2
3const BaseExceptionHandler = use('BaseExceptionHandler')
4
5/**
6 * This class handles all exceptions thrown during
7 * the HTTP request lifecycle.
8 *
9 * @class ExceptionHandler
10 */
11class ExceptionHandler extends BaseExceptionHandler {
12 /**
13 * Handle exception thrown during the HTTP lifecycle
14 *
15 * @method handle
16 *
17 * @param {Object} error
18 * @param {Object} options.request
19 * @param {Object} options.response
20 *
21 * @return {void}
22 */
23 async handle (error, { request, response }) {
24 if (error.name === 'ValidationException') {
25 return response.status(400).send('Invalid arguments')
26 }
27
28 response.status(error.status).send(error.message)
29 }
30
31 /**
32 * Report exception for logging or debugging.
33 *
34 * @method report
35 *
36 * @param {Object} error
37 * @param {Object} options.request
38 *
39 * @return {void}
40 */
41 async report (error, { request }) {
42 }
43}
44
45module.exports = ExceptionHandler