Advertisement
sourav8256

Untitled

Jul 31st, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. // Import required modules
  2. const express = require('express');
  3. const bodyParser = require('body-parser');
  4. const mongoose = require('mongoose');
  5.  
  6. // Create the Express application
  7. const app = express();
  8. const PORT = process.env.PORT || 3000;
  9.  
  10. // Middleware
  11. app.use(bodyParser.json());
  12. app.use(bodyParser.urlencoded({ extended: true }));
  13.  
  14. // Connect to MongoDB using Mongoose
  15. const MONGODB_URI = 'mongodb://localhost:27017/ecommerce_db'; // Replace with your MongoDB URI
  16. mongoose.connect(MONGODB_URI, {
  17. useNewUrlParser: true,
  18. useUnifiedTopology: true,
  19. });
  20.  
  21. // Create Mongoose Schema and Model for Products
  22. const productSchema = new mongoose.Schema({
  23. name: { type: String, required: true },
  24. price: { type: Number, required: true },
  25. category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' },
  26. });
  27.  
  28. const Product = mongoose.model('Product', productSchema);
  29.  
  30. // Create Mongoose Schema and Model for Categories
  31. const categorySchema = new mongoose.Schema({
  32. name: { type: String, required: true },
  33. });
  34.  
  35. const Category = mongoose.model('Category', categorySchema);
  36.  
  37. // Create Mongoose Schema and Model for Orders
  38. const orderSchema = new mongoose.Schema({
  39. products: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Product' }],
  40. totalAmount: { type: Number, required: true },
  41. // Add other order-related fields as needed
  42. });
  43.  
  44. const Order = mongoose.model('Order', orderSchema);
  45.  
  46. // Endpoints
  47.  
  48. // Products
  49. app.get('/products', async (req, res) => {
  50. try {
  51. const products = await Product.find().populate('category', 'name');
  52. res.json(products);
  53. } catch (error) {
  54. res.status(500).json({ message: 'Error fetching products', error: error.message });
  55. }
  56. });
  57.  
  58. app.get('/products/:id', async (req, res) => {
  59. try {
  60. const product = await Product.findById(req.params.id).populate('category', 'name');
  61. if (!product) {
  62. return res.status(404).json({ message: 'Product not found' });
  63. }
  64. res.json(product);
  65. } catch (error) {
  66. res.status(500).json({ message: 'Error fetching product', error: error.message });
  67. }
  68. });
  69.  
  70. app.post('/products', async (req, res) => {
  71. try {
  72. const { name, price, category } = req.body;
  73. const product = new Product({ name, price, category });
  74. await product.save();
  75. res.status(201).json(product);
  76. } catch (error) {
  77. res.status(500).json({ message: 'Error creating product', error: error.message });
  78. }
  79. });
  80.  
  81. app.put('/products/:id', async (req, res) => {
  82. try {
  83. const { name, price, category } = req.body;
  84. const product = await Product.findByIdAndUpdate(
  85. req.params.id,
  86. { name, price, category },
  87. { new: true }
  88. ).populate('category', 'name');
  89. if (!product) {
  90. return res.status(404).json({ message: 'Product not found' });
  91. }
  92. res.json(product);
  93. } catch (error) {
  94. res.status(500).json({ message: 'Error updating product', error: error.message });
  95. }
  96. });
  97.  
  98. app.delete('/products/:id', async (req, res) => {
  99. try {
  100. const product = await Product.findByIdAndRemove(req.params.id);
  101. if (!product) {
  102. return res.status(404).json({ message: 'Product not found' });
  103. }
  104. res.json({ message: 'Product deleted successfully' });
  105. } catch (error) {
  106. res.status(500).json({ message: 'Error deleting product', error: error.message });
  107. }
  108. });
  109.  
  110. // Categories
  111. app.get('/categories', async (req, res) => {
  112. try {
  113. const categories = await Category.find();
  114. res.json(categories);
  115. } catch (error) {
  116. res.status(500).json({ message: 'Error fetching categories', error: error.message });
  117. }
  118. });
  119.  
  120. app.get('/categories/:id', async (req, res) => {
  121. try {
  122. const category = await Category.findById(req.params.id);
  123. if (!category) {
  124. return res.status(404).json({ message: 'Category not found' });
  125. }
  126. res.json(category);
  127. } catch (error) {
  128. res.status(500).json({ message: 'Error fetching category', error: error.message });
  129. }
  130. });
  131.  
  132. app.post('/categories', async (req, res) => {
  133. try {
  134. const { name } = req.body;
  135. const category = new Category({ name });
  136. await category.save();
  137. res.status(201).json(category);
  138. } catch (error) {
  139. res.status(500).json({ message: 'Error creating category', error: error.message });
  140. }
  141. });
  142.  
  143. app.put('/categories/:id', async (req, res) => {
  144. try {
  145. const { name } = req.body;
  146. const category = await Category.findByIdAndUpdate(
  147. req.params.id,
  148. { name },
  149. { new: true }
  150. );
  151. if (!category) {
  152. return res.status(404).json({ message: 'Category not found' });
  153. }
  154. res.json(category);
  155. } catch (error) {
  156. res.status(500).json({ message: 'Error updating category', error: error.message });
  157. }
  158. });
  159.  
  160. app.delete('/categories/:id', async (req, res) => {
  161. try {
  162. const category = await Category.findByIdAndRemove(req.params.id);
  163. if (!category) {
  164. return res.status(404).json({ message: 'Category not found' });
  165. }
  166. res.json({ message: 'Category deleted successfully' });
  167. } catch (error) {
  168. res.status(500).json({ message: 'Error deleting category', error: error.message });
  169. }
  170. });
  171.  
  172. // Start the server
  173. app.listen(PORT, () => {
  174. console.log(`Server is running on port ${PORT}`);
  175. });
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement