import 'reflect-metadata' ; import { Get , Post , Put , Delete , Body , JsonController , Authorized , QueryParam , Res , Req } from 'routing-controllers' ; import { classToPlain } from 'class-transformer' ; import { CategoryService } from '../services/CategoryService' ; import { AddCategory } from './requests/AddCategoryRequest' ; import { UpdateCategoryRequest } from './requests/UpdateCategoryRequest' ; import { Category } from '../models/CategoryModel' ; import { CategoryPath } from '../models/CategoryPath' ; import arrayToTree from 'array-to-tree' ; import { DeleteCategoryRequest } from './requests/DeleteCategoryRequest' ; import { CategoryPathService } from '../services/CategoryPathService' ; @ JsonController ( ) export class CategoryController { constructor ( private categoryService , private categoryPathService ) { } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Post ( '/add-category' ) @ Authorized ( ) public async addCategory ( @ Body ( { validate : true } ) category , @ Res ( ) response ) { console . log ( category . name ) ; const newCategory = new Category ( ) ; newCategory . name = category . name ; newCategory . parentInt = category . parentInt ; newCategory . sortOrder = category . sortOrder ; newCategory . metaTagTitle = category . metaTagTitle ; newCategory . metaTagDescription = category . metaTagDescription ; newCategory . metaTagKeyword = category . metaTagKeyword ; newCategory . isActive = category . status ; const categorySave = await this . categoryService . create ( newCategory ) ; const getAllPath = await this . categoryPathService . find ( { where : { categoryId : category . parentInt } , order : { level : 'ASC' } , } ) ; console . log ( getAllPath ) ; let level = 0 ; for ( const path of getAllPath ) { const CategoryPathLoop = new CategoryPath ( ) ; CategoryPathLoop . categoryId = categorySave . categoryId ; CategoryPathLoop . pathId = path . pathId ; CategoryPathLoop . level = level ; const vv = await this . categoryPathService . create ( CategoryPathLoop ) ; console . log ( 'vv' + vv ) ; level ++ ; } const newCategoryPath = new CategoryPath ( ) ; newCategoryPath . categoryId = categorySave . categoryId ; newCategoryPath . pathId = categorySave . categoryId ; newCategoryPath . level = level ; await this . categoryPathService . create ( newCategoryPath ) ; if ( categorySave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully created new category.' , data : categorySave , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Unable to create the category. ' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Put ( '/update-category/:id' ) @ Authorized ( ) public async updateCategory ( @ Body ( { validate : true } ) category , @ Res ( ) response , @ Req ( ) request ) { const categoryId = await this . categoryService . findOne ( { where : { categoryId : category . categoryId , } , } ) ; if ( ! categoryId ) { const errorResponse = { status : 0 , message : 'Invalid categoryId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } categoryId . name = category . name ; categoryId . parentInt = category . parentInt ; categoryId . sortOrder = category . sortOrder ; categoryId . metaTagTitle = category . metaTagTitle ; categoryId . metaTagDescription = category . metaTagDescription ; categoryId . metaTagKeyword = category . metaTagKeyword ; categoryId . isActive = category . status ; const categorySave = await this . categoryService . create ( categoryId ) ; const deleteCategory = await this . categoryPathService . find ( { where : { categoryId : category . categoryId } } ) ; for ( const val of deleteCategory ) { await this . categoryPathService . delete ( val . categoryPathId ) ; } const getAllPath = await this . categoryPathService . find ( { where : { categoryId : category . parentInt } , order : { level : 'ASC' } , } ) ; console . log ( getAllPath ) ; let level = 0 ; for ( const path of getAllPath ) { const CategoryPathLoop = new CategoryPath ( ) ; CategoryPathLoop . categoryId = categorySave . categoryId ; CategoryPathLoop . pathId = path . pathId ; CategoryPathLoop . level = level ; this . categoryPathService . create ( CategoryPathLoop ) ; level ++ ; } const newCategoryPath = new CategoryPath ( ) ; newCategoryPath . categoryId = categorySave . categoryId ; newCategoryPath . pathId = categorySave . categoryId ; newCategoryPath . level = level ; await this . categoryPathService . create ( newCategoryPath ) ; if ( categorySave !== undefined ) { const successResponse = { status : 1 , message : 'Successfully updated category.' , data : classToPlain ( categorySave ) , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Unable to update the category. ' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParamExample apiSuccessExample apiSampleRequest apiErrorExample @ Delete ( '/delete-category/:id' ) @ Authorized ( ) public async deleteCategory ( @ Body ( { validate : true } ) category , @ Res ( ) response , @ Req ( ) request ) { const categoryId = await this . categoryService . findOne ( { where : { categoryId : category . categoryId , } , } ) ; if ( ! categoryId ) { const errorResponse = { status : 0 , message : 'Invalid categoryId' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } const parentCategoryId = await this . categoryService . findOne ( { where : { parentInt : category . categoryId , } , } ) ; if ( parentCategoryId ) { const errorresponse = { status : 0 , message : 'you cannot delete parent categoryId' , } ; return response . status ( 400 ) . send ( errorresponse ) ; } const categoryPath = await this . categoryPathService . find ( { where : { categoryId : category . categoryId } } ) ; for ( const path of categoryPath ) { await this . categoryPathService . delete ( path . categoryPathId ) ; } const deleteCategory = await this . categoryService . delete ( categoryId ) ; console . log ( 'category' + deleteCategory ) ; if ( ! deleteCategory ) { const successResponse = { status : 1 , message : 'Successfully deleted category.' , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const errorResponse = { status : 0 , message : 'Unable to delete the category. ' , } ; return response . status ( 400 ) . send ( errorResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/categorylist' ) @ Authorized ( ) public async categorylist ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'sortOrder' ) sortOrder , @ QueryParam ( 'status' ) status , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { console . log ( keyword ) ; const select = [ 'categoryId' , 'name' , 'parentInt' , 'sortOrder' , 'metaTagTitle' , 'metaTagDescription' , 'metaTagKeyword' , 'isActive' ] ; const search = [ { name : 'name' , op : 'like' , value : keyword , } , { name : 'isActive' , op : 'where' , value : status , } , ] ; const WhereConditions = [ ] ; const category = await this . categoryService . list ( limit , offset , select , search , WhereConditions , sortOrder , count ) ; if ( count ) { const successResponse = { status : 1 , message : 'successfully got the complete category list. ' , data : category , } ; return response . status ( 200 ) . send ( successResponse ) ; } const promise = category . map ( async ( result ) => { const temp = result ; const categoryLevel = await this . categoryPathService . find ( { select : [ 'level' , 'pathId' ] , where : { categoryId : result . categoryId } , order : { level : 'ASC' } , } ) . then ( ( values ) => { const categories = values . map ( async ( val ) => { const categoryNames = await this . categoryService . findOne ( { categoryId : val . pathId } ) ; const JsonData = JSON . stringify ( categoryNames ) ; const ParseData = JSON . parse ( JsonData ) ; const tempVal = val ; tempVal . categoryName = ParseData . name ; return tempVal ; } ) ; const results = Promise . all ( categories ) ; return results ; } ) ; temp . levels = categoryLevel ; return temp ; } ) ; const value = await Promise . all ( promise ) ; if ( category ) { const successResponse = { status : 1 , message : 'successfully got the complete category list. ' , data : value , } ; return response . status ( 200 ) . send ( successResponse ) ; } } api apiGroup apiHeader apiParam apiParam apiParam apiParam apiParam apiSuccessExample apiSampleRequest apiErrorExample @ Get ( '/category-list-intree' ) @ Authorized ( ) public async categoryListTree ( @ QueryParam ( 'limit' ) limit , @ QueryParam ( 'offset' ) offset , @ QueryParam ( 'keyword' ) keyword , @ QueryParam ( 'sortOrder' ) sortOrder , @ QueryParam ( 'count' ) count , @ Res ( ) response ) { console . log ( keyword ) ; const select = [ 'categoryId' , 'name' , 'parentInt' , 'sortOrder' , 'metaTagTitle' , 'metaTagDescription' , 'metaTagKeyword' , 'isActive' ] ; const search = [ { name : 'name' , op : 'like' , value : keyword , } , ] ; const WhereConditions = [ ] ; const category = await this . categoryService . list ( limit , offset , select , search , WhereConditions , sortOrder , count ) ; if ( count ) { const successResponse = { status : 1 , message : 'Successfully get category List count' , data : category , } ; return response . status ( 200 ) . send ( successResponse ) ; } else { const categoryList = arrayToTree ( category , { parentProperty : 'parentInt' , customID : 'categoryId' , } ) ; const successResponse = { status : 1 , message : 'successfully got the complete category list.' , data : categoryList , } ; return response . status ( 200 ) . send ( successResponse ) ; } } }