diff --git a/mern/server/controllers/records.controller.js b/mern/server/controllers/records.controller.js new file mode 100644 index 0000000..1b73917 --- /dev/null +++ b/mern/server/controllers/records.controller.js @@ -0,0 +1,123 @@ +// This will help us connect to the database +import db from "../db/connection.js"; + +// This help convert the id from string to ObjectId for the _id. +import { ObjectId } from "mongodb"; + +// Controller function to fetch all records +export const getAllRecords = async (req, res) => { + try { + // Get the "records" collection + let collection = await db.collection("records"); + + // Fetch all documents from the collection and convert them to an array + let results = await collection.find({}).toArray(); + + // Send the results as a JSON response with a success status code + res.status(200).send(results); + } catch (error) { + // Handle errors and send an error response with a 500 status code + res.status(500).send({ message: "Error fetching records", error }); + } +}; + +// Controller function to fetch a single record by its ID +export const getRecordById = async (req, res) => { + try { + // Get the "records" collection + let collection = await db.collection("records"); + + // Construct a query to find a record by its ObjectId + let query = { _id: new ObjectId(req.params.id) }; + + // Find the record matching the query + let result = await collection.findOne(query); + + // If no record is found, send a "Not found" response with a 404 status code + if (!result) { + res.status(404).send("Not found"); + } else { + // Send the found record as a JSON response with a success status code + res.status(200).send(result); + } + } catch (error) { + // Handle errors and send an error response with a 500 status code + console.error(err); + res.status(500).send({ message: "Error fetching record", error }); + } +}; + +// Controller function to create a new record +export const createRecord = async (req, res) => { + try { + // Create a new document based on the request body + let newDocument = { + name: req.body.name, + position: req.body.position, + level: req.body.level, + }; + + // Get the "records" collection + let collection = await db.collection("records"); + + // Insert the new document into the collection + let result = await collection.insertOne(newDocument); + + // Send the result of the insertion as a JSON response with a success status code + res.status(201).send(result); + } catch (err) { + // Handle errors and send an error response with a 500 status code + console.error(err); + res.status(500).send("Error adding record"); + } +}; + +// Controller function to update a record by its ID +export const updateRecordById = async (req, res) => { + try { + // Construct a query to find a record by its ObjectId + const query = { _id: new ObjectId(req.params.id) }; + + // Define updates to be applied to the record + const updates = { + $set: { + name: req.body.name, + position: req.body.position, + level: req.body.level, + }, + }; + // Get the "records" collection + let collection = await db.collection("records"); + + // Update the record matching the query with the specified updates + let result = await collection.updateOne(query, updates); + + // Send the result of the update operation as a JSON response with a success status code + res.status(200).send(result); + } catch (err) { + // Handle errors and send an error response with a 500 status code + console.error(err); + res.status(500).send("Error updating record"); + } +}; + +// Controller function to delete a record by its ID +export const deleteRecordById = async (req, res) => { + try { + // Construct a query to find a record by its ObjectId + const query = { _id: new ObjectId(req.params.id) }; + + // Get the "records" collection + const collection = db.collection("records"); + + // Delete the record matching the query + let result = await collection.deleteOne(query); + + // Send the result of the deletion as a JSON response with a success status code + res.status(200).send(result); + } catch (err) { + // Handle errors and send an error response with a 500 status code + console.error(err); + res.status(500).send("Error deleting record"); + } +}; diff --git a/mern/server/routes/record.js b/mern/server/routes/record.js deleted file mode 100644 index c492c96..0000000 --- a/mern/server/routes/record.js +++ /dev/null @@ -1,84 +0,0 @@ -import express from "express"; - -// This will help us connect to the database -import db from "../db/connection.js"; - -// This help convert the id from string to ObjectId for the _id. -import { ObjectId } from "mongodb"; - -// router is an instance of the express router. -// We use it to define our routes. -// The router will be added as a middleware and will take control of requests starting with path /record. -const router = express.Router(); - -// This section will help you get a list of all the records. -router.get("/", async (req, res) => { - let collection = await db.collection("records"); - let results = await collection.find({}).toArray(); - res.send(results).status(200); -}); - -// This section will help you get a single record by id -router.get("/:id", async (req, res) => { - let collection = await db.collection("records"); - let query = { _id: new ObjectId(req.params.id) }; - let result = await collection.findOne(query); - - if (!result) res.send("Not found").status(404); - else res.send(result).status(200); -}); - -// This section will help you create a new record. -router.post("/", async (req, res) => { - try { - let newDocument = { - name: req.body.name, - position: req.body.position, - level: req.body.level, - }; - let collection = await db.collection("records"); - let result = await collection.insertOne(newDocument); - res.send(result).status(204); - } catch (err) { - console.error(err); - res.status(500).send("Error adding record"); - } -}); - -// This section will help you update a record by id. -router.patch("/:id", async (req, res) => { - try { - const query = { _id: new ObjectId(req.params.id) }; - const updates = { - $set: { - name: req.body.name, - position: req.body.position, - level: req.body.level, - }, - }; - - let collection = await db.collection("records"); - let result = await collection.updateOne(query, updates); - res.send(result).status(200); - } catch (err) { - console.error(err); - res.status(500).send("Error updating record"); - } -}); - -// This section will help you delete a record -router.delete("/:id", async (req, res) => { - try { - const query = { _id: new ObjectId(req.params.id) }; - - const collection = db.collection("records"); - let result = await collection.deleteOne(query); - - res.send(result).status(200); - } catch (err) { - console.error(err); - res.status(500).send("Error deleting record"); - } -}); - -export default router; diff --git a/mern/server/routes/record.route.js b/mern/server/routes/record.route.js new file mode 100644 index 0000000..e22f35c --- /dev/null +++ b/mern/server/routes/record.route.js @@ -0,0 +1,30 @@ +import express from "express"; +import { + createRecord, // Importing controller function to create a new record + deleteRecordById, // Importing controller function to delete a record by ID + getAllRecords, // Importing controller function to fetch all records + getRecordById, // Importing controller function to fetch a record by ID + updateRecordById, // Importing controller function to update a record by ID +} from "../controllers/records.controller.js"; + +// router is an instance of the express router. +// We use it to define our routes. +// The router will be added as a middleware and will take control of requests starting with path /record. +const router = express.Router(); + +// Route to fetch all records +router.get("/", getAllRecords); + +// Route to fetch a single record by its ID +router.get("/:id", getRecordById); + +// Route to create a new record +router.post("/", createRecord); + +// Route to update a record by its ID +router.patch("/:id", updateRecordById); + +// Route to delete a record by its ID +router.delete("/:id", deleteRecordById); + +export default router; diff --git a/mern/server/server.js b/mern/server/server.js index d1d6b4c..e182dce 100644 --- a/mern/server/server.js +++ b/mern/server/server.js @@ -1,6 +1,6 @@ import express from "express"; import cors from "cors"; -import records from "./routes/record.js"; +import records from "./routes/record.route.js"; const PORT = process.env.PORT || 5050; const app = express();