Skip to content

Commit 45d1d8b

Browse files
committed
RESTFUL API
BASIC RESTFUL API WITH MONGODB, NODEJS AND EXPRESS
1 parent c7b8c4a commit 45d1d8b

File tree

6 files changed

+219
-7
lines changed

6 files changed

+219
-7
lines changed

controllers/productController.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const mongoose = require("mongoose");
2+
const product = require("../models/products");
3+
const productinstance = require("../models/productinstance");
4+
5+
// fetcing all documents from our db
6+
exports.get_all_products = (req, res, next) => {
7+
product.find().then(results => {
8+
res
9+
.status(200)
10+
.send({
11+
products: results
12+
})
13+
14+
}).catch(next);;
15+
};
16+
17+
// creating a new document
18+
19+
// exports.create_document = (req, res, next) => {
20+
// product.create(req.body).
21+
// then(result => {
22+
// res
23+
// .status(200)
24+
// .send({
25+
// result: result,
26+
// created: true
27+
// })
28+
29+
// }).catch(next);
30+
// };
31+
32+
exports.create_document = (req, res, next) => {
33+
product
34+
.create(req.body)
35+
.then(product => {
36+
res.status(200).send({ product: product, create: true });
37+
})
38+
.catch(next);
39+
// var product = new Products({
40+
// _id:new mongoose.Types.ObjectId(),
41+
// req:req.body
42+
// });
43+
// product.save((err)=> {
44+
// if(err) return next(err);
45+
// });
46+
};
47+
48+
// fetching a single a document details
49+
exports.get_single_product = (req, res, next) => {
50+
product.findById(req.params._id).then(product => {
51+
res
52+
.status(200)
53+
.send({
54+
product: product
55+
})
56+
57+
}).catch(next);;
58+
};
59+
60+
// dleting a single document
61+
exports.delete_single_document = (req, res, next) => {
62+
product.findByIdAndRemove({_id: req.params._id}).then(result => {
63+
res
64+
.status(200)
65+
.send({
66+
result: result,
67+
deleted: true
68+
})
69+
70+
}).catch(next);;
71+
};
72+
73+
// update a single a document
74+
exports.update_document = (req, res, next) => {
75+
product
76+
.findOneAndUpdate({ _id: req.params._id }, req.body)
77+
.then(() => {
78+
product.findOne({ _id: req.params._id }).then(result => {
79+
res
80+
.status(200)
81+
.send({
82+
result: result,
83+
update: true
84+
})
85+
.catch(next);
86+
});
87+
})
88+
.catch(next);
89+
};
90+
91+
//delete all documents
92+
93+
exports.delete_all =(req,res,next)=>{
94+
product.remove({}).then((results)=>{
95+
res.status(200).send({
96+
result:results, deleted:true
97+
})
98+
}).catch(next);
99+
}
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const mongoose = require("mongoose");
2+
const product = require("../models/products");
3+
const productinstance = require("../models/productinstance");
4+
5+
// fetcing all documents from our db
6+
exports.get_all_productinstance = (req, res, next) => {
7+
productinstance.find().populate('product').then(results => {
8+
res
9+
.status(200)
10+
.send({
11+
productsinstance: results
12+
})
13+
14+
}).catch(next);;
15+
};
16+
17+
// creating a new document
18+
19+
exports.create_instance_document = (req, res, next) => {
20+
productinstance.create(req.body).
21+
then(result => {
22+
res
23+
.status(200)
24+
.send({
25+
result: result,
26+
created: true
27+
})
28+
29+
}).catch(next);;
30+
};
31+
32+
// fetching a single a document details
33+
exports.get_single_product_instance = (req, res, next) => {
34+
productinstance.findById(req.params._id).then(instance => {
35+
res
36+
.status(200)
37+
.send({
38+
product: instance
39+
})
40+
41+
}).catch(next);;
42+
};
43+
44+
// dleting a single document
45+
exports.delete_single_document_instance = (req, res, next) => {
46+
productinstance.findByIdAndRemove({_id: req.params._id}).then(result => {
47+
res
48+
.status(200)
49+
.send({
50+
result: result,
51+
deleted: true
52+
})
53+
54+
}).catch(next);;
55+
};
56+
57+
// update a single a document
58+
exports.update_document_instance = (req, res, next) => {
59+
productinstance
60+
.findOneAndUpdate({ _id: req.params._id }, req.body)
61+
.then(() => {
62+
productinstance.findOne({ _id: req.params._id }).then(result => {
63+
res
64+
.status(200)
65+
.send({
66+
result: result,
67+
update: true
68+
})
69+
70+
})
71+
})
72+
.catch(next);
73+
};
74+
75+
76+
//delete all instance
77+
exports.delete_all =(req,res,next)=>{
78+
productinstance.remove({}).then((results)=>{
79+
res.status(200).send({
80+
result:results, deleted:true
81+
})
82+
}).catch(next);
83+
}

index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const express = require('express');
22
const mongoose = require('mongoose');
33
const bodyParser = require('body-parser');
4-
5-
const url = "mongodb://localhost:27017/RestfulApi";
4+
const url = 'mongodb://localhost:27017/RestfulApi';
65

76
mongoose.connect(url);
87

98
const app = express();
9+
app.use(bodyParser.json());
1010

1111
const productRouter = require('./routes/api');
1212

13-
app.get('api/', productRouter);
13+
app.use('/api', productRouter);
14+
1415

15-
app.use(bodyParser.json());
1616

1717
app.use((err,req,res,next)=>{
1818
console.log(err);
@@ -30,5 +30,4 @@ app.listen(PORT,(req,res,next)=>{
3030
});
3131

3232

33-
3433
module.exports=app;

models/productinstance.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Schema = mongoose.Schema;
44

55
const productSchema = new Schema({
66
status: {
7+
type:String,
78
enum: ["Finished", "Available"],
89
default: "Available",
910
required: true

models/products.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ const productSchema = new Schema({
2323
});
2424

2525
// export the model
26-
module.exports = mongoose.model("product",productSchema);
26+
module.exports = mongoose.model("product", productSchema);
2727

routes/api.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,40 @@ const productInstance = require("../controllers/productinstanceController");
55
const router = express.Router();
66

77
// route for getting all products
8-
router.get('/products', product.get_all_products)
8+
router.get('/products', product.get_all_products);
99

10+
//creating new document
11+
router.post('/products', product.create_document);
1012

13+
//get a single document route
14+
router.get('/product/:_id', product.get_single_product);
1115

16+
//update a single document route
17+
router.put('/product/:_id', product.update_document);
18+
19+
//delete a document route
20+
router.delete('/product/:_id', product.delete_single_document);
21+
22+
// delete all products
23+
router.delete('/product/', product.delete_all);
24+
25+
// route for getting all products instance
26+
router.get('/productinstance', productInstance.get_all_productinstance);
27+
28+
//creating new document instance
29+
router.post('/productinstance', productInstance.create_instance_document);
30+
31+
//get a single document route
32+
router.get('/productinstance/:_id', productInstance.get_single_product_instance);
33+
34+
//update a single document route
35+
router.put('/productinstance/:_id', productInstance.update_document_instance);
36+
37+
//delete a document route
38+
router.delete('/productinstance/:_id', productInstance.delete_single_document_instance);
39+
40+
// delete all instance route
41+
router.delete('/productinstance/', productInstance.delete_all);
1242

1343

1444
module.exports=router;

0 commit comments

Comments
 (0)