import { Router , Request , Response , NextFunction } from 'express' ; import { Container } from 'typedi' ; import DependenceService from '../services/dependence' ; import { Logger } from 'winston' ; import { celebrate , Joi } from 'celebrate' ; const route = Router ( ) ; export default ( app ) => { app . use ( '/dependencies' , route ) ; route . get ( '/' , async ( req , res , next ) => { const logger = Container . get ( 'logger' ) ; try { const dependenceService = Container . get ( DependenceService ) ; const data = await dependenceService . dependencies ( req . query as any ) ; return res . send ( { code : 200 , data } ) ; } catch ( e ) { logger . error ( '🔥 error: %o' , e ) ; return next ( e ) ; } } ) ; route . post ( '/' , celebrate ( { body : Joi . array ( ) . items ( Joi . object ( { name : Joi . string ( ) . required ( ) , type : Joi . number ( ) . required ( ) , remark : Joi . string ( ) . optional ( ) . allow ( '' ) , } ) ) , } ) , async ( req , res , next ) => { const logger = Container . get ( 'logger' ) ; try { const dependenceService = Container . get ( DependenceService ) ; const data = await dependenceService . create ( req . body ) ; return res . send ( { code : 200 , data } ) ; } catch ( e ) { logger . error ( '🔥 error: %o' , e ) ; return next ( e ) ; } } ) ; route . put ( '/' , celebrate ( { body : Joi . object ( { name : Joi . string ( ) . required ( ) , id : Joi . number ( ) . required ( ) , type : Joi . number ( ) . required ( ) , remark : Joi . string ( ) . optional ( ) . allow ( '' ) , } ) , } ) , async ( req , res , next ) => { const logger = Container . get ( 'logger' ) ; try { const dependenceService = Container . get ( DependenceService ) ; const data = await dependenceService . update ( req . body ) ; return res . send ( { code : 200 , data } ) ; } catch ( e ) { logger . error ( '🔥 error: %o' , e ) ; return next ( e ) ; } } ) ; route . delete ( '/' , celebrate ( { body : Joi . array ( ) . items ( Joi . number ( ) . required ( ) ) , } ) , async ( req , res , next ) => { const logger = Container . get ( 'logger' ) ; try { const dependenceService = Container . get ( DependenceService ) ; const data = await dependenceService . remove ( req . body ) ; return res . send ( { code : 200 , data } ) ; } catch ( e ) { logger . error ( '🔥 error: %o' , e ) ; return next ( e ) ; } } ) ; route . delete ( '/force' , celebrate ( { body : Joi . array ( ) . items ( Joi . number ( ) . required ( ) ) , } ) , async ( req , res , next ) => { const logger = Container . get ( 'logger' ) ; try { const dependenceService = Container . get ( DependenceService ) ; const data = await dependenceService . removeDb ( req . body ) ; return res . send ( { code : 200 , data } ) ; } catch ( e ) { logger . error ( '🔥 error: %o' , e ) ; return next ( e ) ; } } ) ; route . get ( '/:id' , celebrate ( { params : Joi . object ( { id : Joi . number ( ) . required ( ) , } ) , } ) , async ( req , res , next ) => { const logger = Container . get ( 'logger' ) ; try { const dependenceService = Container . get ( DependenceService ) ; const data = await dependenceService . getDb ( { id : req . params . id } ) ; return res . send ( { code : 200 , data } ) ; } catch ( e ) { logger . error ( '🔥 error: %o' , e ) ; return next ( e ) ; } } ) ; route . put ( '/reinstall' , celebrate ( { body : Joi . array ( ) . items ( Joi . number ( ) . required ( ) ) , } ) , async ( req , res , next ) => { const logger = Container . get ( 'logger' ) ; try { const dependenceService = Container . get ( DependenceService ) ; const data = await dependenceService . reInstall ( req . body ) ; return res . send ( { code : 200 , data } ) ; } catch ( e ) { logger . error ( '🔥 error: %o' , e ) ; return next ( e ) ; } } ) ; } ;