Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // routes/products.js
- const express = require('express');
- const router = express.Router();
- const Product = require('../models/product');
- // Define routes for products
- router.get('/', async (req, res) => {
- try {
- const products = await Product.find().populate('category', 'name');
- res.json(products);
- } catch (error) {
- res.status(500).json({ message: 'Error fetching products', error: error.message });
- }
- });
- router.get('/:id', async (req, res) => {
- try {
- const product = await Product.findById(req.params.id).populate('category', 'name');
- if (!product) {
- return res.status(404).json({ message: 'Product not found' });
- }
- res.json(product);
- } catch (error) {
- res.status(500).json({ message: 'Error fetching product', error: error.message });
- }
- });
- router.post('/', async (req, res) => {
- try {
- const { name, price, category } = req.body;
- const product = new Product({ name, price, category });
- await product.save();
- res.status(201).json(product);
- } catch (error) {
- res.status(500).json({ message: 'Error creating product', error: error.message });
- }
- });
- router.put('/:id', async (req, res) => {
- try {
- const { name, price, category } = req.body;
- const product = await Product.findByIdAndUpdate(
- req.params.id,
- { name, price, category },
- { new: true }
- ).populate('category', 'name');
- if (!product) {
- return res.status(404).json({ message: 'Product not found' });
- }
- res.json(product);
- } catch (error) {
- res.status(500).json({ message: 'Error updating product', error: error.message });
- }
- });
- router.delete('/:id', async (req, res) => {
- try {
- const product = await Product.findByIdAndRemove(req.params.id);
- if (!product) {
- return res.status(404).json({ message: 'Product not found' });
- }
- res.json({ message: 'Product deleted successfully' });
- } catch (error) {
- res.status(500).json({ message: 'Error deleting product', error: error.message });
- }
- });
- module.exports = router;
- // routes/categories.js
- const express = require('express');
- const router = express.Router();
- const Category = require('../models/category');
- // Define routes for categories
- router.get('/', async (req, res) => {
- try {
- const categories = await Category.find();
- res.json(categories);
- } catch (error) {
- res.status(500).json({ message: 'Error fetching categories', error: error.message });
- }
- });
- router.get('/:id', async (req, res) => {
- try {
- const category = await Category.findById(req.params.id);
- if (!category) {
- return res.status(404).json({ message: 'Category not found' });
- }
- res.json(category);
- } catch (error) {
- res.status(500).json({ message: 'Error fetching category', error: error.message });
- }
- });
- router.post('/', async (req, res) => {
- try {
- const { name } = req.body;
- const category = new Category({ name });
- await category.save();
- res.status(201).json(category);
- } catch (error) {
- res.status(500).json({ message: 'Error creating category', error: error.message });
- }
- });
- router.put('/:id', async (req, res) => {
- try {
- const { name } = req.body;
- const category = await Category.findByIdAndUpdate(
- req.params.id,
- { name },
- { new: true }
- );
- if (!category) {
- return res.status(404).json({ message: 'Category not found' });
- }
- res.json(category);
- } catch (error) {
- res.status(500).json({ message: 'Error updating category', error: error.message });
- }
- });
- router.delete('/:id', async (req, res) => {
- try {
- const category = await Category.findByIdAndRemove(req.params.id);
- if (!category) {
- return res.status(404).json({ message: 'Category not found' });
- }
- res.json({ message: 'Category deleted successfully' });
- } catch (error) {
- res.status(500).json({ message: 'Error deleting category', error: error.message });
- }
- });
- module.exports = router;
- // routes/cart.js
- const express = require('express');
- const router = express.Router();
- const CartItem = require('../models/cartItem');
- // Define routes for the cart
- router.get('/', async (req, res) => {
- try {
- // Logic to fetch the current user's shopping cart contents
- // You may need to implement authentication and identify the current user
- const cartItems = await CartItem.find().populate('product', 'name price');
- res.json(cartItems);
- } catch (error) {
- res.status(500).json({ message: 'Error fetching cart items', error: error.message });
- }
- });
- router.post('/:productId', async (req, res) => {
- try {
- const productId = req.params.productId;
- // Logic to add the product with productId to the shopping cart
- // You may need to implement authentication and identify the current user
- const cartItem = new CartItem({ product: productId, quantity: 1 });
- await cartItem.save();
- res.status(201).json(cartItem);
- } catch (error) {
- res.status(500).json({ message: 'Error adding product to the cart', error: error.message });
- }
- });
- router.put('/:productId', async (req, res) => {
- try {
- const productId = req.params.productId;
- const quantity = req.body.quantity;
- // Logic to update the quantity of a product in the shopping cart
- // You may need to implement authentication and identify the current user
- const cartItem = await CartItem.findOneAndUpdate(
- { product: productId },
- { quantity },
- { new: true }
- ).populate('product', 'name price');
- if (!cartItem) {
- return res.status(404).json({ message: 'Product not found in the cart' });
- }
- res.json(cartItem);
- } catch (error) {
- res.status(500).json({ message: 'Error updating cart item', error: error.message });
- }
- });
- router.delete('/:productId', async (req, res) => {
- try {
- const productId = req.params.productId;
- // Logic to remove a product from the shopping cart
- // You may need to implement authentication and identify the current user
- const cartItem = await CartItem.findOneAndRemove({ product: productId }).populate(
- 'product',
- 'name'
- );
- if (!cartItem) {
- return res.status(404).json({ message: 'Product not found in the cart' });
- }
- res.json({ message: 'Product removed from the cart successfully' });
- } catch (error) {
- res.status(500).json({ message: 'Error removing product from the cart', error: error.message });
- }
- });
- module.exports = router;
- // app.js
- const express = require('express');
- const bodyParser = require('body-parser');
- const mongoose = require('mongoose');
- const app = express();
- const PORT = process.env.PORT || 3000;
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: true }));
- // Connect to MongoDB using Mongoose
- const MONGODB_URI = 'mongodb://localhost:27017/ecommerce_db'; // Replace with your MongoDB URI
- mongoose.connect(MONGODB_URI, {
- useNewUrlParser: true,
- useUnifiedTopology: true,
- });
- // Include the products, categories, and cart routes
- const productsRoutes = require('./routes/products');
- const categoriesRoutes = require('./routes/categories');
- const cartRoutes = require('./routes/cart');
- app.use('/products', productsRoutes);
- app.use('/categories', categoriesRoutes);
- app.use('/cart', cartRoutes);
- // Start the server
- app.listen(PORT, () => {
- console.log(`Server is running on port ${PORT}`);
- });
- Models
- // models/product.js
- const mongoose = require('mongoose');
- const productSchema = new mongoose.Schema({
- name: { type: String, required: true },
- price: { type: Number, required: true },
- category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' },
- });
- const Product = mongoose.model('Product', productSchema);
- module.exports = Product;
- // models/category.js
- const mongoose = require('mongoose');
- const categorySchema = new mongoose.Schema({
- name: { type: String, required: true },
- });
- const Category = mongoose.model('Category', categorySchema);
- module.exports = Category;
- // models/cartItem.js
- const mongoose = require('mongoose');
- const cartItemSchema = new mongoose.Schema({
- product: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true },
- quantity: { type: Number, default: 1 },
- // Add other cart item-related fields as needed
- });
- const CartItem = mongoose.model('CartItem', cartItemSchema);
- module.exports = CartItem;
- - node_modules/
- - routes/
- - products.js
- - categories.js
- - cart.js
- - models/
- - product.js
- - category.js
- - cartItem.js
- - app.js
- - package.json
- - package-lock.json
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement