Skip to content

Commit fc44784

Browse files
finish node api version 2
0 parents  commit fc44784

File tree

10 files changed

+4481
-0
lines changed

10 files changed

+4481
-0
lines changed

.env_example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NODE_ENV=development
2+
MONGO_URL=
3+
PORT=3000
4+
FRONTEND=http://127.0.0.1:5173

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## About NODE.js API
2+
3+
This project is created to teach you how to create a Restful CRUD API with Node.js, Express and MongoDB.
4+
5+
If you want to learn how the project is developed. You can visit https://www.youtube.com/watch?v=FPYlicctQMM&list=PLbKN8A2wssqUlVHRBeJIgIvkbyrX4kR0V or https://www.youtube.com/watch?v=9OfL9H6AmhQ&feature=youtu.be
6+
7+
### API Features
8+
9+
The application can create, read, update and delete data, for example: products, in a database.

controllers/productController.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const Product = require('../models/productModel')
2+
const asyncHandler = require('express-async-handler')
3+
4+
// get all product
5+
const getProducts = asyncHandler(async(req, res) => {
6+
try {
7+
const products = await Product.find({});
8+
res.status(200).json(products);
9+
} catch (error) {
10+
res.status(500);
11+
throw new Error(error.message);
12+
}
13+
})
14+
15+
// get a single product
16+
const getProduct = asyncHandler(async(req, res) =>{
17+
try {
18+
const {id} = req.params;
19+
const product = await Product.findById(id);
20+
res.status(200).json(product);
21+
} catch (error) {
22+
res.status(500);
23+
throw new Error(error.message);
24+
}
25+
})
26+
27+
// create a product
28+
const createProduct = asyncHandler(async(req, res) => {
29+
try {
30+
const product = await Product.create(req.body)
31+
res.status(200).json(product);
32+
33+
} catch (error) {
34+
res.status(500);
35+
throw new Error(error.message);
36+
}
37+
})
38+
39+
// update a product
40+
const updateProduct = asyncHandler(async(req, res) => {
41+
try {
42+
const {id} = req.params;
43+
const product = await Product.findByIdAndUpdate(id, req.body);
44+
// we cannot find any product in database
45+
if(!product){
46+
res.status(404);
47+
throw new Error(`cannot find any product with ID ${id}`);
48+
}
49+
const updatedProduct = await Product.findById(id);
50+
res.status(200).json(updatedProduct);
51+
52+
} catch (error) {
53+
res.status(500);
54+
throw new Error(error.message);
55+
}
56+
})
57+
58+
const deleteProduct = asyncHandler(async(req, res) =>{
59+
try {
60+
const {id} = req.params;
61+
const product = await Product.findByIdAndDelete(id);
62+
if(!product){
63+
res.status(404);
64+
throw new Error(`cannot find any product with ID ${id}`);
65+
}
66+
res.status(200).json(product);
67+
68+
} catch (error) {
69+
res.status(500);
70+
throw new Error(error.message);
71+
}
72+
})
73+
74+
module.exports = {
75+
getProducts,
76+
getProduct,
77+
createProduct,
78+
updateProduct,
79+
deleteProduct
80+
}

middleware/errorMiddleware.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const errorMiddleware = (err, req, res, next) => {
2+
console.log('here is an error middleware');
3+
const statusCode = res.statusCode ? res.statusCode : 500;
4+
res.status(statusCode);
5+
res.json({message: err.message, stack: process.env.NODE_ENV === "development" ? err.stack : null});
6+
}
7+
8+
module.exports = errorMiddleware

models/productModel.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const mongoose = require('mongoose')
2+
3+
const productSchema = mongoose.Schema(
4+
{
5+
name: {
6+
type: String,
7+
required: [true, "Please enter a product name"]
8+
},
9+
quantity: {
10+
type: Number,
11+
required: true,
12+
default: 0
13+
},
14+
price: {
15+
type: Number,
16+
required: true,
17+
},
18+
image: {
19+
type: String,
20+
required: false,
21+
}
22+
},
23+
{
24+
timestamps: true
25+
}
26+
)
27+
28+
29+
const Product = mongoose.model('Product', productSchema);
30+
31+
module.exports = Product;

0 commit comments

Comments
 (0)