Skip to content

Commit

Permalink
Add My Inventory project with frontend and backend
Browse files Browse the repository at this point in the history
  • Loading branch information
MohdShayan committed Oct 3, 2024
1 parent 4ee4104 commit eeb8ffc
Show file tree
Hide file tree
Showing 1,740 changed files with 286,092 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/.env
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
12 changes: 12 additions & 0 deletions backend/config/db.js
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);
}
}
69 changes: 69 additions & 0 deletions backend/controllers/productController.js
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" });
}
};
21 changes: 21 additions & 0 deletions backend/index.js
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);
});
21 changes: 21 additions & 0 deletions backend/models/productmodel.js
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;
16 changes: 16 additions & 0 deletions backend/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/mime.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions backend/node_modules/.bin/nodemon

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/nodemon.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/nodemon.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions backend/node_modules/.bin/nodetouch

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/nodetouch.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/nodetouch.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions backend/node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions backend/node_modules/.bin/semver.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions backend/node_modules/.bin/semver.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eeb8ffc

Please sign in to comment.