Skip to content

Commit

Permalink
Merge pull request #228 from haseebzaki-07/new_branch_4
Browse files Browse the repository at this point in the history
Added station services
  • Loading branch information
dhairyagothi authored Oct 13, 2024
2 parents eaa5d93 + 23ba999 commit d9a3ee9
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 114 deletions.
47 changes: 47 additions & 0 deletions backend/controllers/StationController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Station from "../models/Stations.js";

export const createStation = async (req, res) => {
try {
const { name, location, stationCode, capacity, services } = req.body;

const newStation = new Station({
name,
location,
stationCode,
capacity,
services,
});

const savedStation = await newStation.save();

res.status(201).json({
message: "Station created successfully",
station: savedStation,
});
} catch (error) {
console.error("Error creating station:", error);
res.status(500).json({
message: "Failed to create station",
error: error.message,
});
}
};

export const getAllStations = async (req, res) => {
try {
const stations = await Station.find()
.populate("coolies", "name")
.populate("wheelchairs", "name")
.populate("cloakrooms", "name");

if (!stations || stations.length === 0) {
return res.status(404).json({ message: "No stations found" });
}

return res.status(200).json(stations);
} catch (error) {
return res
.status(500)
.json({ message: "Server error", error: error.message });
}
};
2 changes: 1 addition & 1 deletion backend/controllers/WheelchairController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WheelchairBooking from "../models/WheelChairBooking.js";
import WheelchairBooking from "../models/WheelchairBooking.js";
import { io } from "../index.js";

// Controller to handle booking creation
Expand Down
74 changes: 74 additions & 0 deletions backend/controllers/stationBookingsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Station from "../models/Stations.js";
import CoolieBooking from "../models/CoolieBooking.js";
import WheelchairBooking from "../models/WheelchairBooking.js";
import CloakroomBooking from "../models/CloakroomBooking.js";

export const getStationBookings = async (req, res) => {
try {
const stationId = req.params.id;

const station = await Station.findById(stationId);
if (!station) {
return res.status(404).json({ message: "Station not found" });
}

const coolieBookings = await CoolieBooking.find({ station: stationId });
const wheelchairBookings = await WheelchairBooking.find({
station: stationId,
});
const cloakroomBookings = await CloakroomBooking.find({
station: stationId,
});

const response = {
station: station,
coolieBookings: coolieBookings,
wheelchairBookings: wheelchairBookings,
cloakroomBookings: cloakroomBookings,
};

return res.status(200).json(response);
} catch (error) {
return res.status(500).json({ message: error.message });
}
};

export const getCoolieBookingsByStation = async (req, res) => {
try {
const stationId = req.params.id;

const coolieBookings = await CoolieBooking.find({ station: stationId });

return res.status(200).json(coolieBookings);
} catch (error) {
return res.status(500).json({ message: error.message });
}
};

export const getWheelchairBookingsByStation = async (req, res) => {
try {
const stationId = req.params.id;

const wheelchairBookings = await WheelchairBooking.find({
station: stationId,
});

return res.status(200).json(wheelchairBookings);
} catch (error) {
return res.status(500).json({ message: error.message });
}
};

export const getCloakroomBookingsByStation = async (req, res) => {
try {
const stationId = req.params.id;

const cloakroomBookings = await CloakroomBooking.find({
station: stationId,
});

return res.status(200).json(cloakroomBookings);
} catch (error) {
return res.status(500).json({ message: error.message });
}
};
3 changes: 3 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ app.use(express.urlencoded({ extended: true }));
connectDB();

import authRoutes from "./routes/authRoutes.js";
import stationRoutes from "./routes/stationRoutes.js";

app.use("/auth", authRoutes);
app.use("/api", authRoutes);
app.use("/station", stationRoutes);


app.get("/", (req, res) => {
res.send("Working...");
Expand Down
5 changes: 3 additions & 2 deletions backend/models/CloakroomBooking.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// cloakroomBooking.js
import mongoose from "mongoose";

const Schema = mongoose.Schema;

const cloakroomBookingSchema = new Schema(
{
station: {
type: String,
type: Schema.Types.ObjectId,
ref: 'Station',
required: true,
trim: true,
},
items: {
type: String,
Expand Down
6 changes: 3 additions & 3 deletions backend/models/CoolieBooking.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// coolieBooking.js
import mongoose from "mongoose";

const Schema = mongoose.Schema;

const coolieBookingSchema = new Schema(
{
station: {
type: String,
type: Schema.Types.ObjectId,
ref: 'Station',
required: true,
trim: true,
},
pickupLocation: {
type: String,
Expand Down Expand Up @@ -39,5 +40,4 @@ const coolieBookingSchema = new Schema(
);

const CoolieBooking = mongoose.model("CoolieBooking", coolieBookingSchema);

export default CoolieBooking;
15 changes: 14 additions & 1 deletion backend/models/Stations.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// station.js
import mongoose from "mongoose";

const Schema = mongoose.Schema;
Expand Down Expand Up @@ -40,9 +41,21 @@ const stationSchema = new Schema(
required: true,
},
services: {
type: [String], // An array to list services like cloakroom, parking, restrooms, etc.
type: [String],
required: true,
},
coolies: [{
type: Schema.Types.ObjectId,
ref: 'CoolieBooking',
}],
wheelchairs: [{
type: Schema.Types.ObjectId,
ref: 'WheelchairBooking',
}],
cloakrooms: [{
type: Schema.Types.ObjectId,
ref: 'CloakroomBooking',
}],
},
{
timestamps: true,
Expand Down
16 changes: 8 additions & 8 deletions backend/models/WheelchairBooking.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
// wheelchairBooking.js
import mongoose from 'mongoose';

const Schema = mongoose.Schema;

const wheelchairBookingSchema = new Schema({
station: {
type: String,
type: Schema.Types.ObjectId,
ref: 'Station',
required: true,
trim: true
},
bookingDate: {
type: Date,
required: true
required: true,
},
bookingTime: {
type: String,
required: true
required: true,
},
wheelchairType: {
type: String,
enum: ['manual', 'electric', 'standard'],
default: 'manual'
default: 'manual',
},
}, {
timestamps: true
timestamps: true,
});

const WheelchairBooking = mongoose.model('WheelchairBooking', wheelchairBookingSchema);

const WheelchairBooking = mongoose.models.WheelchairBooking || mongoose.model('WheelchairBooking', wheelchairBookingSchema);
export default WheelchairBooking;
19 changes: 19 additions & 0 deletions backend/routes/stationRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// routes.js
import express from 'express';
import { getCloakroomBookingsByStation, getCoolieBookingsByStation, getStationBookings, getWheelchairBookingsByStation } from '../controllers/stationBookingsController.js';
import { createStation, getAllStations } from '../controllers/StationController.js';

const router = express.Router();

// Route to fetch all bookings for a station
router.get('/:id/bookings', getStationBookings);
router.get('/', getAllStations)
router.post('/', createStation);


// Routes to fetch specific types of bookings by station ID
router.get('/:id/coolies', getCoolieBookingsByStation);
router.get('/:id/wheelchairs', getWheelchairBookingsByStation);
router.get('/:id/cloakrooms', getCloakroomBookingsByStation);

export default router;
1 change: 0 additions & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import About from './components/about';
import Contributor from './Pages/contributor';



function App() {

return (
Expand Down
Loading

0 comments on commit d9a3ee9

Please sign in to comment.