Skip to content

Commit

Permalink
Add trains
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebzaki-07 committed Oct 22, 2024
1 parent 3064aaf commit 7df60dc
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 96 deletions.
62 changes: 62 additions & 0 deletions backend/controllers/trainController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Train from "../models/Trains.js";


// Create a new train
export const createTrain = async (req, res) => {
try {
const { trainNumber, trainName, nextStation, services, platformDetails, coachDetails } = req.body;


const existingTrain = await Train.findOne({ trainNumber });
if (existingTrain) {
return res.status(400).json({ message: "Train with this number already exists" });
}


const newTrain = new Train({
trainNumber,
trainName,
nextStation,
services,
platformDetails,
coachDetails,
});

const savedTrain = await newTrain.save();

return res.status(201).json({
message: "Train created successfully",
train: savedTrain,
});
} catch (error) {
return res.status(500).json({ message: "Error creating train", error: error.message });
}
};

// Fetch all trains
export const getAllTrains = async (req, res) => {
try {
const trains = await Train.find();
return res.status(200).json(trains);
} catch (error) {
return res.status(500).json({ message: "Error fetching trains", error: error.message });
}
};

// Fetch a single train by train number
export const getTrainByNumber = async (req, res) => {
try {
const { trainNumber } = req.params;

const train = await Train.findOne({ trainNumber });

if (!train) {
return res.status(404).json({ message: "Train not found" });
}

return res.status(200).json(train);
} catch (error) {
return res.status(500).json({ message: "Error fetching train", error: error.message });
}
};

2 changes: 2 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ connectDB();

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

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

app.get("/", (req, res) => {
res.send("Working...");
Expand Down
82 changes: 82 additions & 0 deletions backend/models/Trains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// train.js
import mongoose from "mongoose";

const Schema = mongoose.Schema;

const trainSchema = new Schema(
{
trainNumber: {
type: Number,
required: true,
unique: true,
trim: true,
},
trainName: {
type: String,
required: true,
trim: true,
},
nextStation: {
type: {
name: {
type: String,
required: true,
trim: true,
},
stationCode: {
type: String,
required: true,
trim: true,
},
arrivalTime: {
type: Date,
required: true,
},
},
required: true,
},
services: {
type: [String], // An array of available services like ["WiFi", "Food", "Lounge"]
required: true,
},
platformDetails: {
platformNumber: {
type: Number,
required: true,
},
boardingTime: {
type: Date,
required: true,
},
},
coachDetails: [
{
coachNumber: {
type: String,
required: true,
trim: true,
},
coachType: {
type: String,
enum: ['Sleeper', 'AC', 'General', 'ChairCar', 'FirstClass'],
required: true,
},
capacity: {
type: Number,
required: true,
},
reservedSeats: {
type: Number,
required: true,
default: 0,
},
},
],
},
{
timestamps: true,
}
);

const Train = mongoose.model("Train", trainSchema);
export default Train;
13 changes: 13 additions & 0 deletions backend/routes/trainRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// routes.js
import express from 'express';
import { createTrain, getAllTrains, getTrainByNumber } from '../controllers/trainController.js';


const router = express.Router();

router.get('/:trainNumber', getTrainByNumber);
router.post('/', createTrain);
router.get('/', getAllTrains);


export default router;
Loading

0 comments on commit 7df60dc

Please sign in to comment.