import 'reflect-metadata' ; import { Get , Put , Delete , Post , Body , JsonController , Authorized , Res , Req , QueryParam , Param } from 'routing-controllers' ; import { ZoneService } from '../services/zoneService' ; import { CountryService } from '../services/CountryService' ; import { Zone } from '../models/Zone' ; import { CreateZone } from './requests/CreateZoneRequest' ; import { classToPlain } from 'class-transformer' ; @ JsonController ( '/zone' ) export class ZoneController { constructor ( private zoneService , private countryService ) { } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/add-zone' ) @ Authorized ( ) public async addZone ( @ Body ( { validate : true } ) zoneParam , @ Res ( ) response ) { const country = await this . countryService . findOne ( { where : { countryId : zoneParam . countryId , } , } ) ; if ( ! country ) { const errorResponse = { status : 0 , message : 'Invalid countryId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const newZone = new Zone ( ) ; newZone . countryId = zoneParam . countryId ; newZone . code = zoneParam . code ; newZone . name = zoneParam . name ; newZone . isActive = zoneParam . status ; const zoneSave = await this . zoneService . create ( newZone ) ; if ( zoneSave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully created new zone' , data : zoneSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to create zone' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-zone/:id' ) @ Authorized ( ) public async updateZone ( @ Param ( 'id' ) id , @ Body ( { validate : true } ) zoneParam , @ Res ( ) response ) { const zone = await this . zoneService . findOne ( { where : { zoneId : id , } , } ) ; if ( zone ) { const country = await this . countryService . findOne ( { where : { countryId : zoneParam . countryId , } , } ) ; if ( ! country ) { const errorResponse = { status : 0 , message : 'Invalid countryId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } else { const errorResponse = { status : 0 , message : 'Invalid zoneId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } zone . countryId = zoneParam . countryId ; zone . code = zoneParam . code ; zone . name = zoneParam . name ; zone . isActive = zoneParam . status ; const zoneSave = await this . zoneService . create ( zone ) ; if ( zoneSave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully updated zone' , data : zoneSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to update zone' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/zone-list' ) @ Authorized ( ) public async zonelist ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'zoneId' , 'countryId' , 'code' , 'name' , 'isActive' ] ; const search = [ { name : 'name' , op : 'like' , value : keyword , } , ] ; const WhereConditions = [ ] ; const relation = [ 'country' ] ; const zoneList = await this . zoneService . list ( limit , offset , select , search , WhereConditions , relation , count ) ; if ( zoneList ) { const successResponse = { status : 1 , message : 'Successfully get all zone List' , data : classToPlain ( zoneList ) , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 1 , message : 'unable to get zone List' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-zone/:id' ) @ Authorized ( ) public async deleteZone ( @ Param ( 'id' ) id , @ Res ( ) response , @ Req ( ) request ) { const zone = await this . zoneService . findOne ( { where : { zoneId : id , } , } ) ; if ( ! zone ) { const errorResponse = { status : 0 , message : 'Invalid zoneId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deleteZone = await this . zoneService . delete ( zone ) ; console . log ( 'zone' + deleteZone ) ; if ( deleteZone ) { const successResponse = { status : 1 , message : 'Successfully deleted Zone' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to delete zone' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } }