From b185e4e5e36bd0f3c630c0f5e51ffd635726a5d2 Mon Sep 17 00:00:00 2001 From: Patrick <50352812+Mueller-Patrick@users.noreply.github.com> Date: Sun, 16 May 2021 12:28:11 +0200 Subject: [PATCH] BETTERZON-93: Adding API endpoint to get managed shops (#47) --- Backend/src/models/vendors/vendors.router.ts | 24 ++++++++++++++-- Backend/src/models/vendors/vendors.service.ts | 28 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/Backend/src/models/vendors/vendors.router.ts b/Backend/src/models/vendors/vendors.router.ts index e587ca1..aca9086 100644 --- a/Backend/src/models/vendors/vendors.router.ts +++ b/Backend/src/models/vendors/vendors.router.ts @@ -6,6 +6,7 @@ import express, {Request, Response} from 'express'; import * as VendorService from './vendors.service'; import {Vendor} from './vendor.interface'; import {Vendors} from './vendors.interface'; +import * as UserService from '../users/users.service'; /** @@ -19,7 +20,7 @@ export const vendorsRouter = express.Router(); * Controller Definitions */ -// GET items/ +// GET vendors/ vendorsRouter.get('/', async (req: Request, res: Response) => { try { const vendors: Vendors = await VendorService.findAll(); @@ -31,7 +32,24 @@ vendorsRouter.get('/', async (req: Request, res: Response) => { } }); -// GET items/:id +// GET vendors/managed +vendorsRouter.get('/managed', async (req: Request, res: Response) => { + console.log('here'); + try { + // Authenticate user + const user_ip = req.connection.remoteAddress ?? ''; + const user = await UserService.checkSessionWithCookie(req.cookies.betterauth, user_ip); + + const vendors = await VendorService.getManagedShops(user.user_id); + + 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.'})); + } +}); + +// GET vendors/:id vendorsRouter.get('/:id', async (req: Request, res: Response) => { const id: number = parseInt(req.params.id, 10); @@ -50,7 +68,7 @@ vendorsRouter.get('/:id', async (req: Request, res: Response) => { } }); -// GET items/:name +// GET vendors/search/:term vendorsRouter.get('/search/:term', async (req: Request, res: Response) => { const term: string = req.params.term; diff --git a/Backend/src/models/vendors/vendors.service.ts b/Backend/src/models/vendors/vendors.service.ts index ca8bbee..6cd43a5 100644 --- a/Backend/src/models/vendors/vendors.service.ts +++ b/Backend/src/models/vendors/vendors.service.ts @@ -17,6 +17,7 @@ const pool = mariadb.createPool({ import {Vendor} from './vendor.interface'; import {Vendors} from './vendors.interface'; +import {User} from '../users/user.interface'; /** @@ -123,3 +124,30 @@ export const findBySearchTerm = async (term: string): Promise => { return vendorRows; }; + +/** + * Get all vendors that have the given user as admin + * @param user The user to return the managed shops for + */ +export const getManagedShops = async (user_id: number): Promise => { + let conn; + let vendorRows = []; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE admin_id LIKE ?', user_id); + for (let row in rows) { + if (row !== 'meta') { + vendorRows.push(rows[row]); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return vendorRows; +};