Skip to content

Commit cedb56f

Browse files
authored
Video-36-Product-Update-Backend (#36)
1 parent 85b159e commit cedb56f

4 files changed

Lines changed: 140 additions & 40 deletions

File tree

README.md

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
JS AMAZONA
1+
# JS AMAZONA
2+
3+
Welcome to my coding course to build an ecommerce website like amazon. In this course you will learn the essential tools and skills to design, develop and deploy a fully-function website like amazon using Vanilla JavaScript, HTML5 and CSS3 in frontend and Node and MongoDB in backend.
4+
5+
My name is Basir and I’ll be your instructor in this course. After 17 years of coding in international companies like ROI Vision in Montreal, I found my passion in teaching programming. For the last 5 years I have been tutoring 100 thousands students over my youtube channel, udemy courses and zoom online classes.
6+
7+
I designed this course for anyone seeking to develop a fully-functional ecommerce website like amazon. By the end of this course you’ll be able to design a responsive web template, implement a user-friendly frontend and build a scalable backend. Also you can deploy your website on cloud servers like Heroku and connect it to payment gateways like PayPal.
8+
9+
You need to open a code editor along with me and start coding throughout this course. I teach you:
10+
11+
- Web Design using HTML5, CSS3 including Semantic Elements, Flexbox, Grid System and Response Design.
12+
- Frontend Development by Pure JavaScript including ES6 Syntax, Rendering System, Single Page App, Libraries for Date Time, drawing chart and etc.
13+
- Backend Development using Node and MongoDB consist of ExpressJS, JWT Authentication, Mongoose object data modeling and more.
14+
15+
This course is for non-coders or juniors who want to be a professional web developer to get a job in 22 million job opportunities around the world. No requirement is necessary for this course and having a passion for coding is enough.
16+
17+
Feel free to take a look at the course preview and enroll if it is along with your ambitions.
18+
19+
# Table Of Content
220

321
1. Create Folder Structure
422
1. create root folder as jsamazona
@@ -174,26 +192,26 @@ JS AMAZONA
174192
8. create userRoute
175193
9. create createadmin route
176194
20. Sign-in Screen UI
177-
2. create SigninScreen
178-
3. render email and password fields
179-
4. style signin form
195+
1. create SigninScreen
196+
2. render email and password fields
197+
3. style signin form
180198
21. Sign-in Screen Backend
181-
3. create signin api in backend
182-
4. create route for /api/users/signin
183-
5. create check user name and password
184-
6. if it is not ok the return 401 error
185-
7. install express-async-handler
186-
8. wrap it in expressAsyncHandler
187-
9. add error middleware in server.js
188-
10. install Postman
189-
11. send post request
190-
12. test with invalid user password
191-
13. otherwise generate token
192-
14. install jsonwebtoken
193-
15. set config.JWT_SECRET to somethingsecret
194-
16. add generateToken to utils.js
195-
17. return token
196-
18. test with correct user and password
199+
1. create signin api in backend
200+
2. create route for /api/users/signin
201+
3. create check user name and password
202+
4. if it is not ok the return 401 error
203+
5. install express-async-handler
204+
6. wrap it in expressAsyncHandler
205+
7. add error middleware in server.js
206+
8. install Postman
207+
9. send post request
208+
10. test with invalid user password
209+
11. otherwise generate token
210+
12. install jsonwebtoken
211+
13. set config.JWT_SECRET to somethingsecret
212+
14. add generateToken to utils.js
213+
15. return token
214+
16. test with correct user and password
197215
22. Sign-in Screen Action
198216
1. after_render handle form submit
199217
2. create signin request in frontend
@@ -285,15 +303,19 @@ JS AMAZONA
285303
4. call create product function in ProductListScreen
286304
5. redirect to edit product
287305
35. Edit Product UI
288-
2. create ProductEditScreen.js
289-
3. load product data from backend
290-
4. handle form submit
291-
5. save product in backend
292-
36. Delete Product
293-
3. update ProductListScreen.js
294-
4. handle delete button
295-
5. rerender after deletion
296-
37. Upload Product Image
306+
1. create ProductEditScreen.js
307+
2. load product data from backend
308+
3. handle form submit
309+
4. save product in backend
310+
36. Edit Product Backend
311+
1. handle form submit
312+
2. create updateProduct
313+
3. save product in backend
314+
37. Delete Product
315+
1. update ProductListScreen.js
316+
2. handle delete button
317+
3. rerender after deletion
318+
38. Upload Product Image
297319
1. npm install multer
298320
2. create routes/uploadRoute.js
299321
3. import express and multer
@@ -310,43 +332,43 @@ JS AMAZONA
310332
14. call uploadProductImage()
311333
15. create uploadProductImage in api.js
312334
16. update server.js
313-
38. Build Project
335+
39. Build Project
314336
1. create build script for frontend
315337
2. create build script for backend
316338
3. update sever.js to serve frontend build folder and uploads folder
317339
4. stop running frontend
318340
5. npm run build
319341
6. check localhost:5000 for running website and showing images
320-
39. Show Categories In Sidebar Menu
342+
40. Show Categories In Sidebar Menu
321343
1. update ProductListScreen.js
322344
2. handle delete button
323345
3. rerender after deletion
324-
40. Admin Orders
346+
41. Admin Orders
325347
1. create Admin Order menu in header
326348
2. create AdminOrder.js
327349
3. load orders from backend
328350
4. list them in the screen
329351
5. show delete and edit button
330352
6. redirect to order details on edit action
331-
41. Edit Order
353+
42. Edit Order
332354
1. if order is payed show deliver button for admin
333355
2. handle click on deliver button
334356
3. set state to delivered
335-
42. Delete Order
357+
43. Delete Order
336358
1. update OrderListScreen.js
337359
2. handle delete button
338360
3. rerender after deletion
339-
43. Show Summary Report in Dashboard
361+
44. Show Summary Report in Dashboard
340362
1. create summary section
341363
2. style summary
342364
3. create summary backend
343365
4. create getSummary in api.js
344366
5. load data in dashboard screen
345367
6. show 3 boxes for Users, Orders and Sales
346-
44. Show Chart in Dashboard
368+
45. Show Chart in Dashboard
347369
1. import chartist
348370
2. add chartist css to index.html
349371
3. create linear chart for daily sales
350372
4. create pie chart for product categories
351-
45. Publish heroku
373+
46. Publish heroku
352374
1. publish steps

backend/routers/productRouter.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,30 @@ productRouter.post(
4141
}
4242
})
4343
);
44+
productRouter.put(
45+
'/:id',
46+
isAuth,
47+
isAdmin,
48+
expressAysncHandler(async (req, res) => {
49+
const productId = req.params.id;
50+
const product = await Product.findById(productId);
51+
if (product) {
52+
product.name = req.body.name;
53+
product.price = req.body.price;
54+
product.image = req.body.image;
55+
product.brand = req.body.brand;
56+
product.category = req.body.category;
57+
product.countInStock = req.body.countInStock;
58+
product.description = req.body.description;
59+
const updatedProduct = await product.save();
60+
if (updatedProduct) {
61+
res.send({ message: 'Product Updated', product: updatedProduct });
62+
} else {
63+
res.status(500).send({ message: 'Error in updaing product' });
64+
}
65+
} else {
66+
res.status(404).send({ message: 'Product Not Found' });
67+
}
68+
})
69+
);
4470
export default productRouter;

