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