import { Request , Response } from 'express' ; import { inject } from 'inversify' ; import { controller , httpGet , interfaces , request , requestParam , response } from 'inversify-express-utils' ; import SERVICE_IDENTIFIER from '../../../common/constants/identifiers' ; import { ILogger , IMetrics } from '../../../common/interfaces' ; import { IProduct } from '../../interfaces' ; import { HttpError } from '../../models' ; import { ErrorResponseBuilder , HttpStatus } from '../../services' ; /**
 * Shop API Controller
 */ @ controller ( '/shop' ) class ShopController implements interfaces . Controller { public productService : IProduct ; public loggerService : ILogger ; public metricsService : IMetrics ; public constructor ( @ inject ( SERVICE_IDENTIFIER . PRODUCT ) productService , @ inject ( SERVICE_IDENTIFIER . LOGGER ) loggerService , @ inject ( SERVICE_IDENTIFIER . METRICS ) metricsService ) { this . productService = productService ; this . loggerService = loggerService ; this . metricsService = metricsService ; } param req param res @ httpGet ( '/products' ) public allBaseProducts ( @ request ( ) req , @ response ( ) res ) { this . productService . allBaseProducts ( ) . subscribe ( result => { res . status ( HttpStatus . OK ) . json ( result ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } , err => { const error = err as HttpError ; const resp = new ErrorResponseBuilder ( ) . setTitle ( error . name ) . setStatus ( HttpStatus . NOT_FOUND ) . setDetail ( error . stack ) . setMessage ( error . message ) . setSource ( req . url ) . build ( ) ; res . status ( HttpStatus . NOT_FOUND ) . json ( resp ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } ) ; } param req param res @ httpGet ( '/productOptions' ) public allBaseProductOptions ( @ request ( ) req , @ response ( ) res ) { this . productService . allBaseProductOptions ( ) . subscribe ( result => { res . status ( HttpStatus . OK ) . json ( result ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } , err => { const error = err as HttpError ; const resp = new ErrorResponseBuilder ( ) . setTitle ( error . name ) . setStatus ( HttpStatus . NOT_FOUND ) . setDetail ( error . stack ) . setMessage ( error . message ) . setSource ( req . url ) . build ( ) ; res . status ( HttpStatus . NOT_FOUND ) . json ( resp ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } ) ; } param req param res @ httpGet ( '/prices' ) public allBaseProductPrice ( @ request ( ) req , @ response ( ) res ) { this . productService . allBaseProductPrice ( ) . subscribe ( result => { res . status ( HttpStatus . OK ) . json ( result ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } , err => { const error = err as HttpError ; const resp = new ErrorResponseBuilder ( ) . setTitle ( error . name ) . setStatus ( HttpStatus . NOT_FOUND ) . setDetail ( error . stack ) . setMessage ( error . message ) . setSource ( req . url ) . build ( ) ; res . status ( HttpStatus . NOT_FOUND ) . json ( resp ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } ) ; } param req param res @ httpGet ( '/inventory' ) public allBaseProductInventory ( @ request ( ) req , @ response ( ) res ) { this . productService . allBaseProductInventory ( ) . subscribe ( result => { res . status ( HttpStatus . OK ) . json ( result ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } , err => { const error = err as HttpError ; const resp = new ErrorResponseBuilder ( ) . setTitle ( error . name ) . setStatus ( HttpStatus . NOT_FOUND ) . setDetail ( error . stack ) . setMessage ( error . message ) . setSource ( req . url ) . build ( ) ; res . status ( HttpStatus . NOT_FOUND ) . json ( resp ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } ) ; } param id param req param res @ httpGet ( '/products/:id' ) public productbyId ( @ requestParam ( 'id' ) id , @ request ( ) req , @ response ( ) res ) { this . productService . baseProductbyId ( id ) . subscribe ( r => { if ( r ) { res . json ( r ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } else { res . status ( HttpStatus . NOT_FOUND ) . end ( ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } } ) ; } @ httpGet ( '/productOptions/:id' ) public baseProductOptionsbyId ( @ requestParam ( 'id' ) id , @ request ( ) req , @ response ( ) res ) { this . productService . baseProductOptionsbyId ( id ) . subscribe ( r => { if ( r ) { res . json ( r ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } else { res . status ( HttpStatus . NOT_FOUND ) . end ( ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } } ) ; } @ httpGet ( '/prices/:id' ) public baseProductPricebyId ( @ requestParam ( 'id' ) id , @ request ( ) req , @ response ( ) res ) { this . productService . baseProductPricebyId ( id ) . subscribe ( r => { if ( r ) { res . json ( r ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } else { res . status ( HttpStatus . NOT_FOUND ) . end ( ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } } ) ; } /**
     * Check by ID
     */ @ httpGet ( '/inventory/:id' ) public baseProductInventorybyId ( @ requestParam ( 'id' ) id , @ request ( ) req , @ response ( ) res ) { this . productService . baseProductInventorybyId ( id ) . subscribe ( r => { if ( r ) { res . json ( r ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } else { res . status ( HttpStatus . NOT_FOUND ) . end ( ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } } ) ; } param req param res @ httpGet ( '/priceByOptionId/:id' ) public flatMapProductOptionPricebyId ( @ requestParam ( 'id' ) id , @ request ( ) req , @ response ( ) res ) { this . productService . getProductOptionPricebyId ( id ) . subscribe ( r => { if ( r ) { res . json ( r ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } else { res . status ( HttpStatus . NOT_FOUND ) . end ( ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } } , err => { const error = err as HttpError ; const resp = new ErrorResponseBuilder ( ) . setTitle ( error . name ) . setStatus ( HttpStatus . NOT_FOUND ) . setDetail ( error . stack ) . setMessage ( error . message ) . setSource ( req . url ) . build ( ) ; res . status ( HttpStatus . NOT_FOUND ) . json ( resp ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } ) ; } } export default ShopController ;