import express from 'express' ; import { ToPostgresError } from 'db' ; import { LogInstance } from 'log/loginstance' ; import { InventoryCosmetics , ISetCosmeticsBody } from 'entities/inventory/cosmetics' ; /**
 * handles requests to /inventory/:userId/cosmetics
 */ export class InventoryCosmeticsRoute { public static InstallRoutes ( app ) { app . route ( '/inventory/:userId/cosmetics' ) . get ( ( req , res ) => InventoryCosmeticsRoute . onGetInventoryCosmetics ( req , res ) ) . post ( ( req , res ) => InventoryCosmeticsRoute . onPostInventoryCosmetics ( req , res ) ) . put ( ( req , res ) => InventoryCosmeticsRoute . onPutInventoryCosmetics ( req , res ) ) . delete ( ( req , res ) => InventoryCosmeticsRoute . onDeleteInventoryCosmetics ( req , res ) ) ; } param req param res param next private static async onGetInventoryCosmetics ( req , res ) { const reqUserId = Number ( req . params . userId ) ; LogInstance . debug ( `GET request to /inventory/${ reqUserId }/cosmetics` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const cosmetics = await InventoryCosmetics . getById ( reqUserId ) ; if ( cosmetics != null ) { return res . status ( 200 ) . json ( cosmetics ) . 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 onPostInventoryCosmetics ( req , res ) { const reqUserId = Number ( req . params . userId ) ; LogInstance . debug ( `POST request to /inventory/${ reqUserId }/cosmetics` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const newCosmetics = await InventoryCosmetics . create ( reqUserId ) ; return res . status ( 201 ) . json ( newCosmetics ) . end ( ) ; } catch ( error ) { const postgresErr = ToPostgresError ( error ) ; if ( postgresErr != null ) { if ( postgresErr . code === '23505' ) { LogInstance . warn ( `Tried to create cosmetic slots 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 onPutInventoryCosmetics ( req , res ) { const reqUserId = Number ( req . params . userId ) ; const reqCosmetics = req . body as ISetCosmeticsBody ; LogInstance . debug ( `PUT request to /inventory/${ reqUserId }/cosmetics` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const wasUpdated = await InventoryCosmetics . set ( reqCosmetics , 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 onDeleteInventoryCosmetics ( req , res ) { const reqUserId = Number ( req . params . userId ) ; LogInstance . debug ( `DELETE request to /inventory/${ reqUserId }/cosmetics` ) ; if ( isNaN ( reqUserId ) ) { return res . status ( 400 ) . end ( ) ; } try { const wasDeleted = await InventoryCosmetics . 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 ( ) ; } } }