Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion controllers/productController.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const getProducts = async (req, res) => {

const getSellerProducts = async (req, res) => {
try {
let products = await Product.find({ seller: req.params.id })
// #6 key should be "_id" not "seller"
let products = await Product.find({ _id: req.params.id })
console.log(products) ;
if (products.length > 0) {
res.send(products)
} else {
Expand Down
6 changes: 4 additions & 2 deletions controllers/sellerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const sellerRegister = async (req, res) => {

const seller = new Seller({
...req.body,
password: bcrypt.hash
// #8 bcrypt.hash --> hashedPass
password: hashedPass
});

const existingSellerByEmail = await Seller.findOne({ email: req.body.email });
Expand Down Expand Up @@ -51,7 +52,8 @@ const sellerLogIn = async (req, res) => {

seller = {
...seller._doc,
token: tokens
// #11 token : tokens ==> token
token
};

res.send(seller);
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const cors = require("cors")
const mongoose = require("mongoose")
const dotenv = require("dotenv")


const app = express()
const Routes = require("./routes/route.js")

// dotenv.config() should above(here);
dotenv.config();
const PORT = process.env.PORT || 5000

dotenv.config();

app.use(express.json({ limit: '10mb' }))
app.use(cors())
Expand Down
4 changes: 3 additions & 1 deletion middleware/authMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const authMiddleware = (req, res, next) => {
}

try {
const decoded = jwt.env(token, process.env.SECRET_KEY);
// #14 jwt.env == > jwt.verify
const decoded = jwt.verify(token, process.env.SECRET_KEY);
req.user = decoded;
console.log(req.user);
next();
} catch (err) {
return res.status(401).json({ message: 'Invalid token' });
Expand Down
3 changes: 2 additions & 1 deletion models/customerSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const customerSchema = mongoose.Schema({
},
seller: {
type: mongoose.Schema.Types.ObjectId,
ref: 'SELLER'
// #13 ref: 'SELLER' == > ref: 'seller'
ref: 'seller'
},
}],
shippingData: {
Expand Down
3 changes: 2 additions & 1 deletion models/orderSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,5 @@ const orderSchema = new mongoose.Schema(
},
});

module.exports = mongoose.model("customer", orderSchema);
// #3 changing <Collectionname> from "customer" to "order" (cause there is customer collection is already created to it can't be overwritten )
module.exports = mongoose.model("order", orderSchema);
14 changes: 10 additions & 4 deletions models/productSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,24 @@ const productSchema = mongoose.Schema(
},
reviewer: {
type: mongoose.Schema.Types.ObjectId,
ref: "CUSTOMERS",
// #12 ref: "CUSTOMERS" == > ref: "customer"
ref: "customer",
},
date: {
type: Date,
default: Text,
// #1 changing default "text" to "Date.now()"
default: Date.now(),
},
},
],
seller: {
type: mongoose.Schema.Types.ObjectId,
ref: 'seller'
ref: 'seller',
// #15
required : true

},
}, { timestamps: false});

module.exports = mongoose.mongoose("product", productSchema)
// #2 changing mongoose method from "mongoose" to "model" (mongoose.model() method)
module.exports = mongoose.model("product", productSchema)
4 changes: 3 additions & 1 deletion models/sellerSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ const sellerSchema = new mongoose.Schema({
}
});

moduleexports = mongoose.model("seller", sellerSchema)
// #7 moduleexports ==> module.exports (changed)

module.exports = mongoose.model("seller", sellerSchema)
12 changes: 10 additions & 2 deletions routes/route.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const router = require('express').Router();
const authMiddleware = require('../middleware/authMiddleware.js');

// #4 changed (../controllers/orderController.js) to (../controllers/sellerController.js)

//sellerRegisterand sellerLogin should import from "sellerController.js" not from "orderController.js"


const {
sellerRegister,
sellerLogIn
} = require('../controllers/orderController.js');
} = require('../controllers/sellerController.js');

const {
productCreate,
Expand Down Expand Up @@ -40,7 +45,7 @@ router.post('/SellerRegister', sellerRegister);
router.post('/SellerLogin', sellerLogIn);

// Product
router.post('/ProductCreate', productCreate);
router.post('/ProductCreate', authMiddleware , productCreate);
router.get('/getSellerProducts/:id', getSellerProducts);
router.get('/getProducts', getProducts);
router.get('/getProductDetail/:id', getProductDetail);
Expand Down Expand Up @@ -69,3 +74,6 @@ router.put('/CustomerUpdate/:id', cartUpdate);
router.post('/newOrder', newOrder);
router.get('/getOrderedProductsByCustomer/:id', getOrderedProductsBySeller);
router.get('/getOrderedProductsBySeller/:id', getOrderedProductsBySeller);

// #5 router was not exported
module.exports = router;
5 changes: 4 additions & 1 deletion utils/token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const jwt = require("jsonwebtoken");

const createNewToken = (payload) => {
return jwt.sign({ userId: payload }, process.getuid.SECRET_KEY, { expiresIn: '10d' });
// #9 process.getuid.SECRET_KEY ==> process.env.SECRET_KEY
return jwt.sign({ userId: payload }, process.env.SECRET_KEY, { expiresIn: '10d' });
}

// #10 there was to no exporting
module.exports = {createNewToken};