generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from agiledev-students-fall2023/anasofia
stopTimes functions
- Loading branch information
Showing
6 changed files
with
200 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const express = require("express"); | ||
const mongoose = require('mongoose'); | ||
|
||
const stopRoutes = () => { | ||
const router = express.Router(); | ||
|
||
const getDayOfWeek = () => { | ||
const today = new Date(); | ||
const dayOfWeek = today.getDay(); | ||
console.log(dayOfWeek); | ||
return dayOfWeek; | ||
} | ||
|
||
const routeRuns = (route_name, day_of_week) => { | ||
if ( | ||
(route_name === "B" && (day_of_week === 0 || day_of_week === 6)) || | ||
(route_name === "C" && (day_of_week === 0 || day_of_week === 6 || day_of_week === 5)) || | ||
(route_name === "E" && (day_of_week === 0 || day_of_week === 6)) || | ||
(route_name === "F" && (day_of_week === 0 || day_of_week === 6 || day_of_week === 5)) || | ||
(route_name === "W" && (day_of_week !== 0 && day_of_week !== 6)) | ||
) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
const dayToString = (day_of_week) => { | ||
if (day_of_week === 0 || day_of_week === 6) { | ||
return "Wknd"; | ||
} else if (day_of_week === 5) { | ||
return "F"; | ||
} else { | ||
return "Week"; | ||
} | ||
} | ||
|
||
router.get("/:stop_name/:route_name", async (req, res, next) => { | ||
const day_of_week = getDayOfWeek(); | ||
console.log(day_of_week); | ||
const { stop_name, route_name } = req.params; | ||
console.log(stop_name, route_name, "rpint"); | ||
const runs = routeRuns(route_name, day_of_week); | ||
|
||
if (!stop_name || !route_name) { | ||
return res.status(401).json({ | ||
success: false, | ||
message: "Invalid request. Please provide all required fields.", | ||
}); | ||
} | ||
if (!runs) { | ||
return res.status(200).json({ | ||
success: true, | ||
message: "Route is not running today.", | ||
}); | ||
} | ||
try { | ||
const StopModel = mongoose.model('Route' + route_name + dayToString(day_of_week)); | ||
const stop = await StopModel.findOne({ stop_name: { $regex: new RegExp(stop_name, 'i') } }); | ||
if (!stop) { | ||
console.log("Invalid request. Cannot find stop.") | ||
return res.status(404).json({ | ||
success: false, | ||
message: "Stop not found.", | ||
}); | ||
} | ||
return res.json({ | ||
success: true, | ||
message: "Stop found.", | ||
stop, | ||
}); | ||
} catch (err) { | ||
console.log("Invalid request. Please provide all required fields.") | ||
console.error(`Error fetching stop: ${err}`); | ||
return res.status(500).json({ | ||
success: false, | ||
message: "Error looking up stop in the database.", | ||
error: err, | ||
}); | ||
} | ||
}); | ||
|
||
return router; | ||
} | ||
|
||
module.exports = stopRoutes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import axios from "axios"; | ||
|
||
async function getShuttleTimes(stopName, route) { | ||
const url = `http://localhost:4000/stopfind/${stopName}/${route}`; | ||
return axios | ||
.get(url) | ||
.then((response) => { | ||
console.log(response.data); | ||
return response.data; | ||
}) | ||
.catch((error) => { | ||
console.error("Axios error:", error); | ||
throw error; | ||
}); | ||
} | ||
|
||
async function getTimes(stopName, route) { | ||
try { | ||
const response = await getShuttleTimes(stopName, route); | ||
const shuttleTimes = response.stop.times; | ||
return shuttleTimes; | ||
} catch (error) { | ||
console.error("Function error:", error); | ||
throw error; | ||
} | ||
} | ||
|
||
export async function getNextTimes(stopName, route) { | ||
try { | ||
const currentTime = new Date(); | ||
const allTimes = await getTimes(stopName, route); | ||
const upcomingTimes = allTimes | ||
.map(timeString => { | ||
const [time, period] = timeString.split(' '); | ||
const [hours, minutes] = time.split(':'); | ||
let adjustedHours = parseInt(hours, 10); | ||
|
||
if (period === 'PM' && adjustedHours !== 12) { | ||
adjustedHours += 12; | ||
} | ||
const timeObject = new Date(); | ||
timeObject.setHours(adjustedHours, parseInt(minutes, 10)); | ||
return timeObject; | ||
}) | ||
.filter(singleTime => { | ||
return singleTime >= currentTime; | ||
}) | ||
.map(time => time.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' })); | ||
console.log(upcomingTimes); | ||
return upcomingTimes; | ||
} catch (error) { | ||
console.error("Function error:", error); | ||
throw error; | ||
} | ||
} | ||
|
||
export async function timeRemaining(stopName, route) { | ||
try { | ||
const currentTime = new Date(); | ||
const allTimes = await getNextTimes(stopName, route); | ||
const upcomingTimes = allTimes.map(timeString => new Date(timeString)); | ||
const timeDifferences = upcomingTimes.map(upcomingTime => { | ||
const differenceInMilliseconds = upcomingTime - currentTime; | ||
const differenceInMinutes = differenceInMilliseconds / (1000 * 60); | ||
return differenceInMinutes; | ||
}); | ||
return timeDifferences; | ||
} catch (error) { | ||
console.error("Function error:", error); | ||
throw error; | ||
} | ||
} | ||
|
||
|
||
export default getShuttleTimes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters