Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 54 additions & 21 deletions BackEnd/Controllers/flightController.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
const router = require('express').Router();
let Flights = require('../models/Flights');

router.route('/getAllFlights').get(async (req, res) => {
const flights = await Flights.find()
try {
//console.log(flights);
res.send(flights);
}
catch (err) {
res.status(500).send(err);
}
});
router.route('/getAllFlights').get(async(req, res) => {
const flights = await Flights.find({})
try{
res.send(flights);
}
catch(err){
res.status(500).send(err);
}
});

router.patch('/:id', async (req, res, next) => {
try {
const id = req.params.id;
const toUpdate = req.body;
console.log(toUpdate);
const result = await Flights.findByIdAndUpdate({ _id: req.params.id }, toUpdate, { new: true });
console.log(result);
res.send(result);
router.get('/FlightDetails/:id',async(req, res) => {
console.log('id', req.params.id)
try{
const id = req.params.id;
const flights = await Flights.findById(id)
console.log(flights);
res.send(flights);
}
catch(err){
res.status(500).send(err)
}
});

router.route('/getSearchedFlights').get(async(req, res) => {
const flights = await Flights.find(req.query)
try{
res.send(flights)
}
catch (err) {
console.log(err.message);
catch(err){
res.status(500).send(err);
}
})
}
);

router.delete('/:id', async (req, res, next) => {
try {
Expand All @@ -38,5 +47,29 @@ router.delete('/:id', async (req, res, next) => {
}
})

router.patch('/:id', async (req, res, next) => {
try{
const id = req.params;
const toUpdate = req.body.updatedFlights;
console.log(toUpdate);
const result = await Flights.findByIdAndUpdate({ _id: req.params.id }, toUpdate, { new: true });
res.send(result);
}
catch(err){
console.log(err.message);
}
});

router.post('/createFlight',async(req, res) => {
const flight =new Flights( req.body);
try{
await flight.save();
}
catch(err)
{
console.log("ERRORR");
res.status(500).send(err)
}
});

module.exports = router;
35 changes: 32 additions & 3 deletions BackEnd/models/Flights.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,46 @@ const flightSchema = new Schema({
type: String,
required: true
},
FlightDate: {
FlightDepDate: {
type: Date,
required: true,
},
Cabin: {
FlightArrDate: {
type: Date,
required: true,
},
FlightDepTime: {
type: String,
required: true,
},
FlightArrTime: {
type: String,
required: true,
},
FlightNumber: {
type: String,
required: true,
unique:true
},
Airport: {
type: String,
required: true
},
SeatsAvailable: {
Terminal: {
type: Number,
required: true
},
FirstSeats: {
type: Number,
required: false
},
BusinessSeats: {
type: Number,
required: false
},
EconomySeats: {
type: Number,
required: false
}
}, { timestamps: true });

Expand Down
10 changes: 10 additions & 0 deletions BackEnd/routes/FlightRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express=require('express');
const { models } = require('mongoose');
const flightController = require('../Controller/flightController');
const flightRouter = express.Router();
flightRouter.use(express.json());
flightRouter.use(express.urlencoded({extended: false}));

flightRouter.get('/getAllFlights',flightController.getAllFlights);

module.exports = flightRouter;
28 changes: 14 additions & 14 deletions BackEnd/server.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
const express = require("express");
const app = express();
const mongoose = require('mongoose');
const cors = require('cors')

const cors = require('cors');
const dotenv = require('dotenv')
dotenv.config()

const MongoURI = process.env.Mongo_URI

const app = express();
const port = process.env.PORT || "8000";
const Flights = require('./models/Flights');

const flightCont = require('./Controllers/flightController');

const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());

mongoose.connect(MongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(result => console.log("MongoDB is now connected"))
.catch(err => console.log(err));
mongoose.connect(MongoURI, { useNewUrlParser: true, useUnifiedTopology: true})
.then(result =>console.log("MongoDB is now connected") )
.catch(err => console.log(err));

app.use('/flights',flightCont);

app.use('/flights', flightCont);
app.get('/',(req,res) => {
res.send('Welcome');
})

app.listen(port, () => {
console.log(`Listening to requests on http://localhost:${port}`);
});
console.log(`Listening to requests on http://localhost:${port}`);
});
Loading