OpenLake / OpenLake/Student_Database_COSA
[FOSSOVERFLOW-25] Refactor: Backend- Migrate Logic from Routes to Controllers
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 13
- Forks
- 58
- Avg merge
- 20h 27m
- Merged PRs (30d)
- 3
Description
NOTE: THIS ISSUE IS FOR FOSSOVERFLOW MENTEE
Currently, most of the backend logic (database queries, validation, and response handling) is implemented directly within the route handlers ( in backend/routes/). This makes the routes bulky and harder to test or reuse.
This task is to refactor the backend to follow the Controller Pattern. We want to extract the logic into dedicated controller files, leaving the routes responsible only for defining endpoints and middleware chains.
There should be NO changes to the logic itself. This is a structural refactor only.
Objectives
- Cleaner Routes: Route files should only contain the path, the HTTP method, middleware (auth, uploads), and a reference to the controller function.
- Modular Controllers: Logic for each resource (User, Event, Position, etc.) should be encapsulated in a corresponding controller file.
- Improved Readability: Separating "how we find the data" from "where the endpoint is" makes the codebase easier for new contributors to understand.
Implementation Pattern
Current State (Bloated Route):
// routes/eventRoutes.js
router.post("/add-event", async (req, res) => {
try {
const newEvent = new Event(req.body);
await newEvent.save();
res.status(201).json(newEvent);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
Desired State (Refactored):
- Create the Controller:
// controllers/eventController.js
const { Event } = require("../models/schemas");
exports.createEvent = async (req, res) => {
try {
const newEvent = new Event(req.body);
await newEvent.save();
res.status(201).json(newEvent);
} catch (error) {
res.status(400).json({ error: error.message });
}
};
- Update the Route:
// routes/eventRoutes.js
const eventController = require("../controllers/eventController");
router.post("/add-event", eventController.createEvent);
Task Checklist
[ ] Identify all routes currently containing async (req, res) => { ... } blocks.
[ ] Create corresponding controller files in the controllers/ directory:
[ ] Move the logic from each route into a named exported function in the controller.
[ ] Update the route files to import the controllers and call the appropriate functions.
[ ] Verify: Ensure all API endpoints still function exactly as before (test via Postman or the frontend).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inventorying the async route handlers in backend/routes/, then compare routes/eventRoutes.js with the controller pattern and models/schemas reference shown in the issue. Move each handler into a named controller, leaving routes with paths, methods, middleware, and controller references. Verify that endpoints behave unchanged using Postman or the frontend.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, node.js
- Domain
- api, backend, databases
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100