Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Room Vacancy and Lost & Found v2 #25

Merged
merged 22 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
17af2c8
Added auto sized text
Nailsonseat Feb 16, 2024
b6f53fa
added message for lost and found
Nailsonseat Feb 16, 2024
6e2f66a
remove unused resource
Nailsonseat Feb 16, 2024
4e1337f
added a list Id attribute to track who listed a item
Nailsonseat Feb 16, 2024
7c042d5
Create uploads folder if not exist
Nailsonseat Feb 16, 2024
14e15d1
added occupant name to room schema
Nailsonseat Feb 16, 2024
fc62bc3
Added delete method for lost and found alongwith logic to remove list…
Nailsonseat Feb 16, 2024
286ca1d
Accomodated listerId inn get, post and put method
Nailsonseat Feb 16, 2024
a64333b
occupant name in post and put method
Nailsonseat Feb 16, 2024
3b9585f
Fix imports and add lost-and-found resource
Nailsonseat Feb 16, 2024
54e1d00
Updated dummy entries to add new attribute
Nailsonseat Feb 16, 2024
b976caf
Add textAlign, maxLength, and maxLines properties to MaterialTextForm…
Nailsonseat Feb 16, 2024
0d714d4
Add listerId field to LostAndFoundItem model
Nailsonseat Feb 16, 2024
79769e4
Add models and update provider in LostAndFoundProvider
Nailsonseat Feb 16, 2024
3525383
Add user models and import auth provider
Nailsonseat Feb 16, 2024
7bc0c84
Add logger and implement deleteLostAndFoundItem method
Nailsonseat Feb 16, 2024
57da56d
Add occupant name to reserveRoom method and add vacateRoom method
Nailsonseat Feb 16, 2024
c2b50b8
Updated lost and found listing info card and added delete functionality
Nailsonseat Feb 16, 2024
707df94
Added logic to vaccant room for the occupier only
Nailsonseat Feb 16, 2024
6d16a8b
Merge branch 'main' into room-vacancy-lost-and-found-two
Nailsonseat Feb 16, 2024
b69898e
Post merge cleanup
Nailsonseat Feb 16, 2024
63d7d87
Messages for room resources
Nailsonseat Feb 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading