@@ -80,57 +80,49 @@ export const createProduct = async (req: Request, res: Response) => {
80
80
} ;
81
81
82
82
export const updateProduct = async ( req : Request , res : Response ) => {
83
+ const transaction = await sequelize . transaction ( ) ;
83
84
try {
84
85
const { productId } = req . params ;
85
86
86
87
const data = {
87
88
name : req . body . name ,
88
89
description : req . body . description ,
89
90
colors : req . body . colors ,
91
+ categoryName : req . body . categoryName ,
90
92
} ;
91
93
92
- // update images
93
- if ( req . files ) {
94
- const product = await Product . findByPk ( productId ) ;
95
- const foundImages = product ?. images ;
96
-
97
- // delete already existing images
98
- if ( foundImages instanceof Array ) {
99
- for ( let i = 0 ; i < foundImages . length ; i ++ ) {
100
- const strImg = foundImages [ i ] . toString ( ) ;
101
- const imageId = extractImageId ( strImg ) as string ;
102
- await destroyImage ( imageId ) ;
103
- product ! . images = [ ] ;
104
- }
105
- }
94
+ // find product by id
95
+ const foundProduct = Product . findByPk ( productId ) ;
106
96
107
- // update new images
108
- const images : unknown = req . files ;
109
- const productImages = [ ] ;
110
- if ( images instanceof Array && images . length > 3 ) {
111
- for ( const image of images ) {
112
- const imageBuffer : Buffer = image . buffer ;
113
- const url = await uploadImage ( imageBuffer ) ;
114
- productImages . push ( url ) ;
115
- Product . update ( { images : productImages } , { where : { id : productId } } ) ;
116
- }
117
- } else {
118
- return res . status ( 400 ) . json ( {
119
- message : 'Product should have at least 4 images' ,
120
- } ) ;
121
- }
97
+ if ( ! foundProduct ) {
98
+ return res . status ( 400 ) . json ( { message : 'Product not found' } ) ;
122
99
}
100
+
123
101
// update product
124
- Product . update ( data , { where : { id : productId } } ) . then ( ( ) => {
125
- res . status ( 200 ) . json ( {
102
+ const [ affectedRows ] = await Product . update ( data , { where : { id : productId } , transaction } ) ;
103
+
104
+ // update sizes if provided
105
+ if ( req . body . sizes ) {
106
+ const sizes = req . body . sizes ;
107
+ for ( const sizeData of sizes ) {
108
+ await Size . update ( { ...sizeData } , { where : { productId } , transaction } ) ;
109
+ }
110
+ }
111
+
112
+ await transaction . commit ( ) ;
113
+
114
+ if ( affectedRows !== 0 ) {
115
+ return res . status ( 200 ) . json ( {
126
116
ok : true ,
127
117
message : 'Product updated successfully' ,
128
118
} ) ;
129
- } ) ;
119
+ }
130
120
} catch ( error ) {
121
+ await transaction . rollback ( ) ;
131
122
sendInternalErrorResponse ( res , error ) ;
132
123
}
133
124
} ;
125
+
134
126
// get size
135
127
export const getAllSizes = async ( req : Request , res : Response ) => {
136
128
try {
@@ -362,7 +354,7 @@ export const deleteProductById = async (req: Request, res: Response) => {
362
354
}
363
355
364
356
// deleting product related sizes
365
- const sizes = await Size . findAll ( { where : { id } , transaction } ) ;
357
+ await Size . findAll ( { where : { id } , transaction } ) ;
366
358
await Size . destroy ( { where : { id } , transaction } ) ;
367
359
368
360
// deleting the product itself
0 commit comments