Skip to content

Commit

Permalink
Updated API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanField15 committed Oct 27, 2023
1 parent f31936f commit fe1a6ec
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NODE_ENV=development
PORT=3000
PORT=5000
DATABASE=mongodb+srv://4bryanfield:[email protected]/giddyup?retryWrites=true&w=majority
DB_PASSWORD=ERSqJMrOfgLRiHQl
DATABASE_USERNAME=4bryanfield
Expand Down
73 changes: 73 additions & 0 deletions controllers/tripController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,79 @@ exports.aliasTopTrips = (req, res, next) => {
next();
};

exports.bookedTrips = async (req, res) => {
try {
// Retrieve the user's ID from the request (assuming it's available after authentication)
//console.log(req.body);
const userId = req.body.passenger;

// Find all trips where the user's ID is the passenger and ride is booked
const bookedTrips = await Trip.find({
passenger: userId,
rideBooked: true,
});

res.status(200).json({
status: 'success',
results: bookedTrips.length,
data: {
trips: bookedTrips,
},
});
} catch (err) {
res.status(500).json({
status: 'error',
message: err.message,
});
}
};

exports.bookingButton = async (req, res) => {
try {
//console.log(req.params);
const tripId = req.params.id;
const passengerId = req.body.passenger;

// Find the trip document by ID
const trip = await Trip.findById(tripId);

if (!trip) {
return res.status(404).json({
status: 'fail',
message: 'Trip not found',
});
}

// Check if the trip is already booked
if (trip.rideBooked) {
return res.status(400).json({
status: 'fail',
message: 'Trip is already booked',
});
}

// Update the trip document
trip.rideBooked = true;
trip.passenger = passengerId;

// Save the updated trip document
await trip.save();

res.status(200).json({
status: 'success',
message: 'Trip booked successfully',
data: {
trip,
},
});
} catch (err) {
res.status(500).json({
status: 'error',
message: err.message,
});
}
};

exports.searchAllTrips = async (req, res) => {
try {
const { origin, destination, deptDate } = req.body;
Expand Down
2 changes: 2 additions & 0 deletions routes/tripRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ router
.post(tripController.createTrip);

router.route('/search').get(tripController.searchAllTrips);
router.route('/booking-button/:id').patch(tripController.bookingButton);
router.route('/booked-trips').get(tripController.bookedTrips);

router
.route('/:id')
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mongoose
console.error('Error connecting to MongoDB:', error);
});

const port = process.env.PORT || 3000;
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Giddyup is galloping on port ${port}`);
});

0 comments on commit fe1a6ec

Please sign in to comment.