1+ import slugify from "slugify" ;
2+ import { catchAsyncError } from '../../utils/catch_async_error' ;
3+ import { AppError } from '../../utils/app_error' ;
4+ import { subCategoryModel } from '../../models/subcategory_model' ;
5+ import { deleteOne } from '../../handler/factor' ;
6+ import { ApiFeatures } from '../../utils/api_feature' ;
7+
8+ const addSubCategory = catchAsyncError ( async ( req , res , next ) => {
9+ req . body . slug = slugify ( req . body . name ) ;
10+ const addSubcategory = new subCategoryModel ( req . body ) ;
11+ await addSubcategory . save ( ) ;
12+
13+ res . status ( 201 ) . json ( { message : "success" , addSubcategory } ) ;
14+ } ) ;
15+
16+ const getAllSubCategories = catchAsyncError ( async ( req , res , next ) => {
17+ console . log ( req . params ) ;
18+ let filterObj = { } ;
19+
20+
21+ if ( req . params . category ) {
22+ filterObj = { category : req . params . category } ;
23+ }
24+
25+ const apiFeature = new ApiFeatures (
26+ subCategoryModel . find ( filterObj ) ,
27+ req . query
28+ ) . filteration ( ) ;
29+
30+ const getAllSubCategories = await apiFeature . mongooseQuery ;
31+
32+ res . status ( 201 ) . json ( { message : "success" , getAllSubCategories } ) ;
33+ } ) ;
34+
35+ const updateSubCategory = catchAsyncError ( async ( req , res , next ) => {
36+ const { id } = req . params ;
37+ if ( req . body . name ) {
38+ req . body . slug = slugify ( req . body . name ) ;
39+ }
40+ const updateSubCategory = await subCategoryModel . findByIdAndUpdate (
41+ id ,
42+ req . body ,
43+ {
44+ new : true ,
45+ }
46+ ) ;
47+
48+ updateSubCategory &&
49+ res . status ( 201 ) . json ( { message : "success" , updateSubCategory } ) ;
50+
51+ ! updateSubCategory && next ( new AppError ( "subcategory was not found" , 404 ) ) ;
52+ } ) ;
53+
54+ const deleteSubCategory = deleteOne ( subCategoryModel , "subcategory" ) ;
55+ export {
56+ addSubCategory ,
57+ getAllSubCategories ,
58+ updateSubCategory ,
59+ deleteSubCategory ,
60+ } ;
0 commit comments