import 'reflect-metadata' ; import { Get , Put , Post , Delete , Body , JsonController , Authorized , Res , Req , QueryParam , Param } from 'routing-controllers' ; import * as AWS from 'aws-sdk' ; import { CreateManufacturer } from './requests/CreateManufacturerRequest' ; import { Manufacturer } from '../models/ManufacturerModel' ; import { ManufacturerService } from '../services/ManufacturerService' ; import { aws_setup , env } from '../../env' ; import { UpdateManufacturer } from './requests/UpdateManufacturerRequest' ; import { DeleteManufacturer } from './requests/DeleteManufacturerRequest' ; import { S3Service } from '../services/S3Service' ; import { ImageService } from '../services/ImageService' ; AWS . config . update ( { accessKeyId : aws_setup . AWS_ACCESS_KEY_ID , secretAccessKey : aws_setup . AWS_SECRET_ACCESS_KEY , region : aws_setup . AWS_DEFAULT_REGION , } ) ; @ JsonController ( '/manufacturer' ) export class ManufacturerController { constructor ( private manufacturerService , private s3Service , private imageService ) { } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/create-manufacturer' ) @ Authorized ( ) public async createManufacturer ( @ Body ( { validate : true } ) manufacturer , @ Req ( ) request , @ Res ( ) response ) { const image = manufacturer . image ; if ( image ) { const path = 'manufacturer/' ; const base64Data = new Buffer ( image . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; const type = image . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; let val ; if ( env . imageserver === 's3' ) { val = await this . s3Service . imageUpload ( ( path + name ) , base64Data , type ) ; } else { val = await this . imageService . imageUpload ( ( path + name ) , base64Data ) ; } console . log ( val ) ; const newManufacturer = new Manufacturer ( ) ; newManufacturer . name = manufacturer . name ; newManufacturer . image = name ; newManufacturer . imagePath = path ; newManufacturer . sortOrder = manufacturer . sortOrder ; newManufacturer . isActive = manufacturer . status ; await this . manufacturerService . create ( newManufacturer ) ; const successResponse = { status : 1 , message : 'Successfully created a new Brand.' , data : { name , path } , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/manufacturerlist' ) @ Authorized ( ) public async manufacturerList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'status' ) status , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'manufacturerId' , 'name' , 'image' , 'imagePath' , 'sortOrder' , 'isActive' ] ; const search = [ { name : 'name' , op : 'like' , value : keyword , } , { name : 'isActive' , op : 'where' , value : status , } , ] ; const WhereConditions = [ ] ; const manufacturerList = await this . manufacturerService . list ( limit , offset , select , search , WhereConditions , count ) ; const successResponse = { status : 1 , message : 'Successfully got the complete brand list.' , data : manufacturerList , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-manufacturer/:id' ) @ Authorized ( ) public async updateManufacturer ( @ Body ( { validate : true } ) manufacturerParam , @ Res ( ) response , @ Req ( ) request ) { const manufacturer = await this . manufacturerService . findOne ( { where : { manufacturerId : manufacturerParam . manufacturerId , } , } ) ; if ( ! manufacturer ) { const errorResponse = { status : 0 , message : 'Invalid manufacturerId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const image = manufacturerParam . image ; if ( image ) { const type = image . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; const path = 'manufacturer/' ; const base64Data = new Buffer ( image . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; let val ; if ( env . imageserver === 's3' ) { val = await this . s3Service . imageUpload ( ( path + name ) , base64Data , type ) ; } else { val = await this . imageService . imageUpload ( ( path + name ) , base64Data ) ; } console . log ( val ) ; manufacturer . image = name ; manufacturer . imagePath = path ; } manufacturer . name = manufacturerParam . name ; manufacturer . sortOrder = manufacturerParam . sortOrder ; manufacturer . isActive = manufacturerParam . status ; const manufacturerSave = await this . manufacturerService . create ( manufacturer ) ; if ( manufacturerSave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully updated the Brand.' , data : manufacturerSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to update Brand' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-manufacturer/:id' ) @ Authorized ( ) public async deleteManufacturer ( @ Param ( 'id' ) id , @ Res ( ) response , @ Req ( ) request ) { const ManufacturerData = await this . manufacturerService . findOne ( { where : { manufacturerId : id , } , } ) ; if ( ! ManufacturerData ) { const errorResponse = { status : 0 , message : 'Invalid manufacturerId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deleteManufacturer = await this . manufacturerService . delete ( ManufacturerData . manufacturerId ) ; if ( ! deleteManufacturer ) { const successResponse = { status : 1 , message : 'Successfully deleted the Brand. ' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to delete manufacturer' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/delete-manufacturer' ) @ Authorized ( ) public async deleteMultipleManufacturer ( @ Body ( { validate : true } ) deleteManufacturer , @ Res ( ) response , @ Req ( ) request ) { const manufacturerIdNo = deleteManufacturer . manufacturerId . toString ( ) ; const manufacturerid = manufacturerIdNo . split ( ',' ) ; for ( const id of manufacturerid ) { const dataId = await this . manufacturerService . findOne ( id ) ; if ( dataId === undefined ) { const errorResponse = { status : 0 , message : 'Invalid manufacturerId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } else { const deleteManufacturerId = parseInt ( id , 10 ) ; await this . manufacturerService . delete ( deleteManufacturerId ) ; } } const successResponse = { status : 1 , message : 'Successfully deleted manufacturer' , } ; return response . status ( 200 ) . send ( successResponse ) ; } }