Skip to content

Commit cf8f35d

Browse files
committed
fix: add-history file path
1 parent fc6928a commit cf8f35d

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

art-gallery/app/controllers/theme.controller.js

+31-23
Original file line numberDiff line numberDiff line change
@@ -47,60 +47,68 @@ const addHistory = async (req, res) => {
4747
});
4848
}
4949

50-
const { artist, art } = req.body;
50+
const theme = await Theme.findOne({ slug });
51+
if (!theme) {
52+
return res.status(404).json({
53+
status: "error",
54+
message: "Theme not found.",
55+
});
56+
}
5157

58+
const { artist, art } = req.body;
5259
if (!artist || !art) {
53-
return res.status(404).json({
60+
return res.status(400).json({
5461
status: "error",
5562
message: "artist and art is required",
5663
});
5764
}
5865

5966
let image = "";
60-
67+
let newFilename = "";
6168
if (req.file) {
69+
const historyCount = theme.history.length + 1;
70+
console.log(historyCount);
6271
let oldFilename = req.file.filename;
63-
let extension = oldFilename.split(".")[oldFilename.split(".").length - 1];
64-
image = slug + "." + extension;
65-
72+
let extension = oldFilename.split(".").pop();
73+
newFilename = `${slug}-h-${historyCount}.${extension}`;
74+
6675
fs.rename(
6776
req.file.path,
68-
req.file.destination + "/" + image,
77+
req.file.destination + "/" + newFilename,
6978
function (err) {
7079
if (err) {
7180
throw new Error("Error renaming file. Error: " + err);
7281
}
73-
});
74-
image = "/theme/" + image;
75-
82+
}
83+
);
84+
image = "/theme/" + newFilename;
7685
} else {
7786
return res.status(400).json({
7887
status: "error",
7988
message: "Image is required"
8089
});
8190
}
82-
83-
const theme = await Theme.findOne({ slug });
84-
if (!theme) {
85-
return res.status(404).json({
86-
status: "error",
87-
message: "Theme not found.",
88-
});
89-
}
90-
91+
console.log(image);
9192
const historyEntry = {
9293
src: image,
9394
artist: JSON.parse(artist),
94-
art: JSON.parse(art),
95+
art: JSON.parse(art)
9596
};
96-
console.log(historyEntry);
9797

9898
theme.history.push(historyEntry);
9999
await theme.save();
100100

101-
return res.status(201).json(theme);
101+
return res.status(200).json({
102+
status: "success",
103+
message: "History added successfully",
104+
theme,
105+
filename: newFilename
106+
});
102107
} catch (e) {
103-
console.log(e)
108+
console.log(e);
109+
if (req.file) {
110+
fs.existsSync(req.file.path) && deleteFile(req.file.path);
111+
}
104112
return res.status(500).json({
105113
status: "error",
106114
message: "Something went wrong.",

art-gallery/routes/theme.routes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ router.put(
4343

4444
router.put(
4545
"/:slug/add-history",
46-
//verify,
46+
verify,
4747
setPath,
48-
uploader.single("history_image"),
48+
uploader.single("history_image"), // Changed back to "history_image"
4949
convertToWebP,
5050
addHistory
5151
);

0 commit comments

Comments
 (0)