Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
123 changes: 123 additions & 0 deletions mern/server/controllers/records.controller.js
Original file line number Diff line number Diff line change
@@ -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");
}
};
84 changes: 0 additions & 84 deletions mern/server/routes/record.js

This file was deleted.

30 changes: 30 additions & 0 deletions mern/server/routes/record.route.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion mern/server/server.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down