import express from 'express' ; import { ToPostgresError } from 'db' ; import { LogInstance } from 'log/loginstance' ; import { InventoryLoadout , ISetLoadoutBody } from 'entities/inventory/loadout' ; /**
 * handles requests to /inventory/:userId/loadout
 */ export class InventoryLoadoutRoute { public static InstallRoutes ( app ) { app . route ( '/inventory/:userId/loadout' ) . post ( ( req , res ) => InventoryLoadoutRoute . onPostInventoryLoadout ( req , res ) ) . delete ( ( req , res ) => InventoryLoadoutRoute . onDeleteInventoryLoadout ( req , res ) ) ; app . route ( '/inventory/:userId/loadout/:loadoutNum' ) . get ( ( req , res ) => InventoryLoadoutRoute . onGetInventoryLoadout ( req , res ) ) . put ( ( req , res ) => InventoryLoadoutRoute . onPutInventoryLoadout ( req , res ) ) ; } param req param res param next private static async onGetInventoryLoadout ( req , res ) { const reqUserId = Number ( req . params . userId ) ; const reqLoadoutNum = Number ( req . params . loadoutNum ) ; if ( isNaN ( reqUserId ) || isNaN ( reqLoadoutNum ) ) { return res . status ( 400 ) . end ( ) ; } try { const loadout = await InventoryLoadout . getById ( reqLoadoutNum , reqUserId ) ; if ( loadout != null ) { return res . status ( 200 ) . json ( loadout ) . 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 onPostInventoryLoadout ( req , res ) { const reqUserId = Number ( req . params . userId ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const newLoadouts = await InventoryLoadout . create ( reqUserId ) ; return res . status ( 201 ) . json ( newLoadouts ) . end ( ) ; } catch ( error ) { const postgresErr = ToPostgresError ( error ) ; if ( postgresErr != null ) { if ( postgresErr . code === '23505' ) { LogInstance . warn ( `Tried to create loadouts 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 onPutInventoryLoadout ( req , res ) { const reqUserId = Number ( req . params . userId ) ; const reqLoadoutNum = Number ( req . params . loadoutNum ) ; const reqLoadout = req . body as ISetLoadoutBody ; if ( isNaN ( reqUserId ) || isNaN ( reqLoadoutNum ) == null ) { return res . status ( 400 ) . end ( ) ; } try { const wasUpdated = await InventoryLoadout . set ( reqLoadout , reqUserId , reqLoadoutNum ) ; if ( wasUpdated === 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 onDeleteInventoryLoadout ( req , res ) { const reqUserId = Number ( req . params . userId ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const wasDeleted = await InventoryLoadout . 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 ( ) ; } } }