Folder Structure:
- taxi-booking-app
- src
- controllers
- booking.controller.js
- driver.controller.js
- passenger.controller.js
- trip.controller.js
- models
- booking.model.js
- driver.model.js
- passenger.model.js
- trip.model.js
- routes
- booking.route.js
- driver.route.js
- passenger.route.js
- trip.route.js
- app.js
- package.json
Files:
- booking.controller.js: This file will contain the logic for booking a taxi.
const { Booking } = require('../models/booking.model');
exports.bookTaxi = async (req, res) => {
try {
const { passengerId, driverId, pickupLocation, dropoffLocation } = req.body;
const booking = new Booking({ passengerId, driverId, pickupLocation, dropoffLocation });
await booking.save();
return res.status(201).json({ success: true });
} catch (error) {
console.error(error);
return res.status(500).json({ success: false });
}
};
- booking.model.js: This file will contain the schema for a taxi booking.
const mongoose = require('mongoose');
const bookingSchema = new mongoose.Schema({
passengerId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Passenger',
required: true,
},
driverId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Driver',
required: true,
},
pickupLocation: String,
dropoffLocation: String,
});
const Booking = mongoose.model('Booking', bookingSchema);
exports.Booking = Booking;
- booking.route.js: This file will contain the routes for booking a taxi.
const express = require('express');
const router = express.Router();
const { bookTaxi } = require('../controllers/booking.controller');
router.post('/book-taxi', bookTaxi);
module.exports = router;
- driver.controller.js: This file will contain the logic for creating and updating drivers.
const { Driver } = require('../models/driver.model');
exports.createDriver = async (req, res) => {
try {
const { name, vehicleNumber } = req.body;
const driver = new Driver({ name, vehicleNumber });
await driver.save();
return res.status(201).json({ success: true });
} catch (error) {
console.error(error);
return res.status(500).json({ success: false });
}
};
exports.updateDriverLocation = async (req, res) => {
try {
const { driverId, location } = req.body;
await Driver.findByIdAndUpdate(driverId, { location });
return res.status(200).json({ success: true });
} catch (error) {
console.error(error);
return res.status(500).json({ success: false });
}
};
- driver.model.js: This file will contain the schema for a driver.
const mongoose = require('mongoose');
const driverSchema = new mongoose.Schema({
name: String,
vehicleNumber: String,
location: {
type: { type: String },
coordinates: [],
},
});
driverSchema.index({ location: '2dsphere' });
const Driver = mongoose.model('Driver', driverSchema);
exports.Driver = Driver;
- driver.route.js: This file will contain the routes for creating and updating drivers.
const express = require('express');
const router = express.Router();
const { createDriver, updateDriverLocation } = require('../controllers/driver.controller');
router.post('/driver', createDriver);
router.put('/update-driver-location', updateDriverLocation);
module.exports = router;
- passenger.controller.js: This file will contain the logic for creating passengers.
const { Passenger } = require('../models/passenger.model');
exports.createPassenger = async (req, res) => {
try {
const { name } = req.body;
const passenger = new Passenger({ name });
await passenger.save();
return res.status(201).json({ success: true });
} catch (error) {
console.error(error);
return res.status(500).json({ success: false });
}
};
- passenger.model.js: This file will contain the schema for a passenger.
const mongoose = require('mongoose');
const passengerSchema = new mongoose.Schema({
name: String,
});
const Passenger = mongoose.model('Passenger', passengerSchema);
exports.Passenger = Passenger;
- passenger.route.js: This file will contain the routes for creating passengers.
const express = require('express');
const router = express.Router();
const { createPassenger } = require('../controllers/passenger.controller');
router.post('/passenger', createPassenger);
module.exports = router;
- trip.controller.js: This file will contain the logic for starting and ending a trip.
const { Trip } = require('../models/trip.model');
exports.startTrip = async (req, res) => {
try {
const { bookingId, driverId, passengerId, pickupLocation, dropoffLocation } = req.body;
const trip = new Trip({ bookingId, driverId, passengerId, pickupLocation, dropoffLocation });
await trip.save();
return res.status(201).json({ success: true });
} catch (error) {
console.error(error);
return res.status(500).json({ success: false });
}
};
exports.endTrip = async (req, res) => {
try {
const { tripId, location } = req.body;
const trip = await Trip.findById(tripId);
trip.dropoffLocation = location;
trip.completed = true;
await trip.save();
return res.status(200).json({ success: true });
} catch (error) {
console.error(error);
return res.status(500).json({ success: false });
}
};
- trip.model.js: This file will contain the schema for a trip.
const mongoose = require('mongoose');
const tripSchema = new mongoose.Schema({
bookingId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Booking',
required: true,
},
driverId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Driver',
required: true,
},
passengerId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Passenger',
required: true,
},
pickupLocation: String,
dropoffLocation: String,
completed: {
type: Boolean,
default: false,
},
});
const Trip = mongoose.model('Trip', tripSchema);
exports.Trip = Trip;
- trip.route.js: This file will contain the routes for starting and ending a trip.
const express = require('express');
const router = express.Router();
const { startTrip, endTrip } = require('../controllers/trip.controller');
router.post('/start-trip', startTrip);
router.put('/end-trip', endTrip);
module.exports = router;
- app.js: This file will set up the Express server and connect to MongoDB.
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const bookingRoutes = require('./routes/booking.route');
const driverRoutes = require('./routes/driver.route');
const passengerRoutes = require('./routes/passenger.route');
const tripRoutes = require('./routes/trip.route');
mongoose
.connect('mongodb://localhost/taxi-booking-app', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
})
.then(() => console.log('Connected to MongoDB'))
.catch((error) => console.error(error));
app.use(express.json());
app.use('/api', bookingRoutes);
app.use('/api', driverRoutes);
app.use('/api', passengerRoutes);
app.use('/api', tripRoutes);
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
- package.json: This file will contain the dependencies for the project.
{
"name": "taxi-booking-app",
"version": "1.0.0",
"description": "API for taxi booking app",
"main": "app.js",
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.11.8"
}
}