Skip to content

Commit df679d2

Browse files
authored
mern
1 parent 96ea552 commit df679d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+22133
-0
lines changed

Diff for: proshop_mern/Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: node backend/server.js

Diff for: proshop_mern/README.md

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# ProShop eCommerce Platform
2+
3+
> eCommerce platform built with the MERN stack & Redux.
4+
5+
### THIS PROJECT IS DEPRECATED
6+
This project is no longer supported. The new project/course has been released. The code has been cleaned up and now uses Redux Toolkit. You can find the new version [HERE](https://github.com/bradtraversy/proshop-v2)
7+
8+
![screenshot](https://github.com/bradtraversy/proshop_mern/blob/master/uploads/Screen%20Shot%202020-09-29%20at%205.50.52%20PM.png)
9+
10+
## Features
11+
12+
- Full featured shopping cart
13+
- Product reviews and ratings
14+
- Top products carousel
15+
- Product pagination
16+
- Product search feature
17+
- User profile with orders
18+
- Admin product management
19+
- Admin user management
20+
- Admin Order details page
21+
- Mark orders as delivered option
22+
- Checkout process (shipping, payment method, etc)
23+
- PayPal / credit card integration
24+
- Database seeder (products & users)
25+
26+
## Note on Issues
27+
Please do not post issues here that are related to your own code when taking the course. Add those in the Udemy Q/A. If you clone THIS repo and there are issues, then you can submit
28+
29+
## Usage
30+
31+
### ES Modules in Node
32+
33+
We use ECMAScript Modules in the backend in this project. Be sure to have at least Node v14.6+ or you will need to add the "--experimental-modules" flag.
34+
35+
Also, when importing a file (not a package), be sure to add .js at the end or you will get a "module not found" error
36+
37+
You can also install and setup Babel if you would like
38+
39+
### Env Variables
40+
41+
Create a .env file in then root and add the following
42+
43+
```
44+
NODE_ENV = development
45+
PORT = 5000
46+
MONGO_URI = your mongodb uri
47+
JWT_SECRET = 'abc123'
48+
PAYPAL_CLIENT_ID = your paypal client id
49+
```
50+
51+
### Install Dependencies (frontend & backend)
52+
53+
```
54+
npm install
55+
cd frontend
56+
npm install
57+
```
58+
59+
### Run
60+
61+
```
62+
# Run frontend (:3000) & backend (:5000)
63+
npm run dev
64+
65+
# Run backend only
66+
npm run server
67+
```
68+
69+
## Build & Deploy
70+
71+
```
72+
# Create frontend prod build
73+
cd frontend
74+
npm run build
75+
```
76+
77+
There is a Heroku postbuild script, so if you push to Heroku, no need to build manually for deployment to Heroku
78+
79+
### Seed Database
80+
81+
You can use the following commands to seed the database with some sample users and products as well as destroy all data
82+
83+
```
84+
# Import data
85+
npm run data:import
86+
87+
# Destroy data
88+
npm run data:destroy
89+
```
90+
91+
```
92+
Sample User Logins
93+
94+
95+
123456
96+
97+
98+
123456
99+
100+
101+
123456
102+
```
103+
104+
105+
## License
106+
107+
The MIT License
108+
109+
Copyright (c) 2020 Traversy Media https://traversymedia.com
110+
111+
Permission is hereby granted, free of charge, to any person obtaining a copy
112+
of this software and associated documentation files (the "Software"), to deal
113+
in the Software without restriction, including without limitation the rights
114+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
115+
copies of the Software, and to permit persons to whom the Software is
116+
furnished to do so, subject to the following conditions:
117+
118+
The above copyright notice and this permission notice shall be included in
119+
all copies or substantial portions of the Software.
120+
121+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
122+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
123+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
124+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
125+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
126+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
127+
THE SOFTWARE.

Diff for: proshop_mern/backend/config/db.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import mongoose from 'mongoose'
2+
3+
const connectDB = async () => {
4+
try {
5+
const conn = await mongoose.connect(process.env.MONGO_URI, {
6+
useUnifiedTopology: true,
7+
useNewUrlParser: true,
8+
useCreateIndex: true,
9+
})
10+
11+
console.log(`MongoDB Connected: ${conn.connection.host}`.cyan.underline)
12+
} catch (error) {
13+
console.error(`Error: ${error.message}`.red.underline.bold)
14+
process.exit(1)
15+
}
16+
}
17+
18+
export default connectDB

Diff for: proshop_mern/backend/controllers/orderController.js

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import asyncHandler from 'express-async-handler'
2+
import Order from '../models/orderModel.js'
3+
4+
// @desc Create new order
5+
// @route POST /api/orders
6+
// @access Private
7+
const addOrderItems = asyncHandler(async (req, res) => {
8+
const {
9+
orderItems,
10+
shippingAddress,
11+
paymentMethod,
12+
itemsPrice,
13+
taxPrice,
14+
shippingPrice,
15+
totalPrice,
16+
} = req.body
17+
18+
if (orderItems && orderItems.length === 0) {
19+
res.status(400)
20+
throw new Error('No order items')
21+
return
22+
} else {
23+
const order = new Order({
24+
orderItems,
25+
user: req.user._id,
26+
shippingAddress,
27+
paymentMethod,
28+
itemsPrice,
29+
taxPrice,
30+
shippingPrice,
31+
totalPrice,
32+
})
33+
34+
const createdOrder = await order.save()
35+
36+
res.status(201).json(createdOrder)
37+
}
38+
})
39+
40+
// @desc Get order by ID
41+
// @route GET /api/orders/:id
42+
// @access Private
43+
const getOrderById = asyncHandler(async (req, res) => {
44+
const order = await Order.findById(req.params.id).populate(
45+
'user',
46+
'name email'
47+
)
48+
49+
if (order) {
50+
res.json(order)
51+
} else {
52+
res.status(404)
53+
throw new Error('Order not found')
54+
}
55+
})
56+
57+
// @desc Update order to paid
58+
// @route GET /api/orders/:id/pay
59+
// @access Private
60+
const updateOrderToPaid = asyncHandler(async (req, res) => {
61+
const order = await Order.findById(req.params.id)
62+
63+
if (order) {
64+
order.isPaid = true
65+
order.paidAt = Date.now()
66+
order.paymentResult = {
67+
id: req.body.id,
68+
status: req.body.status,
69+
update_time: req.body.update_time,
70+
email_address: req.body.payer.email_address,
71+
}
72+
73+
const updatedOrder = await order.save()
74+
75+
res.json(updatedOrder)
76+
} else {
77+
res.status(404)
78+
throw new Error('Order not found')
79+
}
80+
})
81+
82+
// @desc Update order to delivered
83+
// @route GET /api/orders/:id/deliver
84+
// @access Private/Admin
85+
const updateOrderToDelivered = asyncHandler(async (req, res) => {
86+
const order = await Order.findById(req.params.id)
87+
88+
if (order) {
89+
order.isDelivered = true
90+
order.deliveredAt = Date.now()
91+
92+
const updatedOrder = await order.save()
93+
94+
res.json(updatedOrder)
95+
} else {
96+
res.status(404)
97+
throw new Error('Order not found')
98+
}
99+
})
100+
101+
// @desc Get logged in user orders
102+
// @route GET /api/orders/myorders
103+
// @access Private
104+
const getMyOrders = asyncHandler(async (req, res) => {
105+
const orders = await Order.find({ user: req.user._id })
106+
res.json(orders)
107+
})
108+
109+
// @desc Get all orders
110+
// @route GET /api/orders
111+
// @access Private/Admin
112+
const getOrders = asyncHandler(async (req, res) => {
113+
const orders = await Order.find({}).populate('user', 'id name')
114+
res.json(orders)
115+
})
116+
117+
export {
118+
addOrderItems,
119+
getOrderById,
120+
updateOrderToPaid,
121+
updateOrderToDelivered,
122+
getMyOrders,
123+
getOrders,
124+
}

0 commit comments

Comments
 (0)