-
Notifications
You must be signed in to change notification settings - Fork 120
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
1 parent
3064aaf
commit 7df60dc
Showing
5 changed files
with
274 additions
and
96 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
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 }); | ||
} | ||
}; | ||
|
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,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; |
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,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; |
Oops, something went wrong.