const mongoose = require('mongoose');
const itemSchema = new mongoose.Schema({
name: {
type: String,
required: true // Ensures name is provided
},
price: {
type: mongoose.Types.Decimal128,
required: true // Ensures price is provided
}
});
module.exports = mongoose.model('Item', itemSchema);
const mongoose = require('mongoose');
const itemSchema = new mongoose.Schema({
name: {
type: String,
required: true // Ensures name is provided
},
price: {
type: Number,
required: true // Ensures price is provided
}
});
module.exports = mongoose.model('Item', itemSchema);
// Add this endpoint to get the total
router.get('/total', async (req, res) => {
try {
const items = await Item.find();
const total = items.reduce((acc, item) => acc + item.price, 0); // Calculate the total
res.json({ total: total.toFixed(2) }); // Send back a nicely formatted string
} catch (error) {
res.status(500).send(error);
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping List</title>
<style>
body {
background: floralwhite;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.shopping-total {
color: chartreuse;
font-size: 1.5em;
font-weight: bold;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
}
.form-add {
background: pink;
padding: 20px;
border-radius: 8px;
}
button {
background: orange;
border: none;
padding: 10px 20px;
color: white;
border-radius: 5px;
cursor: pointer;
}
.item-list {
margin-top: 20px;
}
.item {
background: lightyellow;
border: 1px solid orange;
margin-bottom: 10px;
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>Shopping List</h1>
<!-- Total Amount Display -->
<div id="total" class="shopping-total">Total: $0.00</div>