Share
Explore

Full stack node.js web ecommerce app: Peanut's Pet Emporium Your ONLINE Source for bespoke, tailor made costumes for dogs

image.png
Here are 10 warm-up drills to get started with building the business logic for "Peanut's Pet Emporium":

1. Create a Basic Product Object:


const product = {
id: 1,
name: 'Super Dog Cape',
price: 19.99,
description: 'A red cape for your super dog!',
inStock: true
};
console.log(product);

2. Loop through Product Properties:


for (let key in product) {
console.log(`${key}: ${product[key]}`);
}

3. Array of Products:

const products = [
{ id: 1, name: 'Super Dog Cape', price: 19.99 },
{ id: 2, name: 'Dog Wizard Hat', price: 9.99 },
{ id: 3, name: 'Puppy Pirate Eye Patch', price: 4.99 }
];

4. Function to Find Product by ID:


function findProductById(id) {
return products.find(product => product.id === id);
}
console.log(findProductById(2));

5. Method to Add a New Product:


function addProduct(newProduct) {
products.push(newProduct);
}
addProduct({ id: 4, name: 'Canine Knight Armor', price: 49.99 });
console.log(products);

6. Loop to Display All Product Names:


products.forEach(product => {
console.log(product.name);
});

7. Try-Catch to Handle Errors:

function getProductPrice(id) {
try {
const product = findProductById(id);
if (!product) throw new Error('Product not found!');
return product.price;
} catch (error) {
console.error(error.message);
}
}
console.log(getProductPrice(5)); // This will throw an error since product with ID 5 doesn't exist.

8. Data Structure for User Cart:


const userCart = {
userId: 'user123', // we really need this to be an Object Reference
// not possible in JavaScript: Now we need TypeScript
items: [
{ productId: 1, quantity: 2 },
{ productId: 3, quantity: 1 }
]
};


Let's extend the example using TypeScript's strong typing features.

First, we'll define interfaces for the user, cart, and cart items.
Then, we'll create a user object that compositionally connects to the shopping cart.

// Define the structure for a cart iteminterface CartItem { productId: number; quantity: number;}
// Define the structure for a user cartinterface UserCart { userId: string; items: CartItem[];}
// Define the structure for a userinterface User { id: string; name: string; email: string; cart: UserCart;}
// Create a user object that includes the cartconst user: User = { id: 'user123', name: 'John Doe', email: 'john.doe@example.com', cart: { userId: 'user123', items: [ { productId: 1, quantity: 2 }, { productId: 3, quantity: 1 } ] }};
console.log(user);
In this example:
We've defined interfaces for CartItem, UserCart, and User.
The User interface compositionally includes a UserCart.
We've created a user object that adheres to the User interface and includes the cart data.
This approach ensures that our data structures are strongly typed, making it easier to catch potential type-related issues at compile-time.

9. Function to Calculate Total Cart Value:


function calculateCartTotal(cart) {
return cart.items.reduce((total, item) => {
const product = findProductById(item.productId);
return total + (product.price * item.quantity);
}, 0);
}
console.log(calculateCartTotal(userCart));

10. Method to Add an Item to the Cart:


function addItemToCart(cart, productId, quantity) {
const existingItem = cart.items.find(item => item.productId === productId);
if (existingItem) {
existingItem.quantity += quantity;
} else {
cart.items.push({ productId, quantity });
}
}
addItemToCart(userCart, 2, 1);
console.log(userCart);


These drills progressively introduce various JavaScript and Node.js concepts, building towards the business logic for "Peanut's Pet Emporium".
They can serve as a foundation, and you can expand upon them to create more complex functionalities for the e-commerce app.

Here's a CSV representation of 25 product records for "Peanut's Pet Emporium":


id,name,price,description,in_stock1,Super Dog Cape,19.99,A red cape for your super dog!,true2,Dog Wizard Hat,9.99,Magical hat for spell-casting pups!,true
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.