events page, manage events page, create events page
This commit is contained in:
@@ -15,8 +15,8 @@ router.post("/", async (req, res) => {
|
||||
time: yup.string().required(),
|
||||
location: yup.string().required(),
|
||||
category: yup.string().required(),
|
||||
imageUrl: yup.string().matches(/(\/uploads\/.+)/, 'Image URL must be a valid path').required(),
|
||||
slotsAvailable: yup.number().integer().required()
|
||||
imageUrl: yup.string(),
|
||||
slotsAvailable: yup.number().integer().required(),
|
||||
});
|
||||
|
||||
try {
|
||||
@@ -32,68 +32,18 @@ router.post("/", async (req, res) => {
|
||||
});
|
||||
|
||||
const upload = multer({ storage: multer.memoryStorage() });
|
||||
|
||||
router.put(
|
||||
"/:id",
|
||||
upload.single("image"),
|
||||
async (req, res) => {
|
||||
const id = req.params.id;
|
||||
|
||||
// Check if file is uploaded
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ message: "No file uploaded" });
|
||||
}
|
||||
|
||||
try {
|
||||
const { buffer, mimetype, size } = req.file;
|
||||
|
||||
// Validate file type and size (example: max 5MB, only images)
|
||||
const allowedTypes = ["image/jpeg", "image/png", "image/gif"];
|
||||
const maxSize = 5 * 1024 * 1024; // 5MB
|
||||
|
||||
if (!allowedTypes.includes(mimetype)) {
|
||||
return res.status(400).json({
|
||||
message:
|
||||
"Invalid file type\nSupported: jpeg, png, gif\nUploaded: " +
|
||||
mimetype.substring(6),
|
||||
});
|
||||
}
|
||||
|
||||
if (size > maxSize) {
|
||||
return res.status(400).json({
|
||||
message:
|
||||
"File too large!\nMaximum: 5MB, Uploaded: " +
|
||||
(size / 1000000).toFixed(2) +
|
||||
"MB",
|
||||
});
|
||||
}
|
||||
|
||||
// Crop the image to a square
|
||||
const croppedBuffer = await sharp(buffer)
|
||||
.resize({ width: 512, height: 512, fit: sharp.fit.cover }) // Adjust size as necessary
|
||||
.toBuffer();
|
||||
|
||||
await User.update(
|
||||
{ Events: croppedBuffer },
|
||||
{ where: { id: id } }
|
||||
);
|
||||
|
||||
res.json({ message: "Event image successfully uploaded." });
|
||||
} catch (err) {
|
||||
res
|
||||
.status(500)
|
||||
.json({ message: "Internal server error", errors: err.errors });
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const list = await Events.findAll({
|
||||
router.get("/", async (req, res) => {
|
||||
try {
|
||||
const list = await Events.findAll({
|
||||
order: [['createdAt', 'DESC']],
|
||||
});
|
||||
res.json(list);
|
||||
});
|
||||
});
|
||||
res.json(list);
|
||||
} catch (error) {
|
||||
console.error("Error fetching events:", error);
|
||||
res.status(500).json({ message: "Internal Server Error" });
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/:id", async (req, res) => {
|
||||
const id = req.params.id;
|
||||
@@ -115,8 +65,8 @@ router.put("/:id", async (req, res) => {
|
||||
time: yup.string(),
|
||||
location: yup.string(),
|
||||
category: yup.string(),
|
||||
imageUrl: yup.string().matches(/(\/uploads\/.+)/, 'Image URL must be a valid path'),
|
||||
slotsAvailable: yup.number().integer()
|
||||
imageUrl: yup.string(),
|
||||
slotsAvailable: yup.number().integer(),
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user