Skip to content

Commit

Permalink
Messages for room resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Nailsonseat committed Feb 16, 2024
1 parent b69898e commit 63d7d87
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
5 changes: 5 additions & 0 deletions backend/constants/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ export const deleted = "Deleted";
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: 3 additions & 6 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 @@ -25,16 +26,12 @@ router.post("/", async (req, res) => {

// 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
9 changes: 4 additions & 5 deletions backend/resources/rooms/roomResource.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();

// PUT method
Expand All @@ -9,7 +10,7 @@ router.put("/:id", async (req, res) => {
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 });
}

if (vacant) {
Expand All @@ -24,11 +25,9 @@ router.put("/:id", async (req, res) => {

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

0 comments on commit 63d7d87

Please sign in to comment.