Skip to content

Commit

Permalink
Merge pull request #25 from Nailsonseat/room-vacancy-lost-and-found-two
Browse files Browse the repository at this point in the history
Room Vacancy and Lost & Found v2
  • Loading branch information
henilp105 authored Feb 18, 2024
2 parents 35ea30c + 63d7d87 commit 8cfad79
Show file tree
Hide file tree
Showing 25 changed files with 527 additions and 210 deletions.
6 changes: 4 additions & 2 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import roomResource from "./resources/rooms/roomResource.js";
import lostAndFoundListResource from "./resources/lostAndFound/lostAndFoundListResource.js";
import studentListResource from "./resources/student/studentListResource.js";
import facultyListResource from "./resources/faculty/facultyListResource.js";
import messageResource from "./resources/chatroom/messageListResource.js";
import lostAndFoundResource from "./resources/lostAndFound/lostAndFoundResource.js";
import timetableListResource from "./resources/timetable/timetableListResource.js";
import timetableResource from "./resources/timetable/timetableResource.js";
import messageResource from './resources/chatroom/messageListResource.js'

const PORT = `${process.env.PORT || 3000}`;
const app = express();
Expand Down Expand Up @@ -46,7 +47,8 @@ app.use("/", testResource);
app.use("/rooms", roomListResource);
app.use("/room", roomResource);
app.use("/lost-and-found", lostAndFoundListResource);
app.use("/messages",messageResource);
app.use("/lost-and-found-item", lostAndFoundResource);
app.use("/messages", messageResource);

