![]() |
Home | Accounts | Setup | Verify | Play | Hacks |
DOCSE 5 things
5 things with a complete explanation
- 1. Static API
- 2. Dynamic API
- 3. Backend XII Engineer/customizing backend website and creating screens for admin functions
- 4. Kanban Board/planning for all weeks
- 5. Issues/planning with group
1. Static API
async function FindLocations() {
const location = document
.getElementById("location")
.value.trim()
.replace(/\s+/g, "+");
const place = document
.getElementById("place")
.value.trim()
.replace(/\s+/g, "+");
if (!place) {
alert("Please enter a valid city.");
return;
}
const url = `https://nominatim.openstreetmap.org/search?q=${location}+in+${place}&format=json&addressdetails=1&limit=10`;
try {
const response = await fetch(url, {
headers: {
"User-Agent": "MyWaypointApp/1.0 (contact@example.com)",
},
});
if (!response.ok) {
console.error(`HTTP Error: ${response.status}`);
alert(`Failed to fetch data. Status: ${response.status}`);
return;
}
const data = await response.json();
console.log(data);
// Clear previous results and markers
const resultsTableBody = document.querySelector("#resultsTable tbody");
resultsTableBody.innerHTML = ""; // Reset table content
map.eachLayer((layer) => {
if (layer instanceof L.Marker) {
map.removeLayer(layer);
}
});
if (data.length === 0) {
const noResultsRow = document.createElement("tr");
noResultsRow.innerHTML = `<td colspan="4">No locations found. Try a different query.</td>`;
resultsTableBody.appendChild(noResultsRow);
return;
}
// Process results...
} catch (error) {
console.error("Error fetching data:", error);
alert("An error occurred while fetching data. Please try again later.");
}
}
2. Dynamic API
// Data initialization
const data = [
{ _injury: "Fractures", _location: "Hospital" },
{ _injury: "Minor Cuts", _location: "Pharmacy" },
// ... other injury data ...
];
// API Functions
async function updateCareCenterData(waypointId, rating) {
try {
const response = await fetch(`${pythonURI}/api/waypoints`, {
...fetchOptions,
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
waypoint_id: parseInt(waypointId),
rating: parseInt(rating)
})
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Error updating rating:", error.message);
throw error;
}
}
async function getCareCenterData(currentUserID) {
try {
const response = await fetch(`${pythonURI}/api/waypoints?user_id=${currentUserID}`, {
...fetchOptions,
method: 'GET',
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
updateCareCenterTable(data);
} catch (error) {
console.error("Error fetching care center data:", error);
}
}
async function postCareCenterData(injury, location, address) {
try {
const response = await fetch(`${pythonURI}/api/waypoints`, {
...fetchOptions,
method: 'POST',
body: JSON.stringify({
injury: injury,
location: location,
address: address
})
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Error posting data:", error);
throw error;
}
}
// UI Update Functions
function updateCareCenterTable(data) {
const carecenterTable = document.querySelector("#carecenterTable tbody");
carecenterTable.innerHTML = "";
if (data.length === 0) {
const noResultsRow = document.createElement("tr");
noResultsRow.innerHTML = `<td colspan="6">No care center check-ins available.</td>`;
carecenterTable.appendChild(noResultsRow);
return;
}
data.forEach((waypoint, index) => {
const row = createCareCenterRow(waypoint, index);
carecenterTable.appendChild(row);
});
addEventListeners();
}
async function updateRating(waypointId, rating) {
try {
await updateCareCenterData(waypointId, rating);
const ratedRow = document.querySelector(`tr[data-waypointid="${waypointId}"]`);
const address = ratedRow.querySelector('td:nth-child(4)').textContent;
updateRatingDisplay(ratedRow, rating);
updateSearchResultsRating(address, rating);
} catch (error) {
console.error('Error updating rating:', error);
alert('Failed to update rating. Please try again.');
}
}