import express from 'express' ; import { ToPostgresError } from 'db' ; import { LogInstance } from 'log/loginstance' ; import { InventoryBuyMenu } from 'entities/inventory/buymenu' ; /**
 * handles requests to /inventory/:userId/buymenu
 */ export class InventoryBuyMenuRoute { public static InstallRoutes ( app ) { app . route ( '/inventory/:userId/buymenu' ) . get ( ( req , res ) => InventoryBuyMenuRoute . onGetInventoryBuyMenu ( req , res ) ) . post ( ( req , res ) => InventoryBuyMenuRoute . onPostInventoryBuyMenu ( req , res ) ) . put ( ( req , res ) => InventoryBuyMenuRoute . onPutInventoryBuyMenu ( req , res ) ) . delete ( ( req , res ) => InventoryBuyMenuRoute . onDeleteInventoryBuyMenu ( req , res ) ) ; } param req param res param next private static async onGetInventoryBuyMenu ( req , res ) { const reqUserId = Number ( req . params . userId ) ; LogInstance . debug ( `GET request to /inventory/${ reqUserId }/buymenu` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const buyMenu = await InventoryBuyMenu . getById ( reqUserId ) ; if ( buyMenu !== null ) { return res . status ( 200 ) . json ( buyMenu ) . 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 onPostInventoryBuyMenu ( req , res ) { const reqUserId = Number ( req . params . userId ) ; LogInstance . debug ( `POST request to /inventory/${ reqUserId }/buymenu` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const newBuyMenu = await InventoryBuyMenu . create ( reqUserId ) ; return res . status ( 201 ) . json ( newBuyMenu ) . end ( ) ; } catch ( error ) { const postgresErr = ToPostgresError ( error ) ; if ( postgresErr != null ) { if ( postgresErr . code === '23505' ) { LogInstance . warn ( `Tried to create a buy menu 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 onPutInventoryBuyMenu ( req , res ) { const reqUserId = Number ( req . params . userId ) ; LogInstance . debug ( `PUT request to /inventory/${ reqUserId }/buymenu` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const wasUpdated = await InventoryBuyMenu . set ( req . body , reqUserId ) ; 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 onDeleteInventoryBuyMenu ( req , res ) { const reqUserId = Number ( req . params . userId ) ; LogInstance . debug ( `DELETE request to /inventory/${ reqUserId }/buymenu` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const wasDeleted = await InventoryBuyMenu . 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 ( ) ; } } }