Skip to content

Commit 6c704b5

Browse files
serverless example starter update
1 parent a6aa72b commit 6c704b5

File tree

4 files changed

+4
-38
lines changed

4 files changed

+4
-38
lines changed

Controller/products.js

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,15 @@ app.get('/index', async (req, res) => {
2020

2121
// function for creating a new product
2222
app.post('/', async (req, res) => {
23-
2423
try {
25-
2624
await dbConnection();
27-
2825
const data = req.body;
29-
3026
const {name, type, description, cost} = data;
31-
32-
if(!data) {
27+
if(!data) {
3328
return "Please pass all required fields!"
3429
}
35-
3630
const dataToSave = {name,type,description,cost,productId:uuid()};
37-
3831
let createProduct = await ProductService.createProduct(dataToSave);
39-
4032
if (createProduct) {
4133
return res.status(200).send(
4234
createProduct
@@ -46,18 +38,13 @@ app.post('/', async (req, res) => {
4638
// handle errors here
4739
console.log(error, "error!!");
4840
}
49-
5041
})
5142

52-
5343
// function for getting all products
5444
app.get('/', async (req, res) => {
55-
56-
try {
45+
try {
5746
await dbConnection();
58-
5947
const allProducts = await ProductService.getAllProduct();
60-
6148
if (allProducts) {
6249
return res.status(200).send({
6350
data: allProducts
@@ -72,29 +59,19 @@ app.get('/', async (req, res) => {
7259

7360
// function for getting a product by Id
7461
app.get('/:productId/', async (req, res) => {
75-
7662
try {
77-
7863
await dbConnection();
79-
8064
const {productId} = req.params;
81-
8265
const getProduct = await ProductService.getProductById({productId});
83-
8466
if(getProduct) {
8567
return res.status(200).send({
8668
data: getProduct
8769
})
88-
8970
}
90-
9171
} catch (error) {
9272
// handle errors here
9373
console.log(error, "error!!");
94-
9574
}
96-
9775
});
9876

99-
10077
module.exports.handler = serverless(app);

Model/product.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const ProductSchema = new mongoose.Schema (
1111
{timestamps: true}
1212
);
1313

14-
const ProductModel = mongoose.model("product",ProductSchema);
14+
const ProductModel = mongoose.model("product", ProductSchema);
1515

1616
module.exports = ProductModel;
1717

Services/product.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,28 @@ const Product = require('../Model/product');
44
module.exports = {
55

66
async createProduct (product) {
7-
87
let result = await Product.create(product);
98
if(result) {
109
return {
1110
data: product,
1211
message: "Product successfully created!"
1312
};
1413
}
15-
1614
return "Error creating new product"
1715

1816
},
1917

2018

2119
async getAllProduct() {
22-
2320
let product = await Product.find();
24-
2521
if(product) return product;
26-
2722
return "Error fetching products from db"
28-
2923
},
3024

3125

3226
async getProductById(productId) {
33-
3427
let product = await Product.findOne(productId);
35-
3628
if(product) return product;
37-
3829
return "Error fetching product from db";
39-
4030
},
41-
42-
4331
};

sample.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MONGODB_URL = mongodb://localhost:27017

0 commit comments

Comments
 (0)