import 'reflect-metadata' ; import { Get , Put , Delete , Param , QueryParam , Post , Body , JsonController , Authorized , Res , Req } from 'routing-controllers' ; import { BannerService } from '../services/BannerService' ; import { env } from '../../env' ; import { Banner } from '../models/Banner' ; import { CreateBanner } from './requests/CreateBannerRequest' ; import { UpdateBanner } from './requests/UpdateBannerRequest' ; import { S3Service } from '../services/S3Service' ; import { ImageService } from '../services/ImageService' ; import { DeleteBannerRequest } from './requests/DeleteBannerRequest' ; @ JsonController ( '/banner' ) export class BannerController { constructor ( private bannerService , private s3Service , private imageService ) { } api apiGroup apiParam apiParam apiParam apiParam apiParam apiParam apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/add-banner' ) @ Authorized ( ) public async createBanner ( @ Body ( { validate : true } ) bannerParam , @ Res ( ) response ) { const image = bannerParam . image ; if ( image ) { const type = image . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; const path = 'banner/' ; const base64Data = new Buffer ( image . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; if ( env . imageserver === 's3' ) { await this . s3Service . imageUpload ( ( path + name ) , base64Data , type ) ; } else { await this . imageService . imageUpload ( ( path + name ) , base64Data ) ; } const newBanner = new Banner ( ) ; newBanner . title = bannerParam . title ; newBanner . content = bannerParam . content ; newBanner . image = name ; newBanner . imagePath = path ; newBanner . link = bannerParam . link ; newBanner . position = bannerParam . position ; newBanner . isActive = bannerParam . status ; const bannerSave = await this . bannerService . create ( newBanner ) ; console . log ( bannerSave + 'comminggg' ) ; if ( bannerSave ) { const successResponse = { status : 1 , message : 'Successfully created new banner.' , data : bannerSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Unable to create new banner. ' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/bannerlist' ) @ Authorized ( ) public async bannerList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'status' ) status , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'bannerId' , 'title' , 'image' , 'imagePath' , 'content' , 'link' , 'position' , 'isActive' ] ; const search = [ { name : 'title' , op : 'like' , value : keyword , } , { name : 'isActive' , op : 'like' , value : status , } , ] ; const WhereConditions = [ ] ; const bannerList = await this . bannerService . list ( limit , offset , select , search , WhereConditions , count ) ; if ( count ) { const successRes = { status : 1 , message : 'Successfully got banner count' , data : bannerList , } ; return response . status ( 200 ) . send ( successRes ) ; } const successResponse = { status : 1 , message : 'Successfully got banner list' , data : bannerList , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-banner/:id' ) @ Authorized ( ) public async deleteBanner ( @ Param ( 'id' ) id , @ Res ( ) response , @ Req ( ) request ) { const banner = await this . bannerService . findOne ( { where : { bannerId : id , } , } ) ; if ( ! banner ) { const errorResponse = { status : 0 , message : 'Invalid BannerId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deleteBanner = await this . bannerService . delete ( banner ) ; if ( deleteBanner ) { const successResponse = { status : 1 , message : 'Successfully deleted banner' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to delete banner' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-banner/:id' ) @ Authorized ( ) public async updateBanner ( @ Body ( { validate : true } ) bannerParam , @ Res ( ) response , @ Req ( ) request ) { const banner = await this . bannerService . findOne ( { where : { bannerId : bannerParam . bannerId , } , } ) ; if ( ! banner ) { const errorResponse = { status : 0 , message : 'Invalid BannerId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const image = bannerParam . image ; if ( image ) { const type = image . split ( ';' ) [ 0 ] . split ( '/' ) [ 1 ] ; const name = 'Img_' + Date . now ( ) + '.' + type ; const path = 'banner/' ; const base64Data = new Buffer ( image . replace ( /^data:image\/\w+;base64,/ , '' ) , 'base64' ) ; if ( env . imageserver === 's3' ) { await this . s3Service . imageUpload ( ( path + name ) , base64Data , type ) ; } else { await this . imageService . imageUpload ( ( path + name ) , base64Data ) ; } banner . image = name ; banner . imagePath = path ; } banner . title = bannerParam . title ; banner . content = bannerParam . content ; banner . link = bannerParam . link ; banner . position = bannerParam . position ; banner . isActive = bannerParam . status ; const bannerSave = await this . bannerService . create ( banner ) ; if ( bannerSave ) { const successResponse = { status : 1 , message : 'Successfully updated banner.' , data : bannerSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Unable to update the banner list. ' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/delete-banner' ) @ Authorized ( ) public async deleteMultipleBanner ( @ Body ( { validate : true } ) bannerDelete , @ Res ( ) response , @ Req ( ) request ) { const bannerIdNo = bannerDelete . bannerId . toString ( ) ; const bannerid = bannerIdNo . split ( ',' ) ; for ( const id of bannerid ) { const dataId = await this . bannerService . findOne ( id ) ; if ( dataId === undefined ) { const errorResponse = { status : 0 , message : 'Please choose banner for delete' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } else { const deleteBannerId = parseInt ( id , 10 ) ; await this . bannerService . delete ( deleteBannerId ) ; } } const successResponse = { status : 1 , message : 'Successfully deleted Banner' , } ; return response . status ( 200 ) . send ( successResponse ) ; } }