@@ -71,7 +71,7 @@ const addHistory = async (req, res) => {
71
71
throw new Error ( "Error renaming file. Error: " + err ) ;
72
72
}
73
73
} ) ;
74
- image = "/images/ theme/" + image ;
74
+ image = "/theme/" + image ;
75
75
76
76
} else {
77
77
return res . status ( 400 ) . json ( {
@@ -145,12 +145,12 @@ const show = async (req, res) => {
145
145
146
146
const create = async ( req , res ) => {
147
147
try {
148
- const { theme_title, theme_description, work_title, work_description } = req . body ;
149
- let slug = slugify ( name ) ;
148
+ const { theme_title, theme_description, work_title, work_description, info_link } = req . body ;
149
+ let slug = slugify ( theme_title ) ;
150
150
let i = 0 ;
151
151
152
152
while ( await Theme . findOne ( { slug : slug } ) ) {
153
- slug = slugify ( name , ++ i ) ;
153
+ slug = slugify ( theme_title , ++ i ) ;
154
154
}
155
155
156
156
if ( ! req . files || ! Object . hasOwn ( req . files , "theme_image" ) || ! Object . hasOwn ( req . files , "work_image" ) ) {
@@ -161,12 +161,13 @@ const create = async (req, res) => {
161
161
}
162
162
163
163
let theme_images = [ ] ;
164
- for ( img of req . files [ "theme_image" ] ) {
164
+ for ( let idx = 0 ; idx < req . files [ "theme_image" ] . length ; idx ++ ) {
165
+ const img = req . files [ "theme_image" ] [ idx ] ;
165
166
let image = "" ;
166
-
167
- let oldFilename = img . filename ;
168
- let extension = oldFilename . split ( "." ) [ oldFilename . split ( "." ) . length - 1 ] ;
169
- image = slug + "." + extension ;
167
+ let extension = img . filename . split ( "." ) . pop ( ) ;
168
+ // Add number suffix for multiple images
169
+ image = ` ${ slug } - ${ idx + 1 } . ${ extension } ` ;
170
+
170
171
fs . rename (
171
172
img . path ,
172
173
img . destination + "/" + image ,
@@ -176,16 +177,17 @@ const create = async (req, res) => {
176
177
}
177
178
}
178
179
) ;
179
- theme_images . push ( "/images/ theme/" + image ) ;
180
+ theme_images . push ( "/theme/" + image ) ;
180
181
}
181
-
182
+
182
183
let work_images = [ ] ;
183
- for ( img of req . files [ "work_image" ] ) {
184
+ for ( let idx = 0 ; idx < req . files [ "work_image" ] . length ; idx ++ ) {
185
+ const img = req . files [ "work_image" ] [ idx ] ;
184
186
let image = "" ;
185
-
186
- let oldFilename = img . filename ;
187
- let extension = oldFilename . split ( "." ) [ oldFilename . split ( "." ) . length - 1 ] ;
188
- image = slug + "." + extension ;
187
+ let extension = img . filename . split ( "." ) . pop ( ) ;
188
+ // Add number suffix for multiple images
189
+ image = ` ${ slug } -work- ${ idx + 1 } . ${ extension } ` ;
190
+
189
191
fs . rename (
190
192
img . path ,
191
193
img . destination + "/" + image ,
@@ -195,7 +197,7 @@ const create = async (req, res) => {
195
197
}
196
198
}
197
199
) ;
198
- work_images . push ( "/images/ theme/" + image ) ;
200
+ work_images . push ( "/theme/" + image ) ;
199
201
}
200
202
201
203
const theme = await Theme . create ( {
@@ -205,11 +207,14 @@ const create = async (req, res) => {
205
207
theme_images,
206
208
work_images,
207
209
work_description,
208
- work_title
210
+ work_title,
211
+ info_link : info_link || null // Add info_link with null fallback
209
212
} ) ;
213
+
210
214
return res . status ( 200 ) . json ( {
211
215
status : "success" ,
212
216
message : "Theme added successfully." ,
217
+ theme : theme
213
218
} ) ;
214
219
} catch ( e ) {
215
220
if ( req . file ) {
@@ -225,7 +230,7 @@ const create = async (req, res) => {
225
230
const edit = async ( req , res ) => {
226
231
try {
227
232
const { slug } = req . params ;
228
- const { theme_title, theme_description, work_title, work_description } = req . body ;
233
+ const { theme_title, theme_description, work_title, work_description, info_link } = req . body ;
229
234
let theme = await Theme . findOne ( { slug } ) ;
230
235
if ( ! theme ) {
231
236
return res
@@ -245,13 +250,15 @@ const edit = async (req, res) => {
245
250
if ( work_description ) {
246
251
theme . work_description = work_description ;
247
252
}
253
+ if ( info_link !== undefined ) theme . info_link = info_link ; // Add info_link update
248
254
249
255
if ( req . files && Object . hasOwn ( req . files , 'theme_image' ) ) {
250
256
let theme_images = [ ] ;
251
- for ( file of req . files [ "theme_image" ] ) {
252
- let oldFilename = file . filename ;
253
- let extension = oldFilename . split ( "." ) [ oldFilename . split ( "." ) . length - 1 ] ;
254
- let image = slug + "." + extension ;
257
+ for ( let idx = 0 ; idx < req . files [ "theme_image" ] . length ; idx ++ ) {
258
+ const file = req . files [ "theme_image" ] [ idx ] ;
259
+ let extension = file . filename . split ( "." ) . pop ( ) ;
260
+ let image = `${ slug } -${ idx + 1 } .${ extension } ` ;
261
+
255
262
fs . rename (
256
263
file . path ,
257
264
file . destination + "/" + image ,
@@ -263,18 +270,18 @@ const edit = async (req, res) => {
263
270
}
264
271
}
265
272
) ;
266
- theme_images . push ( image ) ;
273
+ theme_images . push ( "/theme/" + image ) ;
267
274
}
268
275
theme . theme_images = theme_images ;
269
276
}
270
277
271
278
if ( req . files && Object . hasOwn ( req . files , 'work_image' ) ) {
272
279
let work_images = [ ] ;
273
-
274
- for ( file of req . files [ "work_image" ] ) {
275
- let oldFilename = file . filename ;
276
- let extension = oldFilename . split ( "." ) [ oldFilename . split ( "." ) . length - 1 ] ;
277
- let image = slug + "." + extension ;
280
+ for ( let idx = 0 ; idx < req . files [ "work_image" ] . length ; idx ++ ) {
281
+ const file = req . files [ "work_image" ] [ idx ] ;
282
+ let extension = file . filename . split ( "." ) . pop ( ) ;
283
+ let image = ` ${ slug } -work- ${ idx + 1 } . ${ extension } ` ;
284
+
278
285
fs . rename (
279
286
file . path ,
280
287
file . destination + "/" + image ,
@@ -286,7 +293,7 @@ const edit = async (req, res) => {
286
293
}
287
294
}
288
295
) ;
289
- work_images . push ( image ) ;
296
+ work_images . push ( "/theme/" + image ) ;
290
297
}
291
298
theme . work_images = work_images ;
292
299
}
0 commit comments