From 7ddf5ffe841891bb324c6055bf8f419d3075b2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Thu, 13 May 2021 18:44:18 +0200 Subject: [PATCH] BETTERZON-100: Some code reformatting --- .../models/categories/categories.router.ts | 6 ++--- .../manufacturers/manufacturers.router.ts | 6 ++--- Backend/src/models/prices/prices.router.ts | 8 +++---- .../src/models/products/products.router.ts | 8 +++---- Backend/src/models/users/users.router.ts | 24 ++++++++++++------- Backend/src/models/users/users.service.ts | 8 +++---- Backend/src/models/vendors/vendors.router.ts | 6 ++--- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/Backend/src/models/categories/categories.router.ts b/Backend/src/models/categories/categories.router.ts index 500d94f..9d811f6 100644 --- a/Backend/src/models/categories/categories.router.ts +++ b/Backend/src/models/categories/categories.router.ts @@ -27,7 +27,7 @@ categoriesRouter.get('/', async (req: Request, res: Response) => { res.status(200).send(categories); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -46,7 +46,7 @@ categoriesRouter.get('/:id', async (req: Request, res: Response) => { res.status(200).send(category); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -65,6 +65,6 @@ categoriesRouter.get('/search/:term', async (req: Request, res: Response) => { res.status(200).send(categories); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); diff --git a/Backend/src/models/manufacturers/manufacturers.router.ts b/Backend/src/models/manufacturers/manufacturers.router.ts index bbd3c09..e78815c 100644 --- a/Backend/src/models/manufacturers/manufacturers.router.ts +++ b/Backend/src/models/manufacturers/manufacturers.router.ts @@ -27,7 +27,7 @@ manufacturersRouter.get('/', async (req: Request, res: Response) => { res.status(200).send(manufacturers); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -46,7 +46,7 @@ manufacturersRouter.get('/:id', async (req: Request, res: Response) => { res.status(200).send(manufacturer); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -65,6 +65,6 @@ manufacturersRouter.get('/search/:term', async (req: Request, res: Response) => res.status(200).send(manufacturer); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); diff --git a/Backend/src/models/prices/prices.router.ts b/Backend/src/models/prices/prices.router.ts index f215a82..12ffaf1 100644 --- a/Backend/src/models/prices/prices.router.ts +++ b/Backend/src/models/prices/prices.router.ts @@ -40,7 +40,7 @@ pricesRouter.get('/', async (req: Request, res: Response) => { res.status(200).send(prices); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -59,7 +59,7 @@ pricesRouter.get('/:id', async (req: Request, res: Response) => { res.status(200).send(price); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -78,7 +78,7 @@ pricesRouter.get('/bestDeals/:amount', async (req: Request, res: Response) => { res.status(200).send(prices); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -97,6 +97,6 @@ pricesRouter.get('/byProduct/list/:ids', async (req: Request, res: Response) => res.status(200).send(prices); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); diff --git a/Backend/src/models/products/products.router.ts b/Backend/src/models/products/products.router.ts index 0c5c22d..f0ea9a3 100644 --- a/Backend/src/models/products/products.router.ts +++ b/Backend/src/models/products/products.router.ts @@ -27,7 +27,7 @@ productsRouter.get('/', async (req: Request, res: Response) => { res.status(200).send(products); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -46,7 +46,7 @@ productsRouter.get('/:id', async (req: Request, res: Response) => { res.status(200).send(product); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -65,7 +65,7 @@ productsRouter.get('/search/:term', async (req: Request, res: Response) => { res.status(200).send(products); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -84,6 +84,6 @@ productsRouter.get('/list/:ids', async (req: Request, res: Response) => { res.status(200).send(products); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); diff --git a/Backend/src/models/users/users.router.ts b/Backend/src/models/users/users.router.ts index 07a2bad..db28a93 100644 --- a/Backend/src/models/users/users.router.ts +++ b/Backend/src/models/users/users.router.ts @@ -47,10 +47,13 @@ usersRouter.post('/register', async (req: Request, res: Response) => { const session: Session = await UserService.createUser(username, password, email, ip); // Send the session details back to the user - res.cookie('betterauth', JSON.stringify({id: session.session_id, key: session.session_key}), {expires: new Date(Date.now() + 1000*60*60*24*30)}).sendStatus(201); + res.cookie('betterauth', JSON.stringify({ + id: session.session_id, + key: session.session_key + }), {expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30)}).sendStatus(201); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -70,17 +73,20 @@ usersRouter.post('/login', async (req: Request, res: Response) => { // Update the user entry and create a session const session: Session = await UserService.login(username, password, ip); - if(!session.session_id) { + if (!session.session_id) { // Error logging in, probably wrong username / password - res.status(401).send(JSON.stringify({messages: ["Wrong username and / or password"], codes: [1, 4]})); + res.status(401).send(JSON.stringify({messages: ['Wrong username and / or password'], codes: [1, 4]})); return; } // Send the session details back to the user - res.cookie('betterauth', JSON.stringify({id: session.session_id, key: session.session_key}), {expires: new Date(Date.now() + 1000*60*60*24*30)}).sendStatus(200); + res.cookie('betterauth', JSON.stringify({ + id: session.session_id, + key: session.session_key + }), {expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 30)}).sendStatus(200); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -92,9 +98,9 @@ usersRouter.post('/checkSessionValid', async (req: Request, res: Response) => { // Update the user entry and create a session const user: User = await UserService.checkSessionWithCookie(req.cookies.betterauth, ip); - if(!user.user_id) { + if (!user.user_id) { // Error logging in, probably wrong username / password - res.status(401).send(JSON.stringify({messages: ["Invalid session"], codes: [5]})); + res.status(401).send(JSON.stringify({messages: ['Invalid session'], codes: [5]})); return; } @@ -102,6 +108,6 @@ usersRouter.post('/checkSessionValid', async (req: Request, res: Response) => { res.status(201).send(user); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); diff --git a/Backend/src/models/users/users.service.ts b/Backend/src/models/users/users.service.ts index ac1cfd5..6958cc6 100644 --- a/Backend/src/models/users/users.service.ts +++ b/Backend/src/models/users/users.service.ts @@ -179,7 +179,7 @@ export const checkSession = async (sessionId: string, sessionKey: string, ip: st // Key is valid, continue // Check if the session is still valid - if(validUntil <= new Date()) { + if (validUntil <= new Date()) { // Session expired, return invalid return {} as User; } @@ -193,7 +193,7 @@ export const checkSession = async (sessionId: string, sessionKey: string, ip: st await conn.commit(); // Get the other required user information and update the user - const userQuery = "SELECT user_id, username, email, registration_date, last_login_date FROM users WHERE user_id = ?"; + const userQuery = 'SELECT user_id, username, email, registration_date, last_login_date FROM users WHERE user_id = ?'; const userRows = await conn.query(userQuery, userId); let username = ''; let email = ''; @@ -234,14 +234,14 @@ export const checkSession = async (sessionId: string, sessionKey: string, ip: st * @param cookie The betterauth cookie * @param ip The users IP address */ -export const checkSessionWithCookie = async(cookie: any, ip: string): Promise => { +export const checkSessionWithCookie = async (cookie: any, ip: string): Promise => { const parsedCookie = JSON.parse(cookie); const session_id = parsedCookie.id; const session_key = parsedCookie.key; return checkSession(session_id, session_key, ''); -} +}; /** * Used in the checkUsernameAndEmail method as return value diff --git a/Backend/src/models/vendors/vendors.router.ts b/Backend/src/models/vendors/vendors.router.ts index 335cac6..e587ca1 100644 --- a/Backend/src/models/vendors/vendors.router.ts +++ b/Backend/src/models/vendors/vendors.router.ts @@ -27,7 +27,7 @@ vendorsRouter.get('/', async (req: Request, res: Response) => { res.status(200).send(vendors); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -46,7 +46,7 @@ vendorsRouter.get('/:id', async (req: Request, res: Response) => { res.status(200).send(vendor); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); @@ -65,6 +65,6 @@ vendorsRouter.get('/search/:term', async (req: Request, res: Response) => { res.status(200).send(vendors); } catch (e) { console.log('Error handling a request: ' + e.message); - res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } });