import express from 'express' ; import { ToPostgresError } from 'db' ; import { LogInstance } from 'log/loginstance' ; import { Inventory } from 'entities/inventory/inventory' ; /**
 * handles requests to /inventory/:userId
 */ export class InventoryRoute { public static InstallRoutes ( app ) { app . route ( '/inventory/:userId' ) . get ( ( req , res ) => InventoryRoute . onGetInventory ( req , res ) ) . post ( ( req , res ) => InventoryRoute . onPostInventory ( req , res ) ) . delete ( ( req , res ) => InventoryRoute . onDeleteInventory ( req , res ) ) ; app . route ( '/inventory/:userId/item' ) . put ( ( req , res ) => InventoryRoute . onPutInventoryItem ( req , res ) ) . delete ( ( req , res ) => InventoryRoute . onDeleteInventoryItem ( req , res ) ) ; } param req param res param next private static async onGetInventory ( req , res ) { const reqUserId = Number ( req . params . userId ) ; LogInstance . debug ( `GET request to /inventory/${ reqUserId }` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const userInv = await Inventory . getById ( reqUserId ) ; if ( userInv != null ) { return res . status ( 200 ) . json ( userInv ) . end ( ) ; } else { return res . status ( 404 ) . end ( ) ; } } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res param next private static async onPostInventory ( req , res ) { const reqUserId = Number ( req . params . userId ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const newInventory = await Inventory . create ( reqUserId ) ; return res . status ( 201 ) . json ( newInventory ) . end ( ) ; } catch ( error ) { const postgresErr = ToPostgresError ( error ) ; if ( postgresErr != null ) { if ( postgresErr . code === '23505' ) { LogInstance . warn ( `Tried to create inventory for an existing user (${ reqUserId })` ) ; return res . status ( 409 ) . end ( ) ; } } LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res param next private static async onDeleteInventory ( req , res ) { const reqUserId = Number ( req . params . userId ) ; LogInstance . debug ( `DELETE request to /inventory/${ reqUserId }` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const wasDeleted = await Inventory . remove ( reqUserId ) ; if ( wasDeleted === true ) { return res . status ( 200 ) . end ( ) ; } else { return res . status ( 404 ) . end ( ) ; } } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res param next private static async onPutInventoryItem ( req , res ) { type putItemBody = { itemId : string ; ammount : string ; } ; const body = req . body as putItemBody ; const reqUserId = Number ( req . params . userId ) ; const reqItemId = Number ( body . itemId ) ; const reqItemAmmount = Number ( body . ammount ) ; LogInstance . debug ( `PUT request to /inventory/${ reqUserId }/item` ) ; if ( isNaN ( reqUserId ) || isNaN ( reqItemId ) || isNaN ( reqItemAmmount ) ) { return res . status ( 400 ) . end ( ) ; } try { const wasAdded = await Inventory . addItem ( reqItemId , reqItemAmmount , reqUserId ) ; if ( wasAdded === true ) { return res . status ( 200 ) . end ( ) ; } else { return res . status ( 404 ) . end ( ) ; } } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } param req param res param next private static async onDeleteInventoryItem ( req , res ) { type deleteItemBody = { itemId : string ; ammount : string ; } ; const body = req . body as deleteItemBody ; const reqUserId = Number ( req . params . userId ) ; const reqItemId = Number ( body . itemId ) ; const reqItemAmmount = Number ( body . ammount ) ; LogInstance . debug ( `DELETE request to /inventory/${ reqUserId }/item` ) ; if ( isNaN ( reqUserId ) || reqItemId == null || ( body . ammount != null && isNaN ( reqItemAmmount ) ) ) { return res . status ( 400 ) . end ( ) ; } try { const wasDeleted = await Inventory . removeItem ( reqItemId , reqUserId , reqItemAmmount ) ; if ( wasDeleted === true ) { return res . status ( 200 ) . end ( ) ; } else { return res . status ( 404 ) . end ( ) ; } } catch ( error ) { LogInstance . error ( error ) ; return res . status ( 500 ) . end ( ) ; } } }