import 'reflect-metadata' ; import { Get , Put , Delete , Post , Body , JsonController , Authorized , Res , Req , Param , QueryParam } from 'routing-controllers' ; import { CreateCountry } from './requests/CreateCountryRequest' ; import { Country } from '../models/Country' ; import { CountryService } from '../services/CountryService' ; import { UpdateCountry } from './requests/UpdateCountryRequest' ; @ JsonController ( '/country' ) export class CountryController { constructor ( private countryService ) { } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/add-country' ) @ Authorized ( ) public async addCountry ( @ Body ( { validate : true } ) countryParam , @ Res ( ) response ) { const existCountry = await this . countryService . findOne ( { where : { name : countryParam . name , isoCode2 : countryParam . isoCode2 , isoCode3 : countryParam . isoCode3 , } , } ) ; if ( existCountry ) { const errorResponse = { status : 0 , message : 'you already added this country.' , } ; return response . status ( 200 ) . send ( errorResponse ) ; } const newCountry = new Country ( ) ; newCountry . name = countryParam . name ; newCountry . isoCode2 = countryParam . isoCode2 ; newCountry . isoCode3 = countryParam . isoCode3 ; newCountry . postcodeRequired = countryParam . postcodeRequired ; newCountry . isActive = countryParam . status ; const countrySave = await this . countryService . create ( newCountry ) ; if ( countrySave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully added new country.' , data : countrySave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Unable to add the country. ' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-country/:id' ) @ Authorized ( ) public async updateCountry ( @ Body ( { validate : true } ) countryParam , @ Res ( ) response ) { const country = await this . countryService . findOne ( { where : { countryId : countryParam . countryId , } , } ) ; if ( ! country ) { const errorResponse = { status : 0 , message : 'Invalid countryId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } country . name = countryParam . name ; country . isoCode2 = countryParam . isoCode2 ; country . isoCode3 = countryParam . isoCode3 ; country . postcodeRequired = countryParam . postcodeRequired ; country . isActive = countryParam . status ; const countrySave = await this . countryService . create ( country ) ; if ( countrySave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully updated country' , data : countrySave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to update country' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/countrylist' ) @ Authorized ( ) public async countryList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'countryId' , 'name' , 'isoCode2' , 'isoCode3' , 'postcodeRequired' , 'isActive' ] ; const search = [ { name : 'name' , op : 'like' , value : keyword , } , ] ; const WhereConditions = [ ] ; const countryList = await this . countryService . list ( limit , offset , select , search , WhereConditions , count ) ; if ( countryList ) { const successResponse = { status : 1 , message : 'Successfully got country List' , data : countryList , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to get countryList' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-country/:id' ) @ Authorized ( ) public async deleteCountry ( @ Param ( 'id' ) id , @ Res ( ) response , @ Req ( ) request ) { const country = await this . countryService . findOne ( { where : { countryId : id , } , } ) ; if ( ! country ) { const errorResponse = { status : 0 , message : 'Invalid CountryId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deleteCountry = await this . countryService . delete ( country ) ; if ( deleteCountry ) { const successResponse = { status : 1 , message : 'Successfully deleted country.' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to delete country' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } }