Order Management System — Node.js API Structure
Navigate article by sections
🚇 src/api/routes/orders.js is the entry point for all order‑related API activity in a modern Node.js application. Whenever a client interacts with the system to create, update, retrieve, or delete an order, the request passes through this route file before reaching the controller and business‑logic layers.
Every request that deals with creating, updating, retrieving, or deleting an order passes through this route file before reaching the controller and business‑logic layers.
In a well‑structured backend architecture, the routes directory acts as the public-facing gateway of the API.
Each file inside it defines a specific domain of functionality — in this case, orders.
src/api/routes/orders.js connects to controllers and models is the foundation of designing clean, maintainable, and scalable backend systems.
This article breaks down how the orders route integrates with controllers, models, and the overall API workflow.
Order Class
class Order {
constructor(id, customerName, items = []) {
this.id = id;
this.customerName = customerName;
this.items = items; // Array of item objects
this.createdAt = new Date();
this.updatedAt = new Date();
}
}
This is the blueprint for an order. Every time you create a new order, it follows this structure — it has an ID, a customer name, a list of items, and timestamps.
Think of this like a blank invoice template. Every time a customer buys something, you fill in this template with their details.
Order Store
const orders = [];
This is where all orders are kept temporarily — like a simple list. In a real system, this could be a database.
Imagine a tray on a counter where all new order receipts are placed before being processed.
Add Orders
function addOrder(order) {
if (!(order instanceof Order)) {
throw new Error("addOrder() expects an instance of Order.");
}
orders.push(order);
return order;
}
This function adds a new order to the list. It first checks if the order is valid, then stores it.
Like a cashier checking if a receipt is properly filled before placing it in the tray.
Update Orders
function updateOrder(orderId, updatedDetails = {}) {
const index = orders.findIndex(order => order.id === orderId);
if (index === -1) {
throw new Error(`Order with ID ${orderId} not found.`);
}
Object.assign(orders[index], updatedDetails, {
updatedAt: new Date()
});
return orders[index];
}
This function finds an order by its ID and updates only the fields you specify — without deleting the rest.
Like editing a customer’s receipt when they add or remove an item — you don’t rewrite the whole receipt, just adjust the parts that changed.
Delete Orders
function deleteOrder(orderId) {
const index = orders.findIndex(order => order.id === orderId);
if (index === -1) {
throw new Error(`Order with ID ${orderId} not found.`);
}
const removedOrder = orders.splice(index, 1)[0];
return removedOrder;
}
This removes an order from the list completely.
Like taking a receipt out of the tray and throwing it away because the order was canceled.
Retrieve Orders
function getAllOrders() {
return [...orders]; // Return a safe copy
}
function getOrderById(orderId) {
return orders.find(order => order.id === orderId) || null;
}
These functions let you view all orders or find a specific one by its ID.
Like flipping through all receipts or searching for one specific customer’s receipt.
No comments:
Post a Comment