manascb1344 / Online-Auction-System

The Online Auction System is a comprehensive platform designed to streamline online auctions for buyers, sellers, and admins. 🎉 With features like user management, item tracking, real-time bidding, and transaction processing, it creates a seamless auction experience. Built with React.js and Node.js, it offers a responsive interface and robust back
https://dbms-project-alpha.vercel.app
2 stars 10 forks source link

Enhancement: Code Reusability: Repetitive Code Patterns Across the Codebase - Issue 9 #15

Closed HTSagara closed 1 month ago

HTSagara commented 1 month ago

Overview

The code base had some redundancy. In this PR I tried to reduce it as much as possible. Most of the changes were made in the server side.

Backend

Utils folder

In the utils folder I added three new files asyncHandler.js, dbUtils.js, and responseHandler.js

asynceHandler.js

This file exports a function called asyncHandler that wraps asynchronous route handlers in an Express.js application. It ensures that any errors occurring in asynchronous code are caught and handled properly. If an error occurs, it logs the error message and responds with a 500 Internal Server Error, or with a custom error message if provided.

// utils/asyncHandler.js
const asyncHandler =
  (fn, errorMessage = "Internal server error") =>
  (req, res, next) =>
    Promise.resolve(fn(req, res, next)).catch((error) => {
      console.error(`Error: ${error.message}`);
      res.status(500).json({ error: errorMessage });
    });

module.exports = asyncHandler;

This function was implemented in most of the routes.

dbUtils.js

This file provides utility functions for interacting with the database:

// /utils/dbUtils.js
const pool = require("../config/db");

const getBuyerById = async (buyerId) => {
  const query = "SELECT * FROM Buyers WHERE Buyer_ID = $1";
  const { rows } = await pool.query(query, [buyerId]);
  return rows.length > 0 ? rows[0] : null;
};

const getTransactionsByBuyerId = async (buyerId) => {
  const query = "SELECT * FROM Transactions WHERE Buyer_ID = $1";
  const { rows } = await pool.query(query, [buyerId]);
  return rows;
};

module.exports = {
  getBuyerById,
  getTransactionsByBuyerId,
};

responseHandler.js

This file provides utility functions to handle HTTP responses:

// utils/responseHandler.js
const send404 = (res, message) => res.status(404).json({ error: message });

const send500 = (res, message) => res.status(500).json({ error: message });

const sendSuccess = (res, data) => res.status(200).json(data);

module.exports = {
  send404,
  send500,
  sendSuccess,
};

Frontend

For the frontend the only change that I made was the suggested one in the issue description. I couldn't find the routes to create a new user so I could not login and test the rest of the frontend. So, I decided to stick with the proposed change.

AdminDashboard.jsx

I created the reusable button component that accepts the prop.

// Reusable ButtonLink component
const ButtonLink = ({ to, text }) => {
  return (
    <Link to={to} className="mr-4">
      <button className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
        {text}
      </button>
    </Link>
  );
};
vercel[bot] commented 1 month ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dbms-project ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 18, 2024 10:09pm
manascb1344 commented 1 month ago

Thanks for the awesome PR @HTSagara Really appreciate your contribution!