app.get("/protected", tokenRequired, (req, res) => {
res.json({ message: "Access granted" });
Expand Down
12 changes: 11 additions & 1 deletion backend/constants/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ export const invalidUserType = "Invalid user type";
export const tokenUpdateError = "Error updating token";
export const badRequest = "Bad request";
export const notFound = "Not found";
export const deleted = "Deleted";
export const deleted = "Deleted";

// Lost and Found
export const itemNotFound = "Item not found";
export const itemAdded = "Item added successfully";
export const itemDeleted = "Item deleted successfully";

// Rooom Vacancy
export const roomNotFound = "Room not found";
export const roomUpdated = "Room updated successfully";
export const roomCreated = "Room created successfully";
9 changes: 8 additions & 1 deletion backend/middlewares/multerConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import multer from "multer";
import fs from "fs";

const uploadsFolder = "uploads/";

if (!fs.existsSync(uploadsFolder)) {
fs.mkdirSync(uploadsFolder);
}

// Define storage configuration
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads/");
cb(null, uploadsFolder);
},
filename: function (req, file, cb) {
cb(null, file.originalname);
Expand Down
4 changes: 4 additions & 0 deletions backend/models/lost_and_found.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const lostAndFoundItemSchema = new mongoose.Schema({
type: String,
required: true,
},
listerId: {
type: String,
required: true,
},
isLost: {
type: Boolean,
required: true,
Expand Down
4 changes: 4 additions & 0 deletions backend/models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const roomSchema = new mongoose.Schema({
type: String,
default: null,
},
occupantName: {
type: String,
default: null,
},
});

const Room = mongoose.model("Room", roomSchema);
Expand Down
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 * as 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;
32 changes: 32 additions & 0 deletions backend/resources/lostAndFound/lostAndFoundResource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import express from "express";
import * as messages from "../../constants/messages.js";
import LostAndFoundItem from "../../models/lost_and_found.js";
import fs from "fs";

const lostAndFoundRouter = express.Router();

// DELETE item by id
lostAndFoundRouter.delete("/:id", async (req, res) => {
const itemId = req.params.id;

try {
const item = await LostAndFoundItem.findByIdAndDelete(itemId);

if (!item) {
return res.status(404).json({ message: messages.itemNotFound });
}

// Delete the image file
fs.unlink(item.imagePath, (err) => {
if (err) {
console.error(err);
}
});

res.json(item);
} catch (err) {
res.status(500).json({ message: messages.internalServerError });
}
});

export default lostAndFoundRouter;
27 changes: 0 additions & 27 deletions backend/resources/rooms/occupantListResource.js

This file was deleted.

12 changes: 5 additions & 7 deletions backend/resources/rooms/roomListResource.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router } from "express";
import Room from "../../models/room.js";
import * as messages from "../../constants/messages.js";
const router = Router();

// GET method
Expand All @@ -13,27 +14,24 @@ router.get("/", async (req, res) => {
router.post("/", async (req, res) => {
try {
// Extract data from request body
const { name, vacant, occupantId } = req.body;
const { name, vacant, occupantId, occupantName } = 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
occupantName: occupantName || 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 });
res.status(201).json({ message: messages.roomCreated, room: newRoom });
} catch (error) {
// Handle errors
console.error("Error creating room:", error);
res.status(500).json({ error: "Internal server error" });
res.status(500).json({ message: messages.internalServerError });
}
});

Expand Down
22 changes: 14 additions & 8 deletions backend/resources/rooms/roomResource.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import { Router } from "express";
import Room from "../../models/room.js";
import * as messages from "../../constants/messages.js";
const router = Router();

// PUT method
router.put("/:id", async (req, res) => {
try {
const { id } = req.params;
const { occupantId } = req.body;
const { occupantName, occupantId, vacant } = req.body;
const room = await Room.findById(id);
if (!room) {
return res.status(404).json({ error: "Room not found" });
return res.status(404).json({ message: messages.roomNotFound });
}

room.vacant = false;
room.occupantId = occupantId || room.occupantId;
if (vacant) {
room.vacant = true;
room.occupantId = null;
room.occupantName = null;
} else {
room.vacant = false;
room.occupantId = occupantId;
room.occupantName = occupantName;
}

await room.save();

console.log("Room updated successfully");
res.json({ message: "Room updated successfully", room });
res.json({ message: messages.roomUpdated, room });
} catch (error) {
console.error("Error updating room:", error);
res.status(500).json({ error: "Internal server error" });
res.status(500).json({ message: messages.internalServerError });
}
});

Expand Down
16 changes: 10 additions & 6 deletions frontend/lib/components/material_textformfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class MaterialTextFormField extends StatelessWidget {
this.hintColor,
this.enabled,
this.controllerLessValue,
this.onTap,
this.maxLength,
this.maxLines,
this.textAlign,
this.inputFormatters,
this.onTap});
this.inputFormatters,});

final TextEditingController? controller;
final String? Function(String?)? validator;
Expand All @@ -29,6 +31,8 @@ class MaterialTextFormField extends StatelessWidget {
final TextAlign? textAlign;
final List<TextInputFormatter>? inputFormatters;
final Function? onTap;
final int? maxLength;
final int? maxLines;

@override
Widget build(BuildContext context) {
Expand All @@ -40,13 +44,13 @@ class MaterialTextFormField extends StatelessWidget {
onTap: () => onTap != null ? onTap!() : null,
enabled: enabled ?? true,
controller: controller ?? substituteController,
maxLines: 1,
inputFormatters: inputFormatters,
textAlign: textAlign ?? TextAlign.start,
maxLines: maxLines ?? 1,
maxLength: maxLength,
inputFormatters: inputFormatters,
onChanged: (value) => onChanged != null ? onChanged!(value) : null,
validator: (value) => validator != null ? validator!(value) : null,
onFieldSubmitted: (value) =>
onSubmitted != null ? onSubmitted!(value) : null,
onFieldSubmitted: (value) => onSubmitted != null ? onSubmitted!(value) : null,
cursorColor: Colors.teal.shade900,
style: TextStyle(
color: Colors.teal.shade900,
Expand Down
Loading

0 comments on commit 8cfad79

Please sign in to comment.