-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'room-vacancy-lost-and-found' into auth-complete
- Loading branch information
Showing
32 changed files
with
1,720 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import multer from "multer"; | ||
|
||
// Define storage configuration | ||
const storage = multer.diskStorage({ | ||
destination: function (req, file, cb) { | ||
cb(null, "uploads/"); | ||
}, | ||
filename: function (req, file, cb) { | ||
cb(null, file.originalname); | ||
}, | ||
}); | ||
|
||
const uploader = multer({ storage: storage }); | ||
|
||
export default uploader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
import mongoose from 'mongoose'; | ||
import mongoose from "mongoose"; | ||
|
||
const lostAndFoundItemSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
lastSeenLocation: { | ||
type: String, | ||
}, | ||
imagePath: { | ||
type: String, | ||
}, | ||
description: { | ||
type: String, | ||
required: true | ||
}, | ||
contactNumber: { | ||
type: String, | ||
required: true | ||
}, | ||
isLost: { | ||
type: Boolean, | ||
required: true | ||
} | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
lastSeenLocation: { | ||
type: String, | ||
}, | ||
imagePath: { | ||
type: String, | ||
nullable: true, | ||
}, | ||
description: { | ||
type: String, | ||
}, | ||
contactNumber: { | ||
type: String, | ||
required: true, | ||
}, | ||
isLost: { | ||
type: Boolean, | ||
required: true, | ||
}, | ||
}); | ||
|
||
const LostAndFoundItem = mongoose.model('LostAndFoundItem', lostAndFoundItemSchema); | ||
const LostAndFoundItem = mongoose.model( | ||
"LostAndFoundItem", | ||
lostAndFoundItemSchema | ||
); | ||
|
||
export default LostAndFoundItem; | ||
export default LostAndFoundItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
|
||
const mongoose = require('mongoose'); | ||
import mongoose from "mongoose"; | ||
|
||
const roomSchema = new mongoose.Schema({ | ||
id: { | ||
type: String, | ||
required: true, | ||
}, | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
vacant: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
occupantId: { | ||
type: String, | ||
default: null, | ||
}, | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
vacant: { | ||
type: Boolean, | ||
default: true, | ||
}, | ||
occupantId: { | ||
type: String, | ||
default: null, | ||
}, | ||
}); | ||
|
||
const Room = mongoose.model('Room', roomSchema); | ||
|
||
module.exports = Room; | ||
const Room = mongoose.model("Room", roomSchema); | ||
|
||
export default Room; |
74 changes: 74 additions & 0 deletions
74
backend/resources/lostAndFound/lostAndFoundListResource.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Router } from "express"; | ||
import LostAndFoundItem from "../../models/lost_and_found.js"; | ||
import fs from "fs/promises"; | ||
import uploader from "../../middlewares/multerConfig.js"; | ||
|
||
const router = Router(); | ||
|
||
// GET method to retrieve all items | ||
router.get("/", async (req, res) => { | ||
try { | ||
// Query the database to retrieve all items | ||
const items = await LostAndFoundItem.find({}); | ||
|
||
// 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 | ||
let imagePathBase64 = null; | ||
if (item.imagePath) { | ||
// Read the image file if imagePath is not null | ||
const bufferImage = await fs.readFile(item.imagePath); | ||
imagePathBase64 = bufferImage.toString("base64"); | ||
} | ||
|
||
// Create a new object with the required attributes | ||
const itemWithImage = { | ||
_id: item._id, | ||
name: item.name, | ||
lastSeenLocation: item.lastSeenLocation, | ||
imagePath: imagePathBase64, // Set imagePath to null if null in the database | ||
description: item.description, | ||
contactNumber: item.contactNumber, | ||
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"); | ||
} | ||
}); | ||
|
||
// POST method | ||
router.post("/", uploader.single("image"), async (req, res) => { | ||
// 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, | ||
}); | ||
|
||
// Save the new item to the database | ||
await newItem.save(); | ||
|
||
res.send("Added new item"); | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Router } from "express"; | ||
import Room from "../../models/room.js"; | ||
import Student from "../../models/student.js"; | ||
const router = Router(); | ||
|
||
//GET method | ||
router.get("/", async (req, res) => { | ||
try { | ||
const documentIds = req.body.documentIds; // Assuming the documentIds are sent in the request body | ||
|
||
const occupants = await Promise.all( | ||
documentIds.map(async (documentId) => { | ||
const room = await Room.findOne({ documentId }); | ||
const occupant = await Student.findOne({ _id: room.occupantId }); | ||
return { | ||
occupantName: occupant.name, | ||
roomId: room._id, | ||
}; | ||
}) | ||
); | ||
|
||
res.json(occupants); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: "Internal Server Error" }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Router } from "express"; | ||
import Room from "../../models/room.js"; | ||
const router = Router(); | ||
|
||
// GET method | ||
router.get("/", async (req, res) => { | ||
// Your code here | ||
const rooms = await Room.find({}); | ||
res.send(rooms); | ||
}); | ||
|
||
// POST method | ||
router.post("/", async (req, res) => { | ||
try { | ||
// Extract data from request body | ||
const { name, vacant, occupantId } = req.body; | ||
|
||
// Create a new room instance | ||
const newRoom = new Room({ | ||
name, | ||
vacant: vacant || true, // Set default value if not provided | ||
occupantId: occupantId || null, // Set default value if not provided | ||
}); | ||
|
||
// Save the new room to the database | ||
await newRoom.save(); | ||
console.log("Room created successfully"); | ||
|
||
// Respond with success message | ||
res | ||
.status(201) | ||
.json({ message: "Room created successfully", room: newRoom }); | ||
} catch (error) { | ||
// Handle errors | ||
console.error("Error creating room:", error); | ||
res.status(500).json({ error: "Internal server error" }); | ||
} | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Router } from "express"; | ||
import Room from "../../models/room.js"; | ||
const router = Router(); | ||
|
||
// PUT method | ||
router.put("/:id", async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
const { occupantId } = req.body; | ||
const room = await Room.findById(id); | ||
if (!room) { | ||
return res.status(404).json({ error: "Room not found" }); | ||
} | ||
|
||
room.vacant = false; | ||
room.occupantId = occupantId || room.occupantId; | ||
|
||
await room.save(); | ||
|
||
console.log("Room updated successfully"); | ||
res.json({ message: "Room updated successfully", room }); | ||
} catch (error) { | ||
console.error("Error updating room:", error); | ||
res.status(500).json({ error: "Internal server error" }); | ||
} | ||
}); | ||
|
||
export default router; |
Oops, something went wrong.