-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3271cda
Showing
11 changed files
with
1,155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Ignore node_modules directory | ||
node_modules/ | ||
|
||
# Ignore log files | ||
*.log | ||
|
||
# Ignore environment configuration files | ||
.env | ||
.env.local | ||
|
||
# Ignore compiled code or build artifacts | ||
/dist/ | ||
/build/ | ||
|
||
# Ignore editor-specific files | ||
.vscode/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const router = require('express').Router(); | ||
const product = require('../../models/product'); | ||
// Deleting product | ||
router.delete('/product/', async (req, res) => { | ||
try { | ||
const data = await product.findByIdAndDelete(req.query); | ||
if (!data) { | ||
return res.status(404).json({ | ||
success: false, | ||
message: "Product not found with given id." | ||
}); | ||
} | ||
res.status(200).json({ | ||
success: true, | ||
message: "Product Deleted Successfully" | ||
}); | ||
} catch (err) { | ||
console.error('Error deleting product:', err.message); | ||
res.status(500).json({ success: false, message: 'Internal server error' }); | ||
} | ||
}); | ||
module.exports= router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const router = require('express').Router(); | ||
const product = require('../../models/product'); | ||
|
||
// Creating new product | ||
router.post('/product/new', async (req, res) => { | ||
try { | ||
const data = await product.create(req.body); | ||
res.status(201).json({ | ||
success: true, | ||
message: 'Product created successfully!', | ||
data | ||
}); | ||
} catch (err) { | ||
console.error('Error creating product:', err.message); | ||
res.status(500).json({ success: false, message: 'Internal server error' }); | ||
} | ||
}); | ||
|
||
module.exports= router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const router = require('express').Router(); | ||
const product = require('../../models/product'); | ||
// Read product | ||
router.get('/products', async (req, res) => { | ||
try { | ||
const data = await product.find(); | ||
res.status(200).json({ | ||
success: true, | ||
data | ||
}); | ||
} catch (err) { | ||
console.error('Error retrieving products:', err.message); | ||
res.status(500).json({ success: false, message: 'Internal server error' }); | ||
} | ||
}); | ||
module.exports= router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const router = require('express').Router(); | ||
const product = require('../../models/product'); | ||
// Updating data | ||
router.put('/product/:id', async (req, res) => { | ||
try { | ||
const data = await product.findByIdAndUpdate(req.params.id, req.body, { | ||
new: true, | ||
useFindAndModify: false, | ||
runValidators: true | ||
}); | ||
if (!data) { | ||
return res.status(404).json({ | ||
success: false, | ||
message: "Product not found." | ||
}); | ||
} | ||
res.status(200).json({ | ||
success: true, | ||
message: "Product modified successfully.", | ||
response: data | ||
}); | ||
} catch (err) { | ||
console.error('Error updating product:', err.message); | ||
res.status(500).json({ success: false, message: 'Internal server error' }); | ||
} | ||
}); | ||
module.exports= router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const router = require('express').Router(); | ||
const product = require('../../models/product'); | ||
//Api Filter Resquest Endpoint | ||
router.get('/products/filter/', async (req, res) => { | ||
try { | ||
|
||
const data = req.query.by; | ||
if (data === 'name') { | ||
try { | ||
const name = await product.distinct('name'); | ||
res.status(200).json({ | ||
count: name.length, | ||
name: name | ||
}) | ||
} catch (error) { | ||
res.status(501).json({ | ||
success:false, | ||
message:"Internal Server Error!" | ||
}) | ||
} | ||
} | ||
else if(data === 'category') | ||
{ | ||
const category = await product.distinct('category'); | ||
res.status(200).json({ | ||
count:category.length, | ||
categories: category | ||
}) | ||
} | ||
else if(data === 'price') | ||
{ | ||
const category = await product.distinct('price'); | ||
res.status(200).json({ | ||
count:category.length, | ||
categories: category | ||
}) | ||
} | ||
|
||
|
||
} | ||
catch (error) { | ||
res.status(502).json({ | ||
success: false, | ||
message: `Internal Server Error! ${error}` | ||
}) | ||
|
||
} | ||
}) | ||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require('dotenv').config(); //Loading all environment variable | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
//Importing Custom Routes | ||
const newProduct = require('./Controller/Routes/New') | ||
const readProduct = require('./Controller/Routes/Read') | ||
const updateProduct = require('./Controller/Routes/Update') | ||
const deleteProduct = require('./Controller/Routes/Delete') | ||
const filterProduct = require('./Controller/Routes/filter') | ||
//Importing Database Connection Function | ||
const connectToDb = require('./models/db'); | ||
//Function to connect to the database | ||
connectToDb(); | ||
|
||
const port= process.env.PORT || 8000; | ||
const app = express(); | ||
const route = '/api/v1' | ||
|
||
// Express Middlewares | ||
app.use(express.json()); | ||
app.use(bodyParser.urlencoded({extended:false})); | ||
app.use(route, newProduct); | ||
app.use(route, readProduct); | ||
app.use(route, updateProduct); | ||
app.use(route, deleteProduct); | ||
app.use(route, filterProduct); | ||
|
||
//Starting the server | ||
app.listen(port, ()=>{ | ||
console.log(`Server is running on http://127.0.0.1:${port}`) | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const mongoose = require('mongoose'); | ||
const connectToDb = ()=>{ | ||
const connnectionPrams ={ | ||
useNewUrlParser:true, | ||
useUnifiedTopology:true | ||
} | ||
try { | ||
mongoose.connect(process.env.DB_URL, connnectionPrams) | ||
console.log(`Successfully connected to Database`) | ||
} catch (error) { | ||
console.log(`Could not connect to the database ${error}`) | ||
|
||
} | ||
|
||
} | ||
module.exports= connectToDb; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
//Creating Schema for product | ||
const productSchema = new mongoose.Schema({ | ||
name: {type:String, required:true}, | ||
price: {type:Number}, | ||
category:{type:String, required:true}, | ||
description: {type:String, required:true} | ||
}); | ||
|
||
const product = mongoose.model('product', productSchema); | ||
|
||
module.exports= product; |
Oops, something went wrong.