diff --git a/backend/dataset/tickets.js b/backend/dataset/tickets.js new file mode 100644 index 0000000..0b1fcb6 --- /dev/null +++ b/backend/dataset/tickets.js @@ -0,0 +1,173 @@ +const availableTickets = [ + { + transport: 'Bus', + departureTime: '2024-11-09 08:00', + arrivalTime: '2024-11-09 12:00', + price: 30, + source: 'New York', + destination: 'Los Angeles', + }, + { + transport: 'Train', + departureTime: '2024-11-09 10:00', + arrivalTime: '2024-11-09 18:00', + price: 100, + source: 'New York', + destination: 'Los Angeles', + }, + { + transport: 'Flight', + departureTime: '2024-11-09 06:00', + arrivalTime: '2024-11-09 09:00', + price: 250, + source: 'New York', + destination: 'Los Angeles', + }, + { + transport: 'Bus', + departureTime: '2024-11-09 15:00', + arrivalTime: '2024-11-09 19:00', + price: 35, + source: 'Chicago', + destination: 'San Francisco', + }, + { + transport: 'Train', + departureTime: '2024-11-09 13:00', + arrivalTime: '2024-11-09 21:00', + price: 120, + source: 'Chicago', + destination: 'San Francisco', + }, + { + transport: 'Flight', + departureTime: '2024-11-09 11:00', + arrivalTime: '2024-11-09 14:00', + price: 180, + source: 'Chicago', + destination: 'San Francisco', + }, + { + transport: 'Bus', + departureTime: '2024-11-09 09:00', + arrivalTime: '2024-11-09 13:30', + price: 40, + source: 'Boston', + destination: 'Washington DC', + }, + { + transport: 'Train', + departureTime: '2024-11-09 14:00', + arrivalTime: '2024-11-09 18:30', + price: 90, + source: 'Boston', + destination: 'Washington DC', + }, + { + transport: 'Flight', + departureTime: '2024-11-09 07:30', + arrivalTime: '2024-11-09 09:00', + price: 160, + source: 'Boston', + destination: 'Washington DC', + }, + { + transport: 'Bus', + departureTime: '2024-11-09 10:00', + arrivalTime: '2024-11-09 14:00', + price: 50, + source: 'Miami', + destination: 'Atlanta', + }, + { + transport: 'Train', + departureTime: '2024-11-09 16:00', + arrivalTime: '2024-11-09 22:00', + price: 110, + source: 'Miami', + destination: 'Atlanta', + }, + { + transport: 'Flight', + departureTime: '2024-11-09 08:30', + arrivalTime: '2024-11-09 10:00', + price: 190, + source: 'Miami', + destination: 'Atlanta', + }, + { + transport: 'Bus', + departureTime: '2024-11-09 11:00', + arrivalTime: '2024-11-09 18:30', + price: 55, + source: 'Los Angeles', + destination: 'San Francisco', + }, + { + transport: 'Train', + departureTime: '2024-11-09 18:00', + arrivalTime: '2024-11-09 22:00', + price: 75, + source: 'Los Angeles', + destination: 'San Francisco', + }, + { + transport: 'Flight', + departureTime: '2024-11-09 12:00', + arrivalTime: '2024-11-09 14:00', + price: 220, + source: 'Los Angeles', + destination: 'San Francisco', + }, + { + transport: 'Bus', + departureTime: '2024-11-09 13:00', + arrivalTime: '2024-11-09 19:30', + price: 60, + source: 'Dallas', + destination: 'Houston', + }, + { + transport: 'Train', + departureTime: '2024-11-09 07:00', + arrivalTime: '2024-11-09 11:00', + price: 80, + source: 'Dallas', + destination: 'Houston', + }, + { + transport: 'Flight', + departureTime: '2024-11-09 10:30', + arrivalTime: '2024-11-09 11:50', + price: 210, + source: 'Dallas', + destination: 'Houston', + }, + { + transport: 'Bus', + departureTime: '2024-11-09 12:00', + arrivalTime: '2024-11-09 16:30', + price: 45, + source: 'Seattle', + destination: 'Portland', + }, + { + transport: 'Train', + departureTime: '2024-11-09 09:30', + arrivalTime: '2024-11-09 14:00', + price: 95, + source: 'Seattle', + destination: 'Portland', + }, + { + transport: 'Flight', + departureTime: '2024-11-09 08:00', + arrivalTime: '2024-11-09 09:30', + price: 175, + source: 'Seattle', + destination: 'Portland', + }, + ]; + + export default availableTickets; + \ No newline at end of file diff --git a/backend/index.js b/backend/index.js index b67aed5..48de940 100644 --- a/backend/index.js +++ b/backend/index.js @@ -32,12 +32,17 @@ import stationRoutes from "./routes/stationRoutes.js"; import trainRoutes from "./routes/trainRoutes.js"; import contactUs from "./routes/contactUsRouter.js"; import complaintRoutes from "./routes/complaintRoutes.js"; + +import ticketRoutes from "./routes/ticketRoutes.js"; + import userRoutes from "./routes/userRoutes.js"; + app.use("/auth", authRoutes); app.use("/api", authRoutes); app.use("/api", userRoutes); app.use("/api", complaintRoutes); +app.use("/api", ticketRoutes); app.use("/station", stationRoutes); app.use("/train", trainRoutes); app.use("/contact", contactUs); diff --git a/backend/routes/ticketRoutes.js b/backend/routes/ticketRoutes.js new file mode 100644 index 0000000..e7579ae --- /dev/null +++ b/backend/routes/ticketRoutes.js @@ -0,0 +1,33 @@ +import availableTickets from "../dataset/tickets.js"; +import express from "express"; +const router = express.Router(); + + +router.get('/get-all-tickets', function(req, res) { + res.json(availableTickets); +}); + + +router.post('/search-tickets', (req, res) => { + const { source, destination } = req.body; + + // Validate input + if (!source || !destination) { + return res.status(400).json({ error: 'Both source and destination must be provided.' }); + } + + // Filter tickets based on source and destination + const filteredTickets = availableTickets.filter( + (ticket) => + ticket.source.toLowerCase() === source.toLowerCase() && + ticket.destination.toLowerCase() === destination.toLowerCase() + ); + + if (filteredTickets.length === 0) { + return res.status(404).json({ message: 'No tickets found for this route.' }); + } + + res.json(filteredTickets); +}); + +export default router; diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 6e38b81..42c3425 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -30,8 +30,12 @@ import ComplainBox from "./Pages/ComplainBox"; import Metadata from "./metadata"; import SettingsPage from "./Pages/Settings"; import Faq from './Pages/Faq'; + +import TicketSearchComponent from "./Pages/TicketsAvailability"; + import ProfilePage from "./Pages/Profile"; + function App() { return ( <> @@ -50,7 +54,11 @@ function App() { } /> } /> } /> + + } /> + } /> + } /> } /> diff --git a/frontend/src/Pages/TicketsAvailability.jsx b/frontend/src/Pages/TicketsAvailability.jsx new file mode 100644 index 0000000..a6dd58b --- /dev/null +++ b/frontend/src/Pages/TicketsAvailability.jsx @@ -0,0 +1,143 @@ +import React, { useState } from 'react'; + +const TicketSearchComponent = () => { + const [source, setSource] = useState(''); + const [destination, setDestination] = useState(''); + const [tickets, setTickets] = useState([]); + const [error, setError] = useState(''); + const [loading, setLoading] = useState(false); + + // Handle input changes + const handleInputChange = (e) => { + const { name, value } = e.target; + if (name === 'source') { + setSource(value); + } else { + setDestination(value); + } + }; + + // Search for tickets + const handleSearch = async (e) => { + e.preventDefault(); + + if (!source || !destination) { + setError('Please enter both source and destination.'); + return; + } + + setError(''); + setLoading(true); + + try { + const ticketData = await fetchTickets(source, destination); + setTickets(ticketData); + } catch (error) { + console.error('Error fetching tickets:', error); + setError('There was an error fetching the tickets.'); + } finally { + setLoading(false); + } + }; + + // Fetch tickets from the backend + const fetchTickets = async (source, destination) => { + try { + const response = await fetch('http://localhost:3000/api/search-tickets', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ source, destination }), + }); + + if (response.ok) { + const data = await response.json(); + return data; + } else { + throw new Error('Failed to fetch tickets'); + } + } catch (error) { + console.error('Error fetching tickets:', error); + setError('Failed to fetch tickets.'); + return []; + } + }; + + return ( +
+

Tickets Availability

+ +
+
+ + +
+ +
+ + +
+ + +
+ + {error &&

{error}

} + + {loading &&

Loading...

} + + {/* Displaying search results */} + {tickets.length > 0 && ( +
+

Available Tickets

+
    + {tickets.map((ticket, index) => ( +
  • +

    {ticket.transport}

    +

    Departure: {ticket.departureTime}

    +

    Arrival: {ticket.arrivalTime}

    +

    Price: ${ticket.price}

    +
  • + ))} +
+
+ )} + + {tickets.length === 0 && source && destination && !error && !loading && ( +

No tickets found for this route.

+ )} +
+ ); +}; + +export default TicketSearchComponent;