Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Import required modules
- const express = require('express');
- const bodyParser = require('body-parser');
- const mongoose = require('mongoose');
- // Create the Express application
- const app = express();
- const PORT = process.env.PORT || 3000;
- // Middleware
- 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,
- });
- // Create Mongoose Schema and Model for Products
- 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);
- // Create Mongoose Schema and Model for Categories
- const categorySchema = new mongoose.Schema({
- name: { type: String, required: true },
- });
- const Category = mongoose.model('Category', categorySchema);
- // Create Mongoose Schema and Model for Orders
- const orderSchema = new mongoose.Schema({
- products: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Product' }],
- totalAmount: { type: Number, required: true },
- // Add other order-related fields as needed
- });
- const Order = mongoose.model('Order', orderSchema);
- // Endpoints
- // Products
- app.get('/products', 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 });
- }
- });
- app.get('/products/: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 });
- }
- });
- app.post('/products', 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 });
- }
- });
- app.put('/products/: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 });
- }
- });
- app.delete('/products/: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 });
- }
- });
- // Categories
- app.get('/categories', 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 });
- }
- });
- app.get('/categories/: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 });
- }
- });
- app.post('/categories', 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 });
- }
- });
- app.put('/categories/: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 });
- }
- });
- app.delete('/categories/: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 });
- }
- });
- // Start the server
- app.listen(PORT, () => {
- console.log(`Server is running on port ${PORT}`);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement