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
9 changes: 9 additions & 0 deletions errors/customError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

class CustomApiError extends Error {
constructor (message) {
super (message)
}
}


module.exports = CustomApiError;
30 changes: 27 additions & 3 deletions middlewares/errorHandler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@


const errorHandler = (err,req,res,next) =>{
return res.status(500).json({
message:'Internal server error'
});
let CustomError = {
statusCode :err.statusCode || 500,
msg: err.message || 'something went wrong, Please try again'
}

// check and handle validation error
if(err.name === 'validation error'){
CustomError.msg = Object.values(err.errors).map((item) => item.message.join(','));
CustomError.statusCode = 400;
}

// check for duplicate values amd handle the error
if(err.code && err.code === 11000){
CustomError.msg = `Duplicate value entered for ${Object.keys(err.KeyValue)} field, Please chose another value`;
CustomError.statusCode = 400;
}

// check for cast errors or searchs with wrong id and handle the error
if(err.name === 'CastError'){
CustomError.msg = `No item with such id`;
CustomError.statusCode = 400;
}

return res.status(CustomError.statusCode).json({msg: CustomError.msg});

}


Expand Down
4 changes: 2 additions & 2 deletions models/favourite.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ const favouriteSchema = new Schema({
});

favouriteSchema.index({ user: 1, favourite: 1 }, { unique: true });
const favourite = mongoose.model("favourite", favouriteSchema);
module.exports = favourite;
// const favourite = mongoose.model("favourite", favouriteSchema);
module.exports = mongoose.model("favourite", favouriteSchema);
4 changes: 2 additions & 2 deletions models/likes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ const likesSchema = new Schema({
}, { timestamps: true });

likesSchema.index({ user: 1, like: 1 }, { unique: true });
const ProductLike = mongoose.model("ProductLike", likesSchema);
module.exports = ProductLike;
// const ProductLike = mongoose.model("ProductLike", likesSchema);
module.exports = mongoose.model("ProductLike", likesSchema);
4 changes: 2 additions & 2 deletions models/paystack.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ const paystackSchema = new Schema({
deliveryLocation: String
})

const Paystack = mongoose.model("Paystack", paystackSchema);
module.exports = Paystack;
// const Paystack = mongoose.model("Paystack", paystackSchema);
module.exports = mongoose.model("Paystack", paystackSchema); // shorter codes like this runs faster.
4 changes: 2 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ const userSchema = new Schema({
}
})

const User = mongoose.model("User", userSchema);
module.exports = User;
// const User = mongoose.model("User", userSchema);
module.exports = mongoose.model("User", userSchema);
2 changes: 0 additions & 2 deletions routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ const {
const { upload } = require('../middlewares/cloudinary')
const expressBusboy = require('express-busboy')



router.route('/signup').post(upload.single('picture'), adminSignup)
expressBusboy.extend(router).route('/signin').post(adminSignIn)

Expand Down
12 changes: 6 additions & 6 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const express = require("express");
const router = express.Router();
// const express = require("express");
// const router = express.Router();

router.get("/", (req, res) => {
res.send("Landing page");
})
// router.get("/", (req, res) => {
// res.send("Landing page");
// })

module.exports = router;
// module.exports = router;
8 changes: 6 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require('dotenv').config()
require('dotenv').config()
const express = require('express')
const app = express()
const { connectDB } = require('./config/database')
Expand All @@ -16,7 +16,7 @@ const errorHandler = require('./middlewares/errorHandler')
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(bodyParser.urlencoded({ extended: true }))

app.use(cors())
app.use('/api/v1/', indexRouter)
app.use('/api/v1/auth/admin', adminRouter)
Expand All @@ -28,6 +28,10 @@ app.use('/api/v1/paystack', paystackRouter)
app.use(NotFound)
app.use(errorHandler)

app.get('/', (req,res) =>{
res.send('<h2>Shooping site API</h2><a href="">Documentation</a>')
});

const port = process.env.PORT || 3000
const start = async () => {
try {
Expand Down
5 changes: 0 additions & 5 deletions utils/catchAsync.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// export default (fn) => {
// return (req, res, next) => {
// fn(req, res, next).catch(next);
// };
// };

const asyncWrapper = (fn)=>{
const x = async(req,res,next) =>{
Expand Down