From 03cec3e1bafb11a7a9aad3daba829bb8de57ad05 Mon Sep 17 00:00:00 2001 From: moromoke01 Date: Sat, 5 Nov 2022 01:07:24 +0100 Subject: [PATCH 01/11] updated with commands required --- controllers/flightController.js | 109 +++++++++++++++++++++++++++++++- flight.rest | 27 ++++++++ index.js | 20 +++--- models/Flight.js | 2 +- package-lock.json | 16 ++++- package.json | 3 +- routes/flightRoute.js | 17 +++-- 7 files changed, 175 insertions(+), 19 deletions(-) create mode 100644 flight.rest diff --git a/controllers/flightController.js b/controllers/flightController.js index 43e6055..fa800dd 100644 --- a/controllers/flightController.js +++ b/controllers/flightController.js @@ -1,6 +1,109 @@ -exports.example = (req, res) => { - console.log("example") - res.send("Flight example") +const {flights}= require("../models/Flight"); +// const {v4:id} = require ('uuid') +const uuid = require("uuid").v4() + + +//get all flights booked +exports.getFlight = async (req, res) => { + try{ + const flight = flights; + + res.status(200).json({ + message: "ALL Flights", + BookedFlight: flight, + }); + }catch(err){ + res.status(500).json({ + message:err.message + }); + } +} + +//get a single flight +exports.getFlightEach = async(req, res) =>{ + try{ + let id = req.params.id; + const flightData = flights.find((flight) => flight.id === id); + + res.status(201).json({ + message: "flight found", + BookedFlight: flightData, + }); + }catch(err){ + res.status(500).json({ + message:err.message + }); + } + +} + +//add or book new flight +exports.CreateFlight = async (req, res) =>{ + try{ + //const bookflight = await req.body; + //console.log(bookflight); + const {title, time, price, date}= await req.body; + const newFlight ={ + id: uuid, + title, + time, + price, + date + }; + flights.push(newFlight); + + res.status(201).json({ + message: "New flight booked", + BookedFlight: newFlight + }); + + }catch(err){ + res.status(500).json({ + message:err.message + }); + } + } +//update flight record +exports.updateFlightData = async(req, res) =>{ + try{ + let id = req.params.id; + const flightRecord = flights.find((flight) => flight.id === id); + const {title, time, price, date}= await req.body; + flightRecord.title = title; + flightRecord.time = time; + flightRecord.price = price; + flightRecord.date = date; + + res.status(200).json({ + message: "flight data updated", + BookedFlight: flightRecord + }); + + }catch(err){ + res.status(500).json({ + message:err.message + }); + } + +} + +//Delete or cancel Booked flight +exports.DeleteFlightRecord = async (req, res) =>{ + try{ + let id= req.params.id; + const flightInfo = flights.find((flight) => flight.id ===id); + flights.splice(flights.indexOf(flight), 1); + res.status(200).json({ + message: "flight Data deleted", + BookedFlight: flightInfo + }); + + }catch(err){ + res.status(500).json({ + message:err.message + }); + } +} \ No newline at end of file diff --git a/flight.rest b/flight.rest new file mode 100644 index 0000000..2f3c565 --- /dev/null +++ b/flight.rest @@ -0,0 +1,27 @@ +@url = "http://localhost:3000/" + +GET http://localhost:3000/flights HTTP/1.1 + +### +GET http://localhost:3000/flights/id HTTP/1.1 + +### +POST http://localhost:3000/flights HTTP/1.1 +Content-Type: "application/json" + +{ + "title": " Janet", + "time": "10am", + "price":"300000", + "date": "20-2-2023" +} + +PUT http://localhost:3000/flights HTTP/1.1 +Content-Type: "application/json" + +{ + "title": " Moromoke", + "time": "11am", + "price":"500000", + "date": "20-12-2022" +} \ No newline at end of file diff --git a/index.js b/index.js index 9719349..b43c4a1 100644 --- a/index.js +++ b/index.js @@ -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 app = express(); +const flight = require('./routes/flightRoute.js'); +const app = express(); app.use(json()); -app.use("/", routes); -const port = process.env.PORT || 3000; +app.use("/flights", flight) + +app.get("/", (req, res) =>{ + res.send("Jane is building flight App"); +}) + + +const PORT = process.env.PORT || 3000; -app.listen(port, () => { - console.log(`Server is running on port ${port}`); + app.listen(PORT, () => { + console.log(`Server is running on port ${PORT}`); }); diff --git a/models/Flight.js b/models/Flight.js index e202b11..6b8588c 100644 --- a/models/Flight.js +++ b/models/Flight.js @@ -1 +1 @@ -exports.exampleModel = []; \ No newline at end of file +exports.flights = []; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index c27064a..b9c0126 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,8 @@ "dependencies": { "express": "^4.18.1", "mongoose": "^6.4.0", - "nodemon": "^2.0.16" + "nodemon": "^2.0.16", + "uuid": "^9.0.0" } }, "node_modules/@sindresorhus/is": { @@ -1955,6 +1956,14 @@ "node": ">= 0.4.0" } }, + "node_modules/uuid": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", + "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", @@ -3454,6 +3463,11 @@ "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==" }, + "uuid": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz", + "integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==" + }, "vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index feada44..926d24b 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/routes/flightRoute.js b/routes/flightRoute.js index ebd9c2d..fa40841 100644 --- a/routes/flightRoute.js +++ b/routes/flightRoute.js @@ -1,9 +1,16 @@ -const express = require('express'); +const router = require('express').Router() -const router = express.Router(); -const controller = require('../controllers/flightController'); +// const express = require('express'); +// const router = express.Router(); +const controller = require("../controllers/flightController") -router.get('/', controller.example) +router.get("/", controller.getFlight) +router.get("/:id", controller.getFlightEach) +router.post("/", controller.CreateFlight) +router.post("/:id", controller.updateFlightData) +router.delete("/:id", controller.DeleteFlightRecord) -module.exports = router; + + +module.exports =router; From 41d66b2f12bf96bd97ce51ec6db8eeb0cd3e81cb Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Mon, 7 Nov 2022 16:30:08 +0100 Subject: [PATCH 02/11] Update flightController.js --- controllers/flightController.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/controllers/flightController.js b/controllers/flightController.js index fa800dd..3cf772f 100644 --- a/controllers/flightController.js +++ b/controllers/flightController.js @@ -1,5 +1,5 @@ const {flights}= require("../models/Flight"); -// const {v4:id} = require ('uuid') +// const {v4:uuid} = require ('uuid') const uuid = require("uuid").v4() @@ -44,7 +44,7 @@ exports.CreateFlight = async (req, res) =>{ //console.log(bookflight); const {title, time, price, date}= await req.body; const newFlight ={ - id: uuid, + id: uuid(), title, time, price, @@ -106,4 +106,4 @@ exports.DeleteFlightRecord = async (req, res) =>{ }); } -} \ No newline at end of file +} From 3c7c21de0afb5b2e5dc252b061e1349f021711f1 Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Mon, 7 Nov 2022 16:31:00 +0100 Subject: [PATCH 03/11] Update flightController.js --- controllers/flightController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/flightController.js b/controllers/flightController.js index 3cf772f..70d3a1e 100644 --- a/controllers/flightController.js +++ b/controllers/flightController.js @@ -1,6 +1,6 @@ const {flights}= require("../models/Flight"); -// const {v4:uuid} = require ('uuid') -const uuid = require("uuid").v4() +const {v4:uuid} = require ('uuid') +//const uuid = require("uuid").v4() //get all flights booked From 790f6ba77326d5aae0f6ac108c006dec7cca56a4 Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Mon, 7 Nov 2022 16:33:05 +0100 Subject: [PATCH 04/11] Update flight.rest --- flight.rest | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/flight.rest b/flight.rest index 2f3c565..499e675 100644 --- a/flight.rest +++ b/flight.rest @@ -16,6 +16,7 @@ Content-Type: "application/json" "date": "20-2-2023" } +### PUT http://localhost:3000/flights HTTP/1.1 Content-Type: "application/json" @@ -24,4 +25,7 @@ Content-Type: "application/json" "time": "11am", "price":"500000", "date": "20-12-2022" -} \ No newline at end of file +} + +### +DELETE http://localhost:3000/Flight/ HTTP/1.1 From fd56bcc09c958381091993467994403c50be458e Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:51:46 +0100 Subject: [PATCH 05/11] Update flightController.js --- controllers/flightController.js | 151 ++++++++++++++++---------------- 1 file changed, 77 insertions(+), 74 deletions(-) diff --git a/controllers/flightController.js b/controllers/flightController.js index 70d3a1e..5f344b5 100644 --- a/controllers/flightController.js +++ b/controllers/flightController.js @@ -1,109 +1,112 @@ -const {flights}= require("../models/Flight"); -const {v4:uuid} = require ('uuid') -//const uuid = require("uuid").v4() +const { Flight } = require("../model/Flight"); +//const uuid = require('uuid').v4(); +const {v4: uuid} = require("uuid"); -//get all flights booked -exports.getFlight = async (req, res) => { - try{ - const flight = flights; - res.status(200).json({ - message: "ALL Flights", - BookedFlight: flight, - }); - }catch(err){ - res.status(500).json({ - message:err.message - }); - } -} -//get a single flight -exports.getFlightEach = async(req, res) =>{ +//get all flights +exports.getAllFlights = async (req, res)=>{ try{ - let id = req.params.id; - const flightData = flights.find((flight) => flight.id === id); + 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 found", - BookedFlight: flightData, + message: "flight booked", + newFlight, }); - }catch(err){ + } catch(err){ res.status(500).json({ - message:err.message - }); - } - + message: err.message + }) +} } -//add or book new flight -exports.CreateFlight = async (req, res) =>{ +//fetch single user +exports.getEachFlight = async (req, res)=>{ try{ - //const bookflight = await req.body; - //console.log(bookflight); - const {title, time, price, date}= await req.body; - const newFlight ={ - id: uuid(), - title, - time, - price, - date - }; - flights.push(newFlight); - - res.status(201).json({ - message: "New flight booked", - BookedFlight: newFlight + id= req.params.id; + const EachFlight = Flight.find((flight) => flight.id === id); + + if(EachFlight) + return res.status(200).json({ + message: "Flight record found", + EachFlight, }); - - }catch(err){ + res.status(404).json({ + message: "Flight record not found in database", + }); + } + catch(err){ res.status(500).json({ - message:err.message - }); - } - + message: err.message + }) +} } -//update flight record -exports.updateFlightData = async(req, res) =>{ +//update users +exports.updateFlight = async (req, res) =>{ try{ - let id = req.params.id; - const flightRecord = flights.find((flight) => flight.id === id); - const {title, time, price, date}= await req.body; - flightRecord.title = title; - flightRecord.time = time; - flightRecord.price = price; - flightRecord.date = date; + 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", - BookedFlight: flightRecord + FlightUpdate, }); - - }catch(err){ + } catch(err){ res.status(500).json({ message:err.message }); } - } -//Delete or cancel Booked flight -exports.DeleteFlightRecord = async (req, res) =>{ +//delete user +exports.removeFlight = async(req, res) =>{ try{ - let id= req.params.id; - const flightInfo = flights.find((flight) => flight.id ===id); - flights.splice(flights.indexOf(flight), 1); + id = req.params.id; + const deleteFlight = Flight.find((flight) => flight.id === id); + Flight.splice(Flight.indexOf(deleteFlight), 1) + res.status(200).json({ - message: "flight Data deleted", - BookedFlight: flightInfo + message: "Booked flight removed successfully", + }); - - }catch(err){ + } catch(err){ res.status(500).json({ message:err.message }); } - } From 3111533a2f9653fbb139561eabb417532918386b Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:52:43 +0100 Subject: [PATCH 06/11] Update flightController.js --- controllers/flightController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/flightController.js b/controllers/flightController.js index 5f344b5..cf19419 100644 --- a/controllers/flightController.js +++ b/controllers/flightController.js @@ -1,4 +1,4 @@ -const { Flight } = require("../model/Flight"); +const { Flight } = require("../models/Flight"); //const uuid = require('uuid').v4(); const {v4: uuid} = require("uuid"); From 3b0ebc6c077a5c06069558280251495fceb1e147 Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:54:05 +0100 Subject: [PATCH 07/11] Update flightRoute.js --- routes/flightRoute.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/routes/flightRoute.js b/routes/flightRoute.js index fa40841..f5f9662 100644 --- a/routes/flightRoute.js +++ b/routes/flightRoute.js @@ -1,16 +1,13 @@ -const router = require('express').Router() - -// const express = require('express'); -// const router = express.Router(); +const router = require('express').Router(); const controller = require("../controllers/flightController") -router.get("/", controller.getFlight) -router.get("/:id", controller.getFlightEach) -router.post("/", controller.CreateFlight) -router.post("/:id", controller.updateFlightData) -router.delete("/:id", controller.DeleteFlightRecord) +router.get ("/", controller.getAllFlights); +router.post ("/", controller.bookFlight); +router.get("/:id", controller.getEachFlight); +router.put("/:id", controller.updateFlight); +router.delete("/:id", controller.removeFlight); -module.exports =router; +module.exports = router; From 9590f4f6c5ec1f9d8c6ffe29e18531c258435300 Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:55:01 +0100 Subject: [PATCH 08/11] Update Flight.js --- models/Flight.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/Flight.js b/models/Flight.js index 6b8588c..7c98738 100644 --- a/models/Flight.js +++ b/models/Flight.js @@ -1 +1 @@ -exports.flights = []; \ No newline at end of file +exports.Flight = []; From 0847ae4188f467bcdcb23ae79bff120ce2aac92d Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:56:40 +0100 Subject: [PATCH 09/11] Update index.js --- index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index b43c4a1..782cc05 100644 --- a/index.js +++ b/index.js @@ -1,21 +1,21 @@ + const express = require("express"); -const { json } = require("express"); +const {v4: uuid} = require('uuid'); +const { json }= require("express"); const flight = require('./routes/flightRoute.js'); const app = express(); app.use(json()); +app.use("/Flight", flight); -app.use("/flights", flight) -app.get("/", (req, res) =>{ - res.send("Jane is building flight App"); -}) +app.get("/", (req, res)=>{ + res.send("working on a project"); + +}) const PORT = process.env.PORT || 3000; - - app.listen(PORT, () => { - console.log(`Server is running on port ${PORT}`); -}); +app.listen(PORT,()=>console.log(`serving on port ${PORT}`)); From 81f95070f0cd0303d04930b6a069cedbe9b7571d Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:57:39 +0100 Subject: [PATCH 10/11] Update flight.rest --- flight.rest | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/flight.rest b/flight.rest index 499e675..c0b36d5 100644 --- a/flight.rest +++ b/flight.rest @@ -1,31 +1,30 @@ @url = "http://localhost:3000/" - -GET http://localhost:3000/flights HTTP/1.1 +GET http://localhost:3000/Flight HTTP/1.1 ### -GET http://localhost:3000/flights/id HTTP/1.1 +GET http://localhost:3000/Flight/d3e6040c-6fd1-42ab-a9d8-61c5b9e29165 HTTP/1. ### -POST http://localhost:3000/flights HTTP/1.1 -Content-Type: "application/json" +POST http://localhost:3000/Flight HTTP/1.1 +Content-Type: application/json { - "title": " Janet", - "time": "10am", - "price":"300000", - "date": "20-2-2023" + "title": "niyi", + "time": "10am", + "price":"300000", + "date": "20-2-2023" } ### -PUT http://localhost:3000/flights HTTP/1.1 -Content-Type: "application/json" +PUT http://localhost:3000/Flight/cc0bdcfb-7989-4816-8b3f-6f4bac97072f HTTP/1.1 +Content-Type: application/json { - "title": " Moromoke", + "title": "Janet", "time": "11am", - "price":"500000", + "price": "500000", "date": "20-12-2022" } ### -DELETE http://localhost:3000/Flight/ HTTP/1.1 +DELETE http://localhost:3000/Flight/d3e6040c-6fd1-42ab-a9d8-61c5b9e29165 From 397f00e2cb58229ada81777c1011cd35ae3951b0 Mon Sep 17 00:00:00 2001 From: moromoke01 <56760496+moromoke01@users.noreply.github.com> Date: Fri, 18 Nov 2022 22:59:38 +0100 Subject: [PATCH 11/11] This is A REST_API for flight bookings --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..c1d1144 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# booking-flight-api +booking flight app clone