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 t…


This content originally appeared on DEV Community and was authored by Henrique Sagara

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;

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 };

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.


This content originally appeared on DEV Community and was authored by Henrique Sagara


Print Share Comment Cite Upload Translate Updates
APA

Henrique Sagara | Sciencx (2024-10-21T00:13:30+00:00) Hacktoberfest week 3- Online Auction System. Retrieved from https://www.scien.cx/2024/10/21/hacktoberfest-week-3-online-auction-system/

MLA
" » Hacktoberfest week 3- Online Auction System." Henrique Sagara | Sciencx - Monday October 21, 2024, https://www.scien.cx/2024/10/21/hacktoberfest-week-3-online-auction-system/
HARVARD
Henrique Sagara | Sciencx Monday October 21, 2024 » Hacktoberfest week 3- Online Auction System., viewed ,<https://www.scien.cx/2024/10/21/hacktoberfest-week-3-online-auction-system/>
VANCOUVER
Henrique Sagara | Sciencx - » Hacktoberfest week 3- Online Auction System. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/21/hacktoberfest-week-3-online-auction-system/
CHICAGO
" » Hacktoberfest week 3- Online Auction System." Henrique Sagara | Sciencx - Accessed . https://www.scien.cx/2024/10/21/hacktoberfest-week-3-online-auction-system/
IEEE
" » Hacktoberfest week 3- Online Auction System." Henrique Sagara | Sciencx [Online]. Available: https://www.scien.cx/2024/10/21/hacktoberfest-week-3-online-auction-system/. [Accessed: ]
rf:citation
» Hacktoberfest week 3- Online Auction System | Henrique Sagara | Sciencx | https://www.scien.cx/2024/10/21/hacktoberfest-week-3-online-auction-system/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.