OpenLake / OpenLake/Student_Database_COSA

[FOSSOVERFLOW-25] Refactor: Backend- Migrate Logic from Routes to Controllers

Open
#226 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backend Foss OverFlow good first issue refactor
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
  1. Cleaner Routes: Route files should only contain the path, the HTTP method, middleware (auth, uploads), and a reference to the controller function.
  2. Modular Controllers: Logic for each resource (User, Event, Position, etc.) should be encapsulated in a corresponding controller file.
  3. 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):
  1. 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 });
  }
};
  1. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.