-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
MONGO_URI=mongodb+srv://shayanqureshi2411:[email protected]/products?retryWrites=true&w=majority&appName=Cluster0 | ||
PORT=5000 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import mongoose from "mongoose" | ||
|
||
|
||
export const ConnectDB = async() => { | ||
try { | ||
const conn= await mongoose.connect(process.env.MONGO_URI) | ||
console.log(`MongoDB Connected ${conn.connection.host}`) | ||
} catch (error) { | ||
console.error(`Error : ${error.message}`); | ||
process.exit(1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import mongoose from 'mongoose'; | ||
import Product from "../models/productmodel.js" | ||
|
||
export const getProducts = async(req,res)=>{ | ||
try { | ||
const allProducts = await Product.find({}); | ||
res.status(200).json({ success: true, data: allProducts }); | ||
} catch (error) { | ||
console.error('Error fetching products:', error.message); | ||
res.status(500).json({ success: false, message: "Server Error" }); | ||
} | ||
}; | ||
|
||
export const createNewProduct= async(req, res)=>{ | ||
const product = req.body; | ||
|
||
if (!product.name || !product.price || !product.image) { | ||
return res.status(400).json({ success: false, message: "Please provide all required fields" }); | ||
} | ||
|
||
const newProduct = new Product(product); | ||
|
||
try { | ||
await newProduct.save(); | ||
res.status(201).json({ success: true, data: newProduct }); | ||
} catch (error) { | ||
console.error("Error creating product:", error.message); | ||
res.status(500).json({ success: false, message: "Server Error" }); | ||
} | ||
}; | ||
|
||
export const deleteProduct = async(req, res)=>{ | ||
const { id } = req.params; | ||
|
||
if (!mongoose.Types.ObjectId.isValid(id)) { | ||
return res.status(404).json({ success: false, message: "Invalid product ID" }); | ||
} | ||
|
||
try { | ||
const deletedProduct = await Product.findByIdAndDelete(id); | ||
if (!deletedProduct) { | ||
return res.status(404).json({ success: false, message: "Product not found" }); | ||
} | ||
res.status(200).json({ success: true, message: "Product deleted" }); | ||
} catch (error) { | ||
console.error("Error deleting product:", error.message); | ||
res.status(500).json({ success: false, message: "Server Error" }); | ||
} | ||
}; | ||
|
||
export const updateProduct = async(req, res)=>{ | ||
const { id } = req.params; | ||
const product = req.body; | ||
|
||
if (!mongoose.Types.ObjectId.isValid(id)) { | ||
return res.status(400).json({ success: false, message: "Invalid product ID" }); | ||
} | ||
|
||
try { | ||
const updatedProduct = await Product.findByIdAndUpdate(id, product, { new: true }); | ||
if (!updatedProduct) { | ||
return res.status(404).json({ success: false, message: "Product not found" }); | ||
} | ||
res.status(200).json({ success: true, data: updatedProduct }); | ||
} catch (error) { | ||
console.error("Error updating product:", error.message); | ||
res.status(500).json({ success: false, message: "Server Error" }); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import express from "express"; | ||
import dotenv from "dotenv"; | ||
import { ConnectDB } from "./config/db.js "; | ||
import cors from 'cors'; | ||
|
||
import productRoutes from "./routes/productRoutes.js" | ||
|
||
dotenv.config(); | ||
const app = express(); | ||
|
||
app.use(cors({ origin: 'http://localhost:5173' })); | ||
const PORT=process.env.PORT || 5000 | ||
|
||
app.use(express.json()); | ||
|
||
app.use('/api/products',productRoutes) | ||
|
||
app.listen(PORT, () => { | ||
ConnectDB(); | ||
console.log("Server Started ! at http://localhost:"+PORT); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const productSchema = new mongoose.Schema({ | ||
name :{ | ||
type: String, | ||
required: true | ||
}, | ||
price:{ | ||
type:Number, | ||
required: true | ||
}, | ||
image: { | ||
type: String, | ||
required: true | ||
} | ||
}, | ||
{timestamps: true}) | ||
|
||
const Product = mongoose.model('Product',productSchema) | ||
|
||
export default Product; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.