From 5cb96a47e282c870a2d4532b07624acdfa8e0acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bahattin=20=C3=87ini=C3=A7?= Date: Mon, 27 May 2024 16:09:53 +0300 Subject: [PATCH] feat: Added missing API integrations --- api/handlers.go | 22 ++++----- ui/src/services/activities.js | 53 +++++++++++++++++++++ ui/src/services/athletes.js | 17 +++++++ ui/src/services/components.js | 86 +++++++++++++++++++++++++++++++++++ ui/src/services/dashboars.js | 85 +++++++++++++++++++++++++++++++++- ui/src/services/gears.js | 17 +++++++ ui/src/services/user.js | 20 +++++++- 7 files changed, 287 insertions(+), 13 deletions(-) create mode 100644 ui/src/services/components.js diff --git a/api/handlers.go b/api/handlers.go index 49403e2..306fd27 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -61,22 +61,22 @@ func (a *API) setupHandlers() { { dash := api.Group("/dashboards") - // Dashboard dash.GET("", a.listDashboards) dash.POST("", a.createDashboard) dash.GET("/:id", a.getDashboard, a.setDashboardMiddleware()) dash.PUT("/:id", a.updateDashboard, a.setDashboardMiddleware()) dash.DELETE("/:id", a.deleteDashboard, a.setDashboardMiddleware()) - dash.POST("/:id/run", a.runQuery, a.setDashboardMiddleware()) - // Component - dash.GET("/:id/components", a.getDashboardComponents, a.setDashboardMiddleware()) - dash.POST("/:id/components", a.createComponent, a.setDashboardMiddleware()) - dash.PUT("/:id/components/:cpid", a.updateComponent, - a.setDashboardMiddleware(), a.setComponentMiddleware()) - dash.DELETE("/:id/components/:cpid", a.deleteComponent, - a.setDashboardMiddleware(), a.setComponentMiddleware()) - dash.POST("/:id/components/:cpid/run", a.runComponent, - a.setDashboardMiddleware(), a.setComponentMiddleware()) + dash.POST("/:id/run", a.runDashboard, a.setDashboardMiddleware()) + + } + + { + comp := api.Group("/dashboards/:id/components", a.setDashboardMiddleware()) + comp.GET("", a.getDashboardComponents) + comp.POST("", a.createComponent) + comp.PUT("/:cpid", a.updateComponent, a.setComponentMiddleware()) + comp.DELETE("/:cpid", a.deleteComponent, a.setComponentMiddleware()) + comp.POST("/:cpid/run", a.runComponent, a.setComponentMiddleware()) } a.ec.GET("/*", a.serveStatic) diff --git a/ui/src/services/activities.js b/ui/src/services/activities.js index f0e1c1e..8d0802a 100644 --- a/ui/src/services/activities.js +++ b/ui/src/services/activities.js @@ -14,3 +14,56 @@ export async function fetchActivities(page) { return await response.json(); } + +export async function getActivity(id) { + const endpoint = `${API_BASE_URL}/activities/${id}`; + + const response = await fetch(endpoint, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Could not get activity'); + } + + return await response.json(); +} + +export async function getActivityGPX(id, accessToken) { + const endpoint = `${API_BASE_URL}/activities/${id}/gpx`; + + const response = await fetch(endpoint, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${accessToken}`, + }, + }); + + if (!response.ok) { + throw new Error('Could not get activity'); + } + + return await response.json(); +} + +export async function getActivityLaps(id, accessToken) { + const endpoint = `${API_BASE_URL}/activities/${id}/laps`; + + const response = await fetch(endpoint, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${accessToken}`, + }, + }); + + if (!response.ok) { + throw new Error('Could not get activity'); + } + + return await response.json(); +} diff --git a/ui/src/services/athletes.js b/ui/src/services/athletes.js index cc10e6f..1767164 100644 --- a/ui/src/services/athletes.js +++ b/ui/src/services/athletes.js @@ -14,3 +14,20 @@ export async function fetchAthletes(page) { return await response.json(); } + +export async function getAthlete(id) { + const endpoint = `${API_BASE_URL}/athletes/${id}`; + + const response = await fetch(endpoint, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Could not get athlete'); + } + + return await response.json(); +} diff --git a/ui/src/services/components.js b/ui/src/services/components.js new file mode 100644 index 0000000..7b9118e --- /dev/null +++ b/ui/src/services/components.js @@ -0,0 +1,86 @@ +import { API_BASE_URL } from './api'; + +export async function fetchComponents(dashId) { + const endpoint = `${API_BASE_URL}/dashboards/${dashId}/components`; + + const response = await fetch(endpoint, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Could not fetch dashboard components'); + } + + return await response.json(); +} + +export async function createComponent(dashId, data) { + const endpoint = `${API_BASE_URL}/dashboards/${dashId}/components`; + + const response = await fetch(endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(data), + }); + + if (!response.ok) { + throw new Error('Could not create a component'); + } + + return await response.json(); +} + +export async function updateComponent(dashId, compId, data) { + const endpoint = `${API_BASE_URL}/dashboards/${dashId}/components/${compId}`; + + const response = await fetch(endpoint, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(data), + }); + + if (!response.ok) { + throw new Error('Could not create a component'); + } + + return await response.json(); +} + +export async function deleteComponent(dashId, compId) { + const endpoint = `${API_BASE_URL}/dashboards/${dashId}/components/${compId}`; + + const response = await fetch(endpoint, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Could not delete a component'); + } +} + +export async function runComponent(dashId, compId) { + const endpoint = `${API_BASE_URL}/dashboards/${dashId}/components/${compId}`; + + const response = await fetch(endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Could not run component'); + } + + return await response.json(); +} diff --git a/ui/src/services/dashboars.js b/ui/src/services/dashboars.js index d1ec508..4cdc2b6 100644 --- a/ui/src/services/dashboars.js +++ b/ui/src/services/dashboars.js @@ -9,7 +9,90 @@ export async function fetchDashboards() { }); if (!response.ok) { - throw new Error('Network response was not ok'); + throw new Error('Could not fetch dashboards'); + } + + return await response.json(); +} + +export async function getDashboard(id) { + const endpoint = `${API_BASE_URL}/dashboards/${id}`; + + const response = await fetch(endpoint, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Could not get dashboard'); + } + + return await response.json(); +} + +export async function createDashboard(data) { + const response = await fetch(`${API_BASE_URL}/dashboards`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(data), + }); + + if (!response.ok) { + throw new Error('Could not create a dashboard'); + } + + return await response.json(); +} + +export async function updateDashboard(id, data) { + const endpoint = `${API_BASE_URL}/dashboards/${id}`; + + const response = await fetch(endpoint, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(data), + }); + + if (!response.ok) { + throw new Error('Could not update a dashboard'); + } + + return await response.json(); +} + +export async function deleteDashboard(id) { + const endpoint = `${API_BASE_URL}/dashboards/${id}`; + + const response = await fetch(endpoint, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Could not delete a dashboard'); + } +} + +export async function runDashboard(id) { + const endpoint = `${API_BASE_URL}/dashboards/${id}/run`; + + const response = await fetch(endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Could not run dashboard'); } return await response.json(); diff --git a/ui/src/services/gears.js b/ui/src/services/gears.js index 8e33a67..9ee7c89 100644 --- a/ui/src/services/gears.js +++ b/ui/src/services/gears.js @@ -14,3 +14,20 @@ export async function fetchGears(page) { return await response.json(); } + +export async function getGear(id) { + const endpoint = `${API_BASE_URL}/gears/${id}`; + + const response = await fetch(endpoint, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (!response.ok) { + throw new Error('Could not get gear'); + } + + return await response.json(); +} diff --git a/ui/src/services/user.js b/ui/src/services/user.js index 8e965a7..eed8c82 100644 --- a/ui/src/services/user.js +++ b/ui/src/services/user.js @@ -32,7 +32,9 @@ export async function getUserMe(accessToken) { } export async function getTaskDetail(id) { - const response = await fetch(`${API_BASE_URL}/user/task/${id}`, { + const endpoint = `${API_BASE_URL}/user/task/${id}`; + + const response = await fetch(endpoint, { method: 'GET', headers: { 'Content-Type': 'application/json', @@ -77,3 +79,19 @@ export async function saveUserConfig(config) { return await response.json(); } + +export async function runQuery(query) { + const response = await fetch(`${API_BASE_URL}/user/query`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(query), + }); + + if (!response.ok) { + throw new Error('Could not run query'); + } + + return await response.json(); +}