forked from vimistify/AmbuFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
815 additions
and
741 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const Ambulance = require("../Models/Ambulance"); | ||
const User = require("../Models/User"); | ||
|
||
|
||
// Controller to get all ambulances | ||
const getAllAmbulances = async (req, res) => { | ||
try { | ||
const ambulances = await Ambulance.find(); | ||
res.json(ambulances); | ||
} catch (error) { | ||
res.status(500).json({ error: 'Server error, unable to fetch ambulances' }); | ||
} | ||
}; | ||
|
||
// Controller to book an ambulance by a user | ||
const bookAmbulance = async (req, res) => { | ||
const { userId, ambulanceId } = req.body; | ||
|
||
try { | ||
const user = await User.findById(userId); | ||
if (!user) { | ||
return res.status(404).json({ error: 'User not found' }); | ||
} | ||
|
||
const ambulance = await Ambulance.findById(ambulanceId); | ||
if (!ambulance) { | ||
return res.status(404).json({ error: 'Ambulance not found' }); | ||
} | ||
|
||
if (!ambulance.availabilityStatus) { | ||
return res.status(400).json({ error: 'Ambulance is already booked' }); | ||
} | ||
|
||
ambulance.availabilityStatus = false; | ||
ambulance.user = user._id; | ||
await ambulance.save(); | ||
|
||
user.bookedAmbulances.push(ambulance._id); | ||
await user.save(); | ||
|
||
res.json({ message: 'Ambulance booked successfully', ambulance }); | ||
} catch (error) { | ||
res.status(500).json({ error: 'Server error, unable to book ambulance' }); | ||
} | ||
}; | ||
|
||
// Controller to create a new ambulance | ||
const createAmbulance = async (req, res) => { | ||
const { numberPlate, driverName, location } = req.body; | ||
|
||
try { | ||
const newAmbulance = new Ambulance({ | ||
numberPlate, | ||
driverName, | ||
location, | ||
availabilityStatus: true, // By default, new ambulances are available | ||
}); | ||
|
||
await newAmbulance.save(); | ||
|
||
res.json({ message: 'Ambulance created successfully', ambulance: newAmbulance }); | ||
} catch (error) { | ||
res.status(500).json({ error: 'Server error, unable to create ambulance' }); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
getAllAmbulances, | ||
bookAmbulance, | ||
createAmbulance, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
const ambulanceSchema = new Schema({ | ||
numberPlate: { type: String, required: true, unique: true }, | ||
driverName: { type: String, required: true }, | ||
location: { type: String, required: true }, | ||
availabilityStatus: { type: Boolean, default: true }, // Indicates if the ambulance is available | ||
user: { type: Schema.Types.ObjectId, ref: 'User', required: false } // Reference to the user who booked the ambulance | ||
}); | ||
|
||
module.exports = mongoose.model('Ambulance', ambulanceSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.