Skip to content

Commit

Permalink
Accomodated listerId inn get, post and put method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nailsonseat committed Feb 16, 2024
1 parent fc62bc3 commit 286ca1d
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions backend/resources/lostAndFound/lostAndFoundListResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Router } from "express";
import LostAndFoundItem from "../../models/lost_and_found.js";
import fs from "fs/promises";
import uploader from "../../middlewares/multerConfig.js";
import messages from "../../constants/messages.js";

const router = Router();

Expand All @@ -13,7 +14,7 @@ router.get("/", async (req, res) => {

// Create an empty array to store items with images
const itemsWithImages = [];
``

// Iterate through each item
for (const item of items) {
// Check if imagePath is null
Expand All @@ -32,43 +33,46 @@ router.get("/", async (req, res) => {
imagePath: imagePathBase64, // Set imagePath to null if null in the database
description: item.description,
contactNumber: item.contactNumber,
listerId: item.listerId,
isLost: item.isLost,
};

// Push the item with image to the array
itemsWithImages.push(itemWithImage);
}

console.log("Retrieved items:", itemsWithImages.length);

// Send the response with the items
res.json(itemsWithImages);
} catch (error) {
// Handle errors
console.error("Error:", error);
res.status(500).send("Error retrieving items");
res.status(500).json({ message: messages.internalServerError });
}
});

// POST method
router.post("/", uploader.single("image"), async (req, res) => {
// Access the uploaded file using req.file
const file = req.file;
try {
// Access the uploaded file using req.file
const file = req.file;

// Construct the LostAndFoundItem object with data from the request
const newItem = new LostAndFoundItem({
name: req.body.name,
lastSeenLocation: req.body.lastSeenLocation,
imagePath: file ? file.path : null,
description: req.body.description,
contactNumber: req.body.contactNumber,
isLost: req.body.isLost,
});
// Construct the LostAndFoundItem object with data from the request
const newItem = new LostAndFoundItem({
name: req.body.name,
lastSeenLocation: req.body.lastSeenLocation,
imagePath: file ? file.path : null,
description: req.body.description,
contactNumber: req.body.contactNumber,
listerId: req.body.listerId,
isLost: req.body.isLost,
});

// Save the new item to the database
await newItem.save();
// Save the new item to the database
await newItem.save();

res.send("Added new item");
res.json({ message: messages.itemAdded });
} catch (error) {
res.status(500).json({ message: messages.internalServerError });
}
});

export default router;

0 comments on commit 286ca1d

Please sign in to comment.