Skip to content

Commit

Permalink
update: update routes, add new routes and controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
khayss committed Jun 21, 2024
1 parent 9a40d0a commit 8b50acd
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 35 deletions.
20 changes: 16 additions & 4 deletions controllers/adminControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const createFood = catchErrorFunc(async (req, res) => {
const { name, category, stock, priceInCents, discountPercentage } = req.body;
const { id } = req.verifiedAdmin;

if (req.files.length < 1)
if (!req.files || req.files.length < 1)
throw new AppError(
"1201",
400,
Expand All @@ -156,7 +156,8 @@ export const createFood = catchErrorFunc(async (req, res) => {
);
}

const foodImages = req.files.map((file) => file.path);
const foodImages = req.files?.map((file) => file.path);
console.log(req.files);
/* create new food */
const newFood = await FoodModel.create({
name,
Expand All @@ -169,7 +170,7 @@ export const createFood = catchErrorFunc(async (req, res) => {
lastUpdatedBy: id,
});

res.status(201).json({ success: true, foodDetails: newFood.toJSON() });
res.status(201).json({ success: true, data: newFood.toJSON() });
});

/* DELETE FOOD CONTROLLER */
Expand Down Expand Up @@ -265,4 +266,15 @@ export const approveRider = catchErrorFunc(async (req, res) => {
});
});

export const ex = catchErrorFunc(async (req, res) => {});
export const getPendingRiders = catchErrorFunc(async (req, res) => {
const page = Math.abs(Number(req.query.page)) || 0;
const limit = Math.abs(Number(req.query.limit)) || 20;

const rider = await RiderModel.find(
{ status: "PENDING" },
{ createdBy: 0, lastUpdatedBy: 0 },
{ limit, skip: page * limit }
);
res.status(200).json({ success: true, data: { rider, limit, page } });
});
// export const ex = catchErrorFunc(async (req, res) => {});
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ const app = express();
app
.use(
cors({
origin: "https://food-app-front-end-sand.vercel.app",
// origin: "https://food-app-front-end-sand.vercel.app",
origin: "http://localhost:3000",
credentials: true,
})
)
.use(morgan("dev")) // Logging requests and response
.use(express.json()) // Parse incoming requests with JSON payload
.use(express.urlencoded({ extended: true })) // Parses incoming requests with urlencoded payloads
.use("/images", express.static("public"))
.use("/images/public", express.static("public"))

/* Router for admin routes */
.use(apiPrefixes.generalApi, generalRouter)
Expand Down
9 changes: 6 additions & 3 deletions middlewares/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export async function verifyAdmin(req, res, next) {
} catch (error) {
res.status(400).json({
success: false,
message: "Cannot verify token. token is invalid or expired",
message:
"Protected Router. No token is provided, or provided key is invalid or expired",
});
}
}
Expand All @@ -31,7 +32,8 @@ export async function verifyRider(req, res, next) {
} catch (error) {
res.status(400).json({
success: false,
message: "Cannot verify token. token is invalid or expired",
message:
"Protected Router. No token is provided, or provided key is invalid or expired",
});
}
}
Expand All @@ -48,7 +50,8 @@ export async function verifyUser(req, res, next) {
} catch (error) {
res.status(400).json({
success: false,
message: "Cannot verify token. token is invalid or expired",
message:
"Protected Router. No token is provided, or provided key is invalid or expired",
});
}
}
8 changes: 4 additions & 4 deletions middlewares/multer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from "path";
/* multer storage for admin signup */
const adminSignupStorage = multer.diskStorage({
destination(req, file, cb) {
cb(null, "uploads/admin");
cb(null, "public/uploads/admin");
},
filename: async (req, file, cb) => {
try {
Expand Down Expand Up @@ -52,7 +52,7 @@ export const adminSignupUpload = multer({
/* Multer storage for new food */
const createFoodStorage = multer.diskStorage({
destination(req, file, cb) {
cb(null, "uploads/food");
cb(null, "public/uploads/food");
},
filename: async (req, file, cb) => {
try {
Expand Down Expand Up @@ -89,11 +89,11 @@ export const createFoodUlpoad = multer({
);
}
},
}).array("images", 5);
}).array("images[]", 5);

const userSignupStorage = multer.diskStorage({
destination(req, file, cb) {
cb(null, "uploads/users");
cb(null, "public/uploads/users");
},
filename: async (req, file, cb) => {
try {
Expand Down
3 changes: 2 additions & 1 deletion routes/adminRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createFood,
deleteFood,
getAdmin,
getPendingRiders,
} from "../controllers/adminControllers.js";
import { adminSignupUpload, createFoodUlpoad } from "../middlewares/multer.js";
import { verifyAdmin } from "../middlewares/authentication.js";
Expand All @@ -26,7 +27,7 @@ adminRouter
.put("/approve-rider", approveRider)
.post("/suspend-user")
.post("/unsuspend-user")
.get("/pending-riders")
.get("/pending-riders", getPendingRiders)
.get("/riders")
.post("/suspend-rider")
.post("unsuspend-rider");
16 changes: 2 additions & 14 deletions utils/adminValidations.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,7 @@ export function validateAdminSignupReqBody(data) {
return result.data;
} else {
const details = result.error.issues.map((issue) => issue.message);
throw new AppError(
"1000",
400,
"ValidationError",
"Incomplete or invalid sign up body",
details
);
throw new AppError("1000", 400, "ValidationError", details[0], details);
}
}

Expand All @@ -104,12 +98,6 @@ export function validateAdminLoginReqBody(data) {
return result.data;
} else {
const details = result.error.issues.map((issue) => issue.message);
throw new AppError(
"1001",
400,
"ValidationError",
"Incomplete or invalid login body",
details
);
throw new AppError("1001", 400, "ValidationError", details[0], details);
}
}
8 changes: 1 addition & 7 deletions utils/foodValidations.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ export function validateNewFoodReqBody(data) {
return result.data;
} else {
const details = result.error.issues.map((issue) => issue.message);
throw new AppError(
"1001",
400,
"ValidationError",
"Incomplete or invalid login body",
details
);
throw new AppError("1001", 400, "ValidationError", details[0], details);
}
}

0 comments on commit 8b50acd

Please sign in to comment.