diff --git a/src/controllers/block.ts b/src/controllers/block.ts index 9b2af30..fab2859 100644 --- a/src/controllers/block.ts +++ b/src/controllers/block.ts @@ -57,15 +57,7 @@ class BlockController { public static async update(accountUUID: string, name: string, data: any): Promise { try { - let _block - const _account = await Account.get(accountUUID) - - try { - _block = await Block.get(accountUUID, name) - } catch (error) { - await _account.saveError(error.message) - throw error - } + const _block = await BlockController.fetchBlock(accountUUID, name) await _block.update(data) const _blockDetails = _block.sanitize() @@ -80,16 +72,7 @@ class BlockController { public static async delete(accountUUID: string, name: string): Promise { try { - let _block - - const _account = await Account.get(accountUUID) - - try { - _block = await Block.get(accountUUID, name) - } catch (error) { - await _account.saveError(error.message) - throw error - } + const _block = await BlockController.fetchBlock(accountUUID, name) logger.info(`Removing block from account: ${accountUUID}`) await _block.delete() @@ -100,6 +83,19 @@ class BlockController { throw error } } + + private static async fetchBlock(accountUUID: string, name: string): Promise { + const _account = await Account.get(accountUUID) + + try { + const _block = await Block.get(accountUUID, name) + + return _block + } catch (error) { + await _account.saveError(error.message) + throw error + } + } } export default BlockController diff --git a/src/interfaces/account.ts b/src/interfaces/account.ts index 57c05e3..39a2692 100644 --- a/src/interfaces/account.ts +++ b/src/interfaces/account.ts @@ -30,3 +30,7 @@ export interface IAccountPublic extends IAccountBase { baskets: IBlockInfo[], percentFull: number, } + +export interface IAccountRequestParams { + pantryID: string, +} diff --git a/src/interfaces/block.ts b/src/interfaces/block.ts index f7d234b..e60947d 100644 --- a/src/interfaces/block.ts +++ b/src/interfaces/block.ts @@ -8,3 +8,8 @@ export interface IBlockInfo { name: string, ttl: number, } + +export interface IBlockRequestParams { + pantryID: string, + basketName: string, +} diff --git a/src/routes/apiV1.ts b/src/routes/apiV1.ts index 2e5520d..e11b9c3 100644 --- a/src/routes/apiV1.ts +++ b/src/routes/apiV1.ts @@ -6,6 +6,10 @@ import AccountController from '../controllers/account' import BlockController from '../controllers/block' import logService from '../services/logger' +// Interfaces +import { IAccountRequestParams } from '../interfaces/account' +import { IBlockRequestParams } from '../interfaces/block' + // Logger setup const logger = new logService('API') @@ -16,6 +20,7 @@ _apiV1Router.post('/create', async (req, res) => { try { const { body } = req + logger.info('[POST] Create Account', body) const _newAccountUUID = await AccountController.create(body) @@ -27,10 +32,10 @@ _apiV1Router.post('/create', async (req, res) => { _apiV1Router.put('/:pantryID', async (req, res) => { try { - const { body, params } = req - const { pantryID } = params + const { body } = req + const { pantryID } = accountParams(req) - logger.info('[PUT] Update Pantry', params) + logger.info('[PUT] Update Pantry', { pantryID }) const _response = await AccountController.update(pantryID, body) res.send(_response) @@ -41,10 +46,9 @@ _apiV1Router.put('/:pantryID', async (req, res) => { _apiV1Router.get('/:pantryID', async (req, res) => { try { - const { params } = req - const { pantryID } = params + const { pantryID } = accountParams(req) - logger.info('[GET] Get Account', params) + logger.info('[GET] Get Account', { pantryID }) const _account = await AccountController.get(pantryID) res.send(_account) @@ -55,10 +59,9 @@ _apiV1Router.get('/:pantryID', async (req, res) => { _apiV1Router.delete('/:pantryID', async (req, res) => { try { - const { params } = req - const { pantryID } = params + const { pantryID } = accountParams(req) - logger.info('[DELETE] Delete Account', params) + logger.info('[DELETE] Delete Account', { pantryID }) const _response = await AccountController.delete(pantryID) res.send(_response) @@ -69,10 +72,10 @@ _apiV1Router.delete('/:pantryID', async (req, res) => { _apiV1Router.post('/:pantryID/basket/:basketName', async (req, res) => { try { - const { body, params } = req - const { pantryID, basketName } = params + const { body } = req + const { pantryID, basketName } = basketParams(req) - logger.info('[POST] Create Basket', params) + logger.info('[POST] Create Basket', { pantryID, basketName }) const _response = await BlockController.create(pantryID, basketName, body) res.send(_response) @@ -83,10 +86,10 @@ _apiV1Router.post('/:pantryID/basket/:basketName', async (req, res) => { _apiV1Router.put('/:pantryID/basket/:basketName', async (req, res) => { try { - const { body, params } = req - const { pantryID, basketName } = params + const { body } = req + const { pantryID, basketName } = basketParams(req) - logger.info('[PUT] Update Basket', params) + logger.info('[PUT] Update Basket', { pantryID, basketName }) const _response = await BlockController.update(pantryID, basketName, body) res.send(_response) @@ -97,10 +100,9 @@ _apiV1Router.put('/:pantryID/basket/:basketName', async (req, res) => { _apiV1Router.get('/:pantryID/basket/:basketName', async (req, res) => { try { - const { params } = req - const { pantryID, basketName } = params + const { pantryID, basketName } = basketParams(req) - logger.info('[GET] Get Basket', params) + logger.info('[GET] Get Basket', { pantryID, basketName }) const _response = await BlockController.get(pantryID, basketName) res.send(_response) @@ -111,10 +113,9 @@ _apiV1Router.get('/:pantryID/basket/:basketName', async (req, res) => { _apiV1Router.delete('/:pantryID/basket/:basketName', async (req, res) => { try { - const { params } = req - const { pantryID, basketName } = params + const { pantryID, basketName } = basketParams(req) - logger.info('[DELETE] Basket', params) + logger.info('[DELETE] Basket', { pantryID, basketName }) const _response = await BlockController.delete(pantryID, basketName) res.send(_response) @@ -123,4 +124,16 @@ _apiV1Router.delete('/:pantryID/basket/:basketName', async (req, res) => { } }) +function basketParams(req): IBlockRequestParams { + const { params } = req + const { pantryID, basketName } = params + return { pantryID, basketName } +} + +function accountParams(req): IAccountRequestParams { + const { params } = req + const { pantryID } = params + return { pantryID } +} + export default _apiV1Router