Skip to content

Commit

Permalink
PS booking added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayroid committed Nov 26, 2023
1 parent 44a8308 commit aaf1902
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
43 changes: 42 additions & 1 deletion controllers/parkingSpotController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const {
UPDATESPOT,
DELETESPOT,
} = require("./db/parkingSpotDatabase");
const { Query } = require("mongoose");

// CREATE NEW PARKING SPOT CONTROLLER
const createNewParkingSpot = async (req, res) => {
Expand Down Expand Up @@ -158,9 +157,51 @@ const deleteParkingSpot = async (req, res) => {
}
};

const bookParkingSpot = async (req, res) => {
try {
// 1. FETCHING DATA FROM REQUEST BODY
const { parkingNumber } = req.body;

// 2. CHECKING IF SPOT EXISTS
const spot = await READSPOT([{ parkingNumber: parkingNumber }]);
if (spot.length === 0) {
return res
.status(StatusCodes.NOT_FOUND)
.send("Parking Spot Not Found ! ❌");
}

// 3. UPDATING SPOT STATUS

const query = { parkingNumber: parkingNumber };
console.log(req.payload)
const data = {
currentlyParkedUser: req.payload.userId,
parkingStatus: "booked",
};

const updated = await UPDATESPOT(query, data);

// 4. SENDING RESPONSE
if (updated) {
res.status(StatusCodes.OK).send("Parking Spot Booked ✅");
} else {
res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Updating parking Spot! ❌");
}
} catch (error) {
// 5. HANDLING ERRORS
console.log(error);
res
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send("Error Updating parking Spot! ❌");
}
};

module.exports = {
CREATENEWPARKINGSPOT: createNewParkingSpot,
UPDATEPARKINGSPOT: updateParkingSpot,
DELETEPARKINGSPOT: deleteParkingSpot,
GETPARKINGSPOTS: getParkingSpots,
BOOKPARKINGSPOT: bookParkingSpot,
};
9 changes: 7 additions & 2 deletions models/parkingSpotModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ const parkingSpotSchema = new mongoose.Schema({
type: String,
required: true,
},
currentlyParked: {
currentlyParkedUser: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
required: false,
},
currentlyParkedVehicle: {
type: mongoose.Schema.Types.ObjectId,
ref: "vehicle",
required: false,
},
lastParked: {
lastParkedVehicle: {
type: mongoose.Schema.Types.ObjectId,
ref: "vehicle",
required: false,
Expand Down
6 changes: 6 additions & 0 deletions routers/parkingSpotRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ const {
CREATENEWPARKINGSPOT,
UPDATEPARKINGSPOT,
DELETEPARKINGSPOT,
BOOKPARKINGSPOT,
} = require("../controllers/parkingSpotController");

// JWT IMPORT
const { VERIFYTOKEN } = require("../middlewares/jwtAuthMW");

// CREATING ROUTER
const PARKINGSPOT = express.Router();

Expand All @@ -18,6 +22,8 @@ PARKINGSPOT.post("/getParkingSpot", GETPARKINGSPOTS);
PARKINGSPOT.post("/updateParkingSpot", UPDATEPARKINGSPOT);
PARKINGSPOT.post("/deleteParkingSpot", DELETEPARKINGSPOT);

PARKINGSPOT.post("/bookParkingSpot", VERIFYTOKEN, BOOKPARKINGSPOT);

// EXPORTING ROUTER
module.exports = {
PARKINGSPOTROUTER: PARKINGSPOT,
Expand Down
8 changes: 2 additions & 6 deletions routers/userRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const { VERIFYUSERMW } = require("../middlewares/userMW");
// FILE UPLOAD CONTROLLER IMPORT
const { UPLOAD } = require("../controllers/fileUploadController");

// JWT IMPORT AND BLACKLIST CHECK IMPORT
// JWT IMPORT
const { VERIFYTOKEN } = require("../middlewares/jwtAuthMW");

// CREATING ROUTER
Expand All @@ -49,11 +49,7 @@ USER.post("/verify/phone", VERIFYOTPPHONE);
USER.post("/verify/token", VERIFYJWTTOKEN);
USER.post("/refreshToken", REFRESHJWTTOKEN);
USER.post("/getUser", VERIFYTOKEN, READUSER);
USER.post(
"/updateUser",
VERIFYTOKEN,
UPDATEUSER
);
USER.post("/updateUser", VERIFYTOKEN, UPDATEUSER);
USER.post(
"/updateProfilePic",
VERIFYTOKEN,
Expand Down

0 comments on commit aaf1902

Please sign in to comment.