frontend/src/api.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ export const createProduct = async () => {
5858
return { error: err.response.data.message || err.message };
5959
}
6060
};
61+
62+
export const updateProduct = async (product) => {
63+
try {
64+
const { token } = getUserInfo();
65+
const response = await axios({
66+
url: `${apiUrl}/api/products/${product._id}`,
67+
method: 'PUT',
68+
headers: {
69+
'Content-Type': 'application/json',
70+
Authorization: `Bearer ${token}`,
71+
},
72+
data: product,
73+
});
74+
if (response.statusText !== 'OK') {
75+
throw new Error(response.data.message);
76+
}
77+
return response.data;
78+
} catch (err) {
79+
return { error: err.response.data.message || err.message };
80+
}
81+
};
82+
6183
export const signin = async ({ email, password }) => {
6284
try {
6385
const response = await axios({
@@ -128,6 +150,7 @@ export const update = async ({ name, email, password }) => {
128150
return { error: err.response.data.message || err.message };
129151
}
130152
};
153+
131154
export const createOrder = async (order) => {
132155
try {
133156
const { token } = getUserInfo();

frontend/src/srceens/ProductEditScreen.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1-
import { parseRequestUrl } from '../utils';
2-
import { getProduct } from '../api';
1+
import {
2+
parseRequestUrl,
3+
showLoading,
4+
showMessage,
5+
hideLoading,
6+
} from '../utils';
7+
import { getProduct, updateProduct } from '../api';
38

49
const ProductEditScreen = {
5-
after_render: () => {},
10+
after_render: () => {
11+
const request = parseRequestUrl();
12+
document
13+
.getElementById('edit-product-form')
14+
.addEventListener('submit', async (e) => {
15+
e.preventDefault();
16+
showLoading();
17+
const data = await updateProduct({
18+
_id: request.id,
19+
name: document.getElementById('name').value,
20+
price: document.getElementById('price').value,
21+
image: document.getElementById('image').value,
22+
brand: document.getElementById('brand').value,
23+
category: document.getElementById('category').value,
24+
countInStock: document.getElementById('countInStock').value,
25+
description: document.getElementById('description').value,
26+
});
27+
hideLoading();
28+
if (data.error) {
29+
showMessage(data.error);
30+
} else {
31+
document.location.hash = '/productlist';
32+
}
33+
});
34+
},
635
render: async () => {
736
const request = parseRequestUrl();
837
const product = await getProduct(request.id);

0 commit comments

Comments
 (0)