import 'reflect-metadata' ; import { Get , Put , Delete , Param , QueryParam , Post , Body , JsonController , Authorized , Res , Req } from 'routing-controllers' ; import { Page } from '../models/Page' ; import { CreatePage } from './requests/CreatePageRequest' ; import { PageService } from '../services/PageService' ; import { UpdatePage } from './requests/UpdatePageRequest' ; import { DeletePageRequest } from './requests/DeletePageRequest' ; @ JsonController ( '/page' ) export class PageController { constructor ( private pageService ) { } api apiGroup apiParam apiParam apiParam apiParam apiParam apiParam apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/add-page' ) @ Authorized ( ) public async createPage ( @ Body ( { validate : true } ) pageParam , @ Res ( ) response ) { const page = new Page ( ) ; page . title = pageParam . title ; page . content = pageParam . content ; page . isActive = pageParam . active ; page . metaTagTitle = pageParam . metaTagTitle ; page . metaTagContent = pageParam . metaTagContent ; page . metaTagKeyword = pageParam . metaTagKeyword ; const pageSave = await this . pageService . create ( page ) ; if ( pageSave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully created a new page.' , data : pageSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to create page' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/pagelist' ) @ Authorized ( ) public async pageList ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'status' ) status , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { const select = [ 'pageId' , 'title' , 'content' , 'isActive' , 'metaTagTitle' , 'metaTagContent' , 'metaTagKeyword' ] ; const search = [ { name : 'title' , op : 'like' , value : keyword , } , { name : 'isActive' , op : 'like' , value : status , } , ] ; const WhereConditions = [ ] ; const pageList = await this . pageService . list ( limit , offset , select , search , WhereConditions , count ) ; if ( count ) { const successRes = { status : 1 , message : 'Successfully got pages count' , data : pageList , } ; return response . status ( 200 ) . send ( successRes ) ; } const successResponse = { status : 1 , message : 'Successfully got the complete list of pages. ' , data : pageList , } ; return response . status ( 200 ) . send ( successResponse ) ; } api apiGroup apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-page/:id' ) @ Authorized ( ) public async updatePage ( @ Body ( { validate : true } ) pageParam , @ Res ( ) response ) { console . log ( pageParam ) ; const page = await this . pageService . findOne ( { where : { pageId : pageParam . pageId , } , } ) ; if ( ! page ) { const errorResponse = { status : 0 , message : 'invalid page id' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } page . title = pageParam . title ; page . content = pageParam . content ; page . isActive = pageParam . active ; page . metaTagTitle = pageParam . metaTagTitle ; page . metaTagContent = pageParam . metaTagContent ; page . metaTagKeyword = pageParam . metaTagKeyword ; const pageSave = await this . pageService . create ( page ) ; if ( pageSave ) { const successResponse = { status : 1 , message : 'Successfully updated the page.' , data : pageSave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to update page' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-page/:id' ) @ Authorized ( ) public async deletePage ( @ Param ( 'id' ) id , @ Res ( ) response , @ Req ( ) request ) { const page = await this . pageService . findOne ( { where : { pageId : id , } , } ) ; if ( ! page ) { const errorResponse = { status : 0 , message : 'Invalid pageId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const deletePage = await this . pageService . delete ( page ) ; if ( deletePage ) { const successResponse = { status : 1 , message : 'Successfully deleted the page.' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'unable to delete page' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/delete-page' ) @ Authorized ( ) public async deleteMultiplePage ( @ Body ( { validate : true } ) pageDelete , @ Res ( ) response , @ Req ( ) request ) { const pageIdNo = pageDelete . pageId . toString ( ) ; const pageid = pageIdNo . split ( ',' ) ; for ( const id of pageid ) { const dataId = await this . pageService . findOne ( id ) ; if ( dataId === undefined ) { const errorResponse = { status : 0 , message : 'Please choose pages for delete' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } else { const deletePageId = parseInt ( id , 10 ) ; await this . pageService . delete ( deletePageId ) ; } } const successResponse = { status : 1 , message : 'Successfully deleted page' , } ; return response . status ( 200 ) . send ( successResponse ) ; } }