Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# booking-flight-api
booking flight app clone
112 changes: 109 additions & 3 deletions controllers/flightController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,112 @@
exports.example = (req, res) => {
console.log("example")
res.send("Flight example")
const { Flight } = require("../models/Flight");
//const uuid = require('uuid').v4();
const {v4: uuid} = require("uuid");




//get all flights
exports.getAllFlights = async (req, res)=>{
try{
const flights = Flight;

//res.status(200).json(users);
res.status(200).json({
message: "All Flight booked",
AllFlight: flights,
});
} catch(err){
res.status(500).json({
message: err.message
});
}
}
//create new flight
exports.bookFlight = async (req, res) =>{
try{

const { title, time, price, date} = await req.body;
const newFlight = {
id: uuid(),
title,
time,
price,
date,
};

Flight.push(newFlight);


res.status(201).json({
message: "flight booked",
newFlight,
});
} catch(err){
res.status(500).json({
message: err.message
})
}
}

//fetch single user
exports.getEachFlight = async (req, res)=>{
try{
id= req.params.id;
const EachFlight = Flight.find((flight) => flight.id === id);

if(EachFlight)
return res.status(200).json({
message: "Flight record found",
EachFlight,
});
res.status(404).json({
message: "Flight record not found in database",
});
}
catch(err){
res.status(500).json({
message: err.message
})
}
}

//update users
exports.updateFlight = async (req, res) =>{
try{
id = req.params.id;
const FlightUpdate = Flight.find((flight) => flight.id === id);

const { title, time, price, date } = await req.body;
FlightUpdate.title = title;
FlightUpdate.time = time;
FlightUpdate.price = price;
FlightUpdate.date = date;

res.status(200).json({
message: "flight data updated",
FlightUpdate,
});
} catch(err){
res.status(500).json({
message:err.message
});
}
}

//delete user
exports.removeFlight = async(req, res) =>{
try{
id = req.params.id;
const deleteFlight = Flight.find((flight) => flight.id === id);
Flight.splice(Flight.indexOf(deleteFlight), 1)

res.status(200).json({
message: "Booked flight removed successfully",

});
} catch(err){
res.status(500).json({
message:err.message
});
}
}
30 changes: 30 additions & 0 deletions flight.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@url = "http://localhost:3000/"
GET http://localhost:3000/Flight HTTP/1.1

###
GET http://localhost:3000/Flight/d3e6040c-6fd1-42ab-a9d8-61c5b9e29165 HTTP/1.

###
POST http://localhost:3000/Flight HTTP/1.1
Content-Type: application/json

{
"title": "niyi",
"time": "10am",
"price":"300000",
"date": "20-2-2023"
}

###
PUT http://localhost:3000/Flight/cc0bdcfb-7989-4816-8b3f-6f4bac97072f HTTP/1.1
Content-Type: application/json

{
"title": "Janet",
"time": "11am",
"price": "500000",
"date": "20-12-2022"
}

###
DELETE http://localhost:3000/Flight/d3e6040c-6fd1-42ab-a9d8-61c5b9e29165
24 changes: 14 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@

const express = require("express");
const { json } = require("express");
const flights = require("./controllers/flightController");
const models = require("./models/Flight");
const routes = require("./routes/flightRoute");
const {v4: uuid} = require('uuid');
const { json }= require("express");

const app = express();
const flight = require('./routes/flightRoute.js');

const app = express();
app.use(json());

app.use("/", routes);
app.use("/Flight", flight);



const port = process.env.PORT || 3000;

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
app.get("/", (req, res)=>{
res.send("working on a project");

})
const PORT = process.env.PORT || 3000;
app.listen(PORT,()=>console.log(`serving on port ${PORT}`));
2 changes: 1 addition & 1 deletion models/Flight.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
exports.exampleModel = [];
exports.Flight = [];
16 changes: 15 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"express": "^4.18.1",
"mongoose": "^6.4.0",
"nodemon": "^2.0.16"
"nodemon": "^2.0.16",
"uuid": "^9.0.0"
}
}
14 changes: 9 additions & 5 deletions routes/flightRoute.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const express = require('express');
const router = require('express').Router();
const controller = require("../controllers/flightController")

router.get ("/", controller.getAllFlights);
router.post ("/", controller.bookFlight);
router.get("/:id", controller.getEachFlight);
router.put("/:id", controller.updateFlight);
router.delete("/:id", controller.removeFlight);

const router = express.Router();
const controller = require('../controllers/flightController');

router.get('/', controller.example)

module.exports = router;

module.exports = router;