DEV Community

Cover image for Hacktoberfest week 3- Online Auction System
Henrique Sagara
Henrique Sagara

Posted on

Hacktoberfest week 3- Online Auction System

Overview

During Week 3 of Hacktoberfest, I decided to contribute to a smaller but promising project: an Online Auction System. Though the project is still in its early stages, it already shows potential for growth, and I saw an opportunity to help improve its codebase. My task was to refactor the project by reducing redundant code and improving overall structure, making it more maintainable and scalable.

My task

The core objective of my task was to identify repetitive code patterns and refactor them to make the codebase cleaner and easier to maintain. Code redundancy can lead to inconsistencies, make future updates harder, and increase the likelihood of bugs. By streamlining the code, I aimed to improve efficiency without altering functionality.

Key Changes:

1. Centralized Asynchronous Error Handling: One of the common issues in Express.js projects is repetitive try-catch blocks for handling errors in asynchronous routes. To address this, I introduced a utility function called asyncHandler that wraps all async functions, ensuring that any errors are automatically caught and handled consistently across the app.

// 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;
Enter fullscreen mode Exit fullscreen mode

This change not only removed redundant try-catch blocks but also made the code cleaner and more focused on the business logic, rather than error handling.

2. Database Utility Functions: The project had several routes that directly interacted with the database, often repeating similar queries. To reduce redundancy, I created a set of reusable utility functions to handle common database queries, such as fetching a buyer's information or retrieving transactions.

// utils/dbUtils.js
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 };
Enter fullscreen mode Exit fullscreen mode

Now, instead of writing SQL queries directly in each route, the codebase can rely on these utility functions, reducing duplication and making the logic easier to manage.

3. Unified Response Handling: Another area of improvement was response handling. Different routes had inconsistent patterns for sending success and error responses. To standardize this, I introduced utility functions like send404, send500, and sendSuccess.

Discussion

Since the project is still in its early stages, I noticed some missing routes like POST, DELETE, and PUT that I could have easily create. However, I decided to stick with only my Issue was asking.

Let me know in the comments, how do you usually deal with this kind of situations that you could contribute more...Do you open a new issue, do you just create it and do a Pull Request? I would love to read you experience.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay