1
+ import slugify from "slugify" ;
2
+ import { categoryModel } from '../../models/category_model' ;
3
+ import { catchAsyncError } from "../../utils/catch_async_error" ;
4
+ import { AppError } from "../../utils/app_error" ;
5
+ import { deleteOne } from "../../handler/factor" ;
6
+ import { ApiFeatures } from '../../utils/api_feature' ;
7
+
8
+ const addCategory = catchAsyncError ( async ( req , res , next ) => {
9
+ console . log ( req . file ) ;
10
+ req . body . Image = req . file . filename ;
11
+ req . body . slug = slugify ( req . body . name ) ;
12
+ const addcategory = new categoryModel ( req . body ) ;
13
+ await addcategory . save ( ) ;
14
+
15
+ res . status ( 201 ) . json ( { message : "success" , addcategory } ) ;
16
+ } ) ;
17
+
18
+ const getAllCategories = catchAsyncError ( async ( req , res , next ) => {
19
+ let apiFeature = new ApiFeatures ( categoryModel . find ( ) , req . query )
20
+ . pagination ( )
21
+ . fields ( )
22
+ . filteration ( )
23
+ . search ( )
24
+ . sort ( ) ;
25
+
26
+ const PAGE_NUMBER = apiFeature . queryString . page * 1 || 1 ;
27
+ let getAllCategories = await apiFeature . mongooseQuery ;
28
+
29
+ res
30
+ . status ( 201 )
31
+ . json ( { page : PAGE_NUMBER , message : "success" , getAllCategories } ) ;
32
+ } ) ;
33
+
34
+ const updateCategory = catchAsyncError ( async ( req , res , next ) => {
35
+ const { id } = req . params ;
36
+ const { name } = req . body ;
37
+ req . body . slug = slugify ( req . body . name ) ;
38
+ const updateCategory = await categoryModel . findByIdAndUpdate ( id , req . body , {
39
+ new : true ,
40
+ } ) ;
41
+
42
+ updateCategory &&
43
+ res . status ( 201 ) . json ( { message : "success" , updateCategory } ) ;
44
+
45
+ ! updateCategory && next ( new AppError ( "category was not found" , 404 ) ) ;
46
+ } ) ;
47
+
48
+ const deleteCategory = deleteOne ( categoryModel , "category" ) ;
49
+ export { addCategory , getAllCategories , updateCategory , deleteCategory } ;
0 commit comments