mirror of
https://github.com/clarencechaan/ufc-cal.git
synced 2026-10-19 00:38:30 -07:00
Implement getEvents function in api.js
This commit is contained in:
1 parent
2f5a30fadf
commit
a2ec15795d
1 file changed
+38
-8
@@ -23,10 +23,12 @@ async function fetchEvent(eventId) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Using the UFC API, search for the latest event ID, then request each event
|
* Using the UFC API, search for the latest event ID, then request each event
|
||||||
* preceding it until we reach an event that occurred 90 days before now.
|
* preceding it until we reach an event that occurred 90 days before now, then
|
||||||
* Finally, create events.json containing these events
|
* create events.json containing these events.
|
||||||
|
* Finally return an array of these events
|
||||||
*
|
*
|
||||||
* @returns {Promise<void>} - A Promise that resolves when events.json is created
|
* @returns {Promise<Array<object>>} - A Promise that resolves with the array
|
||||||
|
* of details of recent and upcoming UFC events
|
||||||
*/
|
*/
|
||||||
async function initializeEvents() {
|
async function initializeEvents() {
|
||||||
// Binary search for the event with the highest valid event ID
|
// Binary search for the event with the highest valid event ID
|
||||||
@@ -71,8 +73,10 @@ async function initializeEvents() {
|
|||||||
console.log("Creating events.json");
|
console.log("Creating events.json");
|
||||||
|
|
||||||
// Write the fetched events to events.json
|
// Write the fetched events to events.json
|
||||||
fetchedEvents = JSON.stringify(fetchedEvents, null, 2);
|
const json = JSON.stringify(fetchedEvents, null, 2);
|
||||||
writeFile("./events.json", fetchedEvents);
|
writeFile("./events.json", json);
|
||||||
|
|
||||||
|
return fetchedEvents;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
@@ -80,8 +84,10 @@ async function initializeEvents() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Update upcoming events in events.json, and add newly found events to events.json
|
* Update upcoming events in events.json, and add newly found events to events.json
|
||||||
|
* Return an array of these events
|
||||||
*
|
*
|
||||||
* @returns {Promise<void>} - A Promise that resolves when events.json is updated
|
* @returns {Promise<Array<object>>} - A Promise that resolves with the array
|
||||||
|
* of details of recent and upcoming UFC events
|
||||||
*/
|
*/
|
||||||
async function updateEventsFile() {
|
async function updateEventsFile() {
|
||||||
try {
|
try {
|
||||||
@@ -120,9 +126,33 @@ async function updateEventsFile() {
|
|||||||
console.log("Updating events.json");
|
console.log("Updating events.json");
|
||||||
|
|
||||||
// Write the newly added and updated events to events.json
|
// Write the newly added and updated events to events.json
|
||||||
events = JSON.stringify(events, null, 2);
|
const json = JSON.stringify(events, null, 2);
|
||||||
writeFile("./events.json", events);
|
writeFile("./events.json", json);
|
||||||
|
|
||||||
|
return events;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create events.json if it does not exist, otherwise update events.json.
|
||||||
|
* Return an array of recent and upcoming events
|
||||||
|
*
|
||||||
|
* @returns {Promise<Array<object>>} - A Promise that resolves with the array
|
||||||
|
* of details of recent and upcoming UFC events
|
||||||
|
*/
|
||||||
|
async function getEvents() {
|
||||||
|
let events;
|
||||||
|
|
||||||
|
try {
|
||||||
|
events = await updateEventsFile();
|
||||||
|
if (!events) events = await initializeEvents();
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return events;
|
||||||
|
}
|
||||||
|
|
||||||
|
export { getEvents };
|
||||||
Reference in new issue
Block a user