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 diff --git a/controllers/flightController.js b/controllers/flightController.js index 43e6055..cf19419 100644 --- a/controllers/flightController.js +++ b/controllers/flightController.js @@ -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 + }); + } +} diff --git a/flight.rest b/flight.rest new file mode 100644 index 0000000..c0b36d5 --- /dev/null +++ b/flight.rest @@ -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 diff --git a/index.js b/index.js index 9719349..782cc05 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 {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}`)); diff --git a/models/Flight.js b/models/Flight.js index e202b11..7c98738 100644 --- a/models/Flight.js +++ b/models/Flight.js @@ -1 +1 @@ -exports.exampleModel = []; \ No newline at end of file +exports.Flight = []; 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..f5f9662 100644 --- a/routes/flightRoute.js +++ b/routes/flightRoute.js @@ -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;