Skip to content

Commit

Permalink
bit of fixes in response statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayroid committed Nov 28, 2023
1 parent 0c5a7af commit 2d4830f
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 49 deletions.
1 change: 0 additions & 1 deletion controllers/db/parkingSpotDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const updateSpot = (query, data) => {
.then((result) => {
if (result) {
console.log(`Parking Spot Updated ✅ - {spotId : ${result._id}}`);
console.log(result);
resolve(result);
}
})
Expand Down
8 changes: 4 additions & 4 deletions controllers/fileUploadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const fileDestionationMapping = (fileName) => {
profilePic: "public/img/profilePic",
};
return fileDestionationMap[fileName];
} catch (err) {
console.log(err);
} catch (error) {
console.log(error);
}
};

Expand All @@ -23,8 +23,8 @@ const filenameMapping = (req, fileName, fileExtension) => {
profilePic: `${req.payload.userId}${fileExtension}`,
};
return fileNameMap[fileName];
} catch (err) {
console.log(err);
} catch (error) {
console.log(error);
}
};

Expand Down
25 changes: 14 additions & 11 deletions controllers/parkingSpotController.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ const createNewParkingSpot = async (req, res) => {

// 5. SENDING RESPONSE
if (created) {
res.status(StatusCodes.CREATED).send({ parkingSpotId: created._id });
return res
.status(StatusCodes.CREATED)
.send({ parkingSpotId: created._id });
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Creating Parking Spot! ❌ ");
}
Expand Down Expand Up @@ -76,9 +78,11 @@ const getParkingSpots = async (req, res) => {

// 4. SENDING RESPONSE
if (spots.length >= 0) {
res.status(StatusCodes.OK).send({ parkingSpots: spots });
return res.status(StatusCodes.OK).send({ parkingSpots: spots });
} else {
res.status(StatusCodes.NOT_FOUND).send("No Parking Spots Found! ❌");
return res
.status(StatusCodes.NOT_FOUND)
.send("No Parking Spots Found! ❌");
}
} catch (error) {
// 5. HANDLING ERRORS
Expand Down Expand Up @@ -108,9 +112,9 @@ const updateParkingSpot = async (req, res) => {

// 4. SENDING RESPONSE
if (updated) {
res.status(StatusCodes.OK).send(updated);
return res.status(StatusCodes.OK).send(updated);
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Updating parking Spot! ❌");
}
Expand Down Expand Up @@ -142,9 +146,9 @@ const deleteParkingSpot = async (req, res) => {

// 4. SENDING RESPONSE
if (deleted) {
res.status(StatusCodes.OK).send("Parking Deleted ✅", deleted);
return res.status(StatusCodes.OK).send("Parking Deleted ✅", deleted);
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Deleting parking Spot! ❌");
}
Expand Down Expand Up @@ -173,7 +177,6 @@ const bookParkingSpot = async (req, res) => {
// 3. UPDATING SPOT STATUS

const query = { parkingNumber: parkingNumber };
console.log(req.payload)
const data = {
currentlyParkedUser: req.payload.userId,
parkingStatus: "booked",
Expand All @@ -183,9 +186,9 @@ const bookParkingSpot = async (req, res) => {

// 4. SENDING RESPONSE
if (updated) {
res.status(StatusCodes.OK).send("Parking Spot Booked ✅");
return res.status(StatusCodes.OK).send("Parking Spot Booked ✅");
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Updating parking Spot! ❌");
}
Expand Down
41 changes: 23 additions & 18 deletions controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ const registerUser = async (req, res) => {
// 5. SENDING USER
if (created) {
SENDMAIL(username, email, 0, "REGISTRATION");
res.status(StatusCodes.CREATED).send("User Created ✅");
return res.status(StatusCodes.CREATED).send("User Created ✅");
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Creating User! ❌");
}
Expand Down Expand Up @@ -129,7 +129,9 @@ const loginUserMail = async (req, res) => {
} catch (error) {
// 8. Handling errors
console.log(error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR).send("Error Logging In! ❌");
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Logging In! ❌");
}
};

Expand Down Expand Up @@ -178,7 +180,9 @@ const loginUserPhone = async (req, res) => {
} catch (error) {
// 7. Handling errors
console.log(error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR).send("Error Logging In! ❌");
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Logging In! ❌");
}
};

Expand All @@ -191,14 +195,16 @@ const logOutUser = async (req, res) => {
// 3. IF TOKEN IS VERIFIED
if (token) {
// 4. SENDING RESPONSE
res.status(StatusCodes.OK).send("Logged Out ✅");
return res.status(StatusCodes.OK).send("Logged Out ✅");
} else {
// 5. SENDING ERROR RESPONSE
res.status(StatusCodes.UNAUTHORIZED).send("Token Invalid! ");
return res.status(StatusCodes.UNAUTHORIZED).send("Token Invalid! ");
}
} catch (error) {
console.log(error);
res.status(StatusCodes.INTERNAL_SERVER_ERROR).send("Error logging out ❌");
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error logging out ❌");
}
};

Expand Down Expand Up @@ -328,7 +334,7 @@ const verifyJWTToken = async (req, res) => {
} catch (error) {
// 5. HANDLING ERRORS
console.log(error);
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Verifying Token! ❌");
}
Expand Down Expand Up @@ -392,9 +398,9 @@ const readUser = async (req, res) => {
})),
};

res.status(StatusCodes.OK).send(data);
return res.status(StatusCodes.OK).send(data);
} else {
res.status(StatusCodes.NOT_FOUND).send("User Not Found ❌");
return res.status(StatusCodes.NOT_FOUND).send("User Not Found ❌");
}
} catch (error) {
// 5. HANDLING ERRORS
Expand All @@ -410,7 +416,6 @@ const updateUser = async (req, res) => {
try {
// 1. FETCHING DATA FROM REQUEST BODY
let { query, data } = req.body;
console.log(req.body);

// 2. CHECKING IF QUERY IS EMPTY
if (query === undefined || query === null) {
Expand Down Expand Up @@ -439,9 +444,9 @@ const updateUser = async (req, res) => {

// 7. SENDING RESPONSE
if (updated) {
res.status(StatusCodes.OK).send("User Updated ✅");
return res.status(StatusCodes.OK).send("User Updated ✅");
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Updating User! ❌");
}
Expand Down Expand Up @@ -483,9 +488,9 @@ const deleteUser = async (req, res) => {

// 6. SENDING RESPONSE
if (deleted) {
res.status(StatusCodes.OK).send("User Deleted ✅", deleted);
return res.status(StatusCodes.OK).send("User Deleted ✅", deleted);
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Deleting User! ❌");
}
Expand All @@ -502,7 +507,7 @@ const deleteUser = async (req, res) => {
const uploadProfilePic = async (req, res) => {
try {
// 0. SETTING DEFAULT URL
const defaultUrl = "http://192.168.1.9:3000/img/profilePic/";
const defaultUrl = "http://localhost:3000/img/profilePic/";

// 1. FETCHING DATA FROM REQUEST BODY
const userId = req.payload.userId;
Expand Down Expand Up @@ -535,9 +540,9 @@ const uploadProfilePic = async (req, res) => {

// 7. SENDING RESPONSE
if (updated) {
res.status(StatusCodes.OK).send("Profile Pic Uploaded ✅");
return res.status(StatusCodes.OK).send("Profile Pic Uploaded ✅");
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Uploading Profile Pic! ❌");
}
Expand Down
14 changes: 7 additions & 7 deletions controllers/vehicleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ const registerVehicle = async (req, res) => {

// 9. SENDING VEHICLE
if (updated) {
res.status(StatusCodes.CREATED).send({ vehicleId: created._id });
return res.status(StatusCodes.CREATED).send({ vehicleId: created._id });
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Registering Vehicle! ❌");
}
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Registering Vehicle! ❌");
}
Expand Down Expand Up @@ -118,9 +118,9 @@ const updateVehicle = async (req, res) => {

// 5. SENDING VEHICLE
if (updated) {
res.status(StatusCodes.OK).send(updated);
return res.status(StatusCodes.OK).send(updated);
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Updating Vehicle! ❌");
}
Expand Down Expand Up @@ -155,9 +155,9 @@ const deleteVehicle = async (req, res) => {

// 5. SENDING VEHICLE
if (deleted) {
res.status(StatusCodes.OK).send("Vehicle Deleted ✅", deleted);
return res.status(StatusCodes.OK).send("Vehicle Deleted ✅", deleted);
} else {
res
return res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Deleting Vehicle! ❌");
}
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ app.use(cors());
// TEST ROUTE
app.post("/api/test", (req, res) => {
console.log("Server is working ✅", Date.now());
res.status(200).send("Server is working ✅");
return res.status(200).send("Server is working ✅");
});

// ROUTERS
Expand Down
6 changes: 3 additions & 3 deletions middlewares/jwtAuthMW.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const checkAccessToken = async (token, tokenType) => {
payload = jwt.verify(token, process.env.JWT_REFRESH_SECRET);
}
return payload;
} catch (err) {
} catch (error) {
// 2. HANDLE ERROR
return false;
}
Expand All @@ -57,8 +57,8 @@ const verifyAccessToken = async (req, res, next) => {
}
req.payload = data;
});
} catch (err) {
console.log(err);
} catch (error) {
console.log(error);
return res.status(403).json({ msg: "User Unauthorized ❌" });
}
next();
Expand Down
8 changes: 4 additions & 4 deletions routers/userRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ var SAPIDS = new Array();

// API ENDPOINTS
USER.get("/test", (req, res) => {
res.status(StatusCodes.OK).send("Server is working ✅");
return res.status(StatusCodes.OK).send("Server is working ✅");
});

USER.post("/test", (req, res) => {
res.status(StatusCodes.OK).send("Server is working ✅");
return res.status(StatusCodes.OK).send("Server is working ✅");
});

// USER ROUTES
Expand All @@ -62,10 +62,10 @@ USER.post("/update", (req, res) => {
const { sapid } = req.body;
SAPIDS.push([sapid, Date.now()]);
console.log(sapid);
res.send("Updated!");
return res.send("Updated!");
});
USER.post("getUpdate", (req, res) => {
res.send(SAPIDS[SAPIDS.length - 1]);
return res.send(SAPIDS[SAPIDS.length - 1]);
});

// PARKING ROUTES
Expand Down

0 comments on commit 2d4830f

Please sign in to comment.