Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sraveshnandan committed Jul 26, 2023
0 parents commit 3271cda
Show file tree
Hide file tree
Showing 11 changed files with 1,155 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
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/
22 changes: 22 additions & 0 deletions Controller/Routes/Delete.js
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;
19 changes: 19 additions & 0 deletions Controller/Routes/New.js
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;
16 changes: 16 additions & 0 deletions Controller/Routes/Read.js
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;
27 changes: 27 additions & 0 deletions Controller/Routes/Update.js
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;
49 changes: 49 additions & 0 deletions Controller/Routes/filter.js
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;
33 changes: 33 additions & 0 deletions app.js
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}`)
})


16 changes: 16 additions & 0 deletions models/db.js
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;
13 changes: 13 additions & 0 deletions models/product.js
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;
Loading

0 comments on commit 3271cda

Please sign in to comment.