Skip to content

Commit d708b23

Browse files
authored
Merge pull request RiddhiM170904#531 from haseebzaki-07/new_branch_9
Add tickets availability
2 parents 0753c47 + 0f0a441 commit d708b23

File tree

5 files changed

+362
-0
lines changed

5 files changed

+362
-0
lines changed

backend/dataset/tickets.js

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
const availableTickets = [
2+
{
3+
transport: 'Bus',
4+
departureTime: '2024-11-09 08:00',
5+
arrivalTime: '2024-11-09 12:00',
6+
price: 30,
7+
source: 'New York',
8+
destination: 'Los Angeles',
9+
},
10+
{
11+
transport: 'Train',
12+
departureTime: '2024-11-09 10:00',
13+
arrivalTime: '2024-11-09 18:00',
14+
price: 100,
15+
source: 'New York',
16+
destination: 'Los Angeles',
17+
},
18+
{
19+
transport: 'Flight',
20+
departureTime: '2024-11-09 06:00',
21+
arrivalTime: '2024-11-09 09:00',
22+
price: 250,
23+
source: 'New York',
24+
destination: 'Los Angeles',
25+
},
26+
{
27+
transport: 'Bus',
28+
departureTime: '2024-11-09 15:00',
29+
arrivalTime: '2024-11-09 19:00',
30+
price: 35,
31+
source: 'Chicago',
32+
destination: 'San Francisco',
33+
},
34+
{
35+
transport: 'Train',
36+
departureTime: '2024-11-09 13:00',
37+
arrivalTime: '2024-11-09 21:00',
38+
price: 120,
39+
source: 'Chicago',
40+
destination: 'San Francisco',
41+
},
42+
{
43+
transport: 'Flight',
44+
departureTime: '2024-11-09 11:00',
45+
arrivalTime: '2024-11-09 14:00',
46+
price: 180,
47+
source: 'Chicago',
48+
destination: 'San Francisco',
49+
},
50+
{
51+
transport: 'Bus',
52+
departureTime: '2024-11-09 09:00',
53+
arrivalTime: '2024-11-09 13:30',
54+
price: 40,
55+
source: 'Boston',
56+
destination: 'Washington DC',
57+
},
58+
{
59+
transport: 'Train',
60+
departureTime: '2024-11-09 14:00',
61+
arrivalTime: '2024-11-09 18:30',
62+
price: 90,
63+
source: 'Boston',
64+
destination: 'Washington DC',
65+
},
66+
{
67+
transport: 'Flight',
68+
departureTime: '2024-11-09 07:30',
69+
arrivalTime: '2024-11-09 09:00',
70+
price: 160,
71+
source: 'Boston',
72+
destination: 'Washington DC',
73+
},
74+
{
75+
transport: 'Bus',
76+
departureTime: '2024-11-09 10:00',
77+
arrivalTime: '2024-11-09 14:00',
78+
price: 50,
79+
source: 'Miami',
80+
destination: 'Atlanta',
81+
},
82+
{
83+
transport: 'Train',
84+
departureTime: '2024-11-09 16:00',
85+
arrivalTime: '2024-11-09 22:00',
86+
price: 110,
87+
source: 'Miami',
88+
destination: 'Atlanta',
89+
},
90+
{
91+
transport: 'Flight',
92+
departureTime: '2024-11-09 08:30',
93+
arrivalTime: '2024-11-09 10:00',
94+
price: 190,
95+
source: 'Miami',
96+
destination: 'Atlanta',
97+
},
98+
{
99+
transport: 'Bus',
100+
departureTime: '2024-11-09 11:00',
101+
arrivalTime: '2024-11-09 18:30',
102+
price: 55,
103+
source: 'Los Angeles',
104+
destination: 'San Francisco',
105+
},
106+
{
107+
transport: 'Train',
108+
departureTime: '2024-11-09 18:00',
109+
arrivalTime: '2024-11-09 22:00',
110+
price: 75,
111+
source: 'Los Angeles',
112+
destination: 'San Francisco',
113+
},
114+
{
115+
transport: 'Flight',
116+
departureTime: '2024-11-09 12:00',
117+
arrivalTime: '2024-11-09 14:00',
118+
price: 220,
119+
source: 'Los Angeles',
120+
destination: 'San Francisco',
121+
},
122+
{
123+
transport: 'Bus',
124+
departureTime: '2024-11-09 13:00',
125+
arrivalTime: '2024-11-09 19:30',
126+
price: 60,
127+
source: 'Dallas',
128+
destination: 'Houston',
129+
},
130+
{
131+
transport: 'Train',
132+
departureTime: '2024-11-09 07:00',
133+
arrivalTime: '2024-11-09 11:00',
134+
price: 80,
135+
source: 'Dallas',
136+
destination: 'Houston',
137+
},
138+
{
139+
transport: 'Flight',
140+
departureTime: '2024-11-09 10:30',
141+
arrivalTime: '2024-11-09 11:50',
142+
price: 210,
143+
source: 'Dallas',
144+
destination: 'Houston',
145+
},
146+
{
147+
transport: 'Bus',
148+
departureTime: '2024-11-09 12:00',
149+
arrivalTime: '2024-11-09 16:30',
150+
price: 45,
151+
source: 'Seattle',
152+
destination: 'Portland',
153+
},
154+
{
155+
transport: 'Train',
156+
departureTime: '2024-11-09 09:30',
157+
arrivalTime: '2024-11-09 14:00',
158+
price: 95,
159+
source: 'Seattle',
160+
destination: 'Portland',
161+
},
162+
{
163+
transport: 'Flight',
164+
departureTime: '2024-11-09 08:00',
165+
arrivalTime: '2024-11-09 09:30',
166+
price: 175,
167+
source: 'Seattle',
168+
destination: 'Portland',
169+
},
170+
];
171+
172+
export default availableTickets;
173+

backend/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ import stationRoutes from "./routes/stationRoutes.js";
3232
import trainRoutes from "./routes/trainRoutes.js";
3333
import contactUs from "./routes/contactUsRouter.js";
3434
import complaintRoutes from "./routes/complaintRoutes.js";
35+
36+
import ticketRoutes from "./routes/ticketRoutes.js";
37+
3538
import userRoutes from "./routes/userRoutes.js";
3639

40+
3741
app.use("/auth", authRoutes);
3842
app.use("/api", authRoutes);
3943
app.use("/api", userRoutes);
4044
app.use("/api", complaintRoutes);
45+
app.use("/api", ticketRoutes);
4146
app.use("/station", stationRoutes);
4247
app.use("/train", trainRoutes);
4348
app.use("/contact", contactUs);

backend/routes/ticketRoutes.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import availableTickets from "../dataset/tickets.js";
2+
import express from "express";
3+
const router = express.Router();
4+
5+
6+
router.get('/get-all-tickets', function(req, res) {
7+
res.json(availableTickets);
8+
});
9+
10+
11+
router.post('/search-tickets', (req, res) => {
12+
const { source, destination } = req.body;
13+
14+
// Validate input
15+
if (!source || !destination) {
16+
return res.status(400).json({ error: 'Both source and destination must be provided.' });
17+
}
18+
19+
// Filter tickets based on source and destination
20+
const filteredTickets = availableTickets.filter(
21+
(ticket) =>
22+
ticket.source.toLowerCase() === source.toLowerCase() &&
23+
ticket.destination.toLowerCase() === destination.toLowerCase()
24+
);
25+
26+
if (filteredTickets.length === 0) {
27+
return res.status(404).json({ message: 'No tickets found for this route.' });
28+
}
29+
30+
res.json(filteredTickets);
31+
});
32+
33+
export default router;

frontend/src/App.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ import ComplainBox from "./Pages/ComplainBox";
3030
import Metadata from "./metadata";
3131
import SettingsPage from "./Pages/Settings";
3232
import Faq from './Pages/Faq';
33+
34+
import TicketSearchComponent from "./Pages/TicketsAvailability";
35+
3336
import ProfilePage from "./Pages/Profile";
3437

38+
3539
function App() {
3640
return (
3741
<>
@@ -50,7 +54,11 @@ function App() {
5054
<Route path="/Notification" element={<NotificationPage />} />
5155
<Route path="/chatbot" element={<Chatbot />} />
5256
<Route path="/ContactUs" element={<ContactUs />} />
57+
58+
<Route path="/Tickets" element={<TicketSearchComponent/>} />
59+
5360
<Route path="/profile" element={<ProfilePage />} />
61+
5462

5563
<Route path="/GoogleTranslate" element={<GoogleTranslate />} />
5664
<Route path="/help" element={<Help />} />
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import React, { useState } from 'react';
2+
3+
const TicketSearchComponent = () => {
4+
const [source, setSource] = useState('');
5+
const [destination, setDestination] = useState('');
6+
const [tickets, setTickets] = useState([]);
7+
const [error, setError] = useState('');
8+
const [loading, setLoading] = useState(false);
9+
10+
// Handle input changes
11+
const handleInputChange = (e) => {
12+
const { name, value } = e.target;
13+
if (name === 'source') {
14+
setSource(value);
15+
} else {
16+
setDestination(value);
17+
}
18+
};
19+
20+
// Search for tickets
21+
const handleSearch = async (e) => {
22+
e.preventDefault();
23+
24+
if (!source || !destination) {
25+
setError('Please enter both source and destination.');
26+
return;
27+
}
28+
29+
setError('');
30+
setLoading(true);
31+
32+
try {
33+
const ticketData = await fetchTickets(source, destination);
34+
setTickets(ticketData);
35+
} catch (error) {
36+
console.error('Error fetching tickets:', error);
37+
setError('There was an error fetching the tickets.');
38+
} finally {
39+
setLoading(false);
40+
}
41+
};
42+
43+
// Fetch tickets from the backend
44+
const fetchTickets = async (source, destination) => {
45+
try {
46+
const response = await fetch('http://localhost:3000/api/search-tickets', {
47+
method: 'POST',
48+
headers: {
49+
'Content-Type': 'application/json',
50+
},
51+
body: JSON.stringify({ source, destination }),
52+
});
53+
54+
if (response.ok) {
55+
const data = await response.json();
56+
return data;
57+
} else {
58+
throw new Error('Failed to fetch tickets');
59+
}
60+
} catch (error) {
61+
console.error('Error fetching tickets:', error);
62+
setError('Failed to fetch tickets.');
63+
return [];
64+
}
65+
};
66+
67+
return (
68+
<div className="ticket-search max-w-3xl mx-auto p-6 bg-white rounded-lg shadow-lg mt-8">
69+
<h2 className="text-3xl font-semibold text-green-600 mb-6 text-center">Tickets Availability</h2>
70+
71+
<form onSubmit={handleSearch} className="space-y-6">
72+
<div>
73+
<label htmlFor="source" className="block text-lg font-medium text-gray-700">
74+
Departure (Source)
75+
</label>
76+
<input
77+
type="text"
78+
id="source"
79+
name="source"
80+
value={source}
81+
onChange={handleInputChange}
82+
placeholder="Enter departure location"
83+
className="w-full p-3 mt-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-green-600 transition"
84+
required
85+
/>
86+
</div>
87+
88+
<div>
89+
<label htmlFor="destination" className="block text-lg font-medium text-gray-700">
90+
Destination
91+
</label>
92+
<input
93+
type="text"
94+
id="destination"
95+
name="destination"
96+
value={destination}
97+
onChange={handleInputChange}
98+
placeholder="Enter destination location"
99+
className="w-full p-3 mt-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-green-600 transition"
100+
required
101+
/>
102+
</div>
103+
104+
<button
105+
type="submit"
106+
className="w-full py-3 bg-green-600 text-white text-lg font-medium rounded-lg hover:bg-green-500 transition-colors"
107+
>
108+
Search
109+
</button>
110+
</form>
111+
112+
{error && <p className="mt-4 text-red-500 text-center">{error}</p>}
113+
114+
{loading && <p className="mt-4 text-blue-600 text-center">Loading...</p>}
115+
116+
{/* Displaying search results */}
117+
{tickets.length > 0 && (
118+
<div className="mt-6">
119+
<h3 className="text-2xl font-semibold text-green-600 mb-4">Available Tickets</h3>
120+
<ul className="space-y-4">
121+
{tickets.map((ticket, index) => (
122+
<li
123+
key={index}
124+
className="p-6 bg-gray-50 border border-gray-300 rounded-lg shadow-md hover:shadow-lg transition-shadow"
125+
>
126+
<p className="text-xl font-semibold text-gray-800">{ticket.transport}</p>
127+
<p><strong>Departure:</strong> {ticket.departureTime}</p>
128+
<p><strong>Arrival:</strong> {ticket.arrivalTime}</p>
129+
<p><strong>Price:</strong> ${ticket.price}</p>
130+
</li>
131+
))}
132+
</ul>
133+
</div>
134+
)}
135+
136+
{tickets.length === 0 && source && destination && !error && !loading && (
137+
<p className="text-green-600 text-center">No tickets found for this route.</p>
138+
)}
139+
</div>
140+
);
141+
};
142+
143+
export default TicketSearchComponent;

0 commit comments

Comments
 (0)