Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up some codesmells #110

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions src/controllers/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,7 @@ class BlockController {

public static async update(accountUUID: string, name: string, data: any): Promise<any> {
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()
Expand All @@ -80,16 +72,7 @@ class BlockController {

public static async delete(accountUUID: string, name: string): Promise<string> {
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()
Expand All @@ -100,6 +83,19 @@ class BlockController {
throw error
}
}

private static async fetchBlock(accountUUID: string, name: string): Promise<Block> {
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
4 changes: 4 additions & 0 deletions src/interfaces/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ export interface IAccountPublic extends IAccountBase {
baskets: IBlockInfo[],
percentFull: number,
}

export interface IAccountRequestParams {
pantryID: string,
}
5 changes: 5 additions & 0 deletions src/interfaces/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export interface IBlockInfo {
name: string,
ttl: number,
}

export interface IBlockRequestParams {
pantryID: string,
basketName: string,
}
55 changes: 34 additions & 21 deletions src/routes/apiV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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