From 3fbf0cc66d6857b4124ba67244b00446c881482a Mon Sep 17 00:00:00 2001 From: Bhasher Date: Sat, 6 Jan 2024 20:24:09 +0100 Subject: [PATCH] Rewrite API --- frontend/src/lib/api/cards.ts | 104 ------------------ frontend/src/lib/api/cardsApi.ts | 49 +++++++++ frontend/src/lib/api/cardsTagsApi.ts | 57 ++++++++++ frontend/src/lib/api/projectTagsApi.ts | 57 ++++++++++ .../lib/api/{projects.ts => projectsApi.ts} | 20 +++- frontend/src/lib/api/tags.ts | 17 --- frontend/src/lib/api/tagsOptions.ts | 44 ++++++++ 7 files changed, 224 insertions(+), 124 deletions(-) delete mode 100644 frontend/src/lib/api/cards.ts create mode 100644 frontend/src/lib/api/cardsApi.ts create mode 100644 frontend/src/lib/api/cardsTagsApi.ts create mode 100644 frontend/src/lib/api/projectTagsApi.ts rename frontend/src/lib/api/{projects.ts => projectsApi.ts} (54%) delete mode 100644 frontend/src/lib/api/tags.ts create mode 100644 frontend/src/lib/api/tagsOptions.ts diff --git a/frontend/src/lib/api/cards.ts b/frontend/src/lib/api/cards.ts deleted file mode 100644 index c973833..0000000 --- a/frontend/src/lib/api/cards.ts +++ /dev/null @@ -1,104 +0,0 @@ -import type Card from '$lib/types/Card'; -import type TagValue from '$lib/types/TagValue'; -import api, { processError } from '../utils/api'; -import status from '../utils/status'; - -export async function newCardApi(projectId: number, tags: TagValue[]): Promise { - const response = await api.post(`/v1/cards`, { - project_id: projectId, - title: 'Untitled', - content: '' - }); - - if (response.status !== status.Created) { - processError(response, 'Failed to create card'); - return Promise.reject(); - } - - const id: number = response.data.id; - - const consistant_tags = []; - - for (const tag of tags) { - if ((tag.option_id === -1 && tag.value == '') || tag.tag_id === -1) continue; - await createCardTagApi(id, tag.tag_id, tag.option_id, tag.value); - consistant_tags.push({ ...tag, card_id: id }); - } - - return { - id: id, - project_id: projectId, - title: 'Untitled', - content: '', - tags: consistant_tags - }; -} - -export async function updateCardApi(card: Card): Promise { - const response = await api.put(`/v1/cards/${card.id}`, { - project_id: card.project_id, - title: card.title, - content: card.content - }); - - if (response.status !== status.NoContent) { - processError(response, 'Failed to update card'); - return false; - } - - return true; -} - -export async function deleteCardApi(cardID: number): Promise { - const response = await api.delete(`/v1/cards/${cardID}`); - - if (response.status !== status.NoContent) { - processError(response, 'Failed to delete card'); - return Promise.reject(); - } -} - -export async function createCardTagApi( - cardId: number, - tagId: number, - optionId: number | null, - value: string | null -): Promise { - const response = await api.post(`/v1/cards/${cardId}/tags/${tagId}`, { - option_id: optionId, - value: value - }); - - if (response.status !== status.Created) { - processError(response, 'Failed to create card tag'); - return false; - } - - return true; -} - -export async function deleteCardTagApi(cardID: number, tagID: number): Promise { - const response = await api.delete(`/v1/cards/${cardID}/tags/${tagID}`); - - if (response.status !== status.NoContent) { - processError(response, 'Failed to delete tag'); - return Promise.reject(); - } -} - -export async function updateCardTagApi( - cardID: number, - tagID: number, - option_id: number | null, - value: string | null -): Promise { - const response = await api.put(`/v1/cards/${cardID}/tags/${tagID}`, { - option_id: option_id == -1 ? null : option_id, - value: value == '' ? null : value - }); - - if (response.status !== status.NoContent) { - processError(response, 'Failed to update card tag'); - return Promise.reject(); - } -} diff --git a/frontend/src/lib/api/cardsApi.ts b/frontend/src/lib/api/cardsApi.ts new file mode 100644 index 0000000..c9625e3 --- /dev/null +++ b/frontend/src/lib/api/cardsApi.ts @@ -0,0 +1,49 @@ +import type Card from '$lib/types/Card'; +import api, { processError } from '../utils/api'; +import status from '../utils/status'; + +async function create(projectId: number): Promise { + const response = await api.post(`/v1/cards`, { + project_id: projectId, + title: 'Untitled', + content: '' + }); + + if (response.status !== status.Created) { + processError(response, 'Failed to create card'); + return null; + } + + return response.data.id; +} + +async function update(card: Card): Promise { + const response = await api.put(`/v1/cards/${card.id}`, { + project_id: card.project_id, + title: card.title, + content: card.content + }); + + if (response.status !== status.NoContent) { + processError(response, 'Failed to update card'); + return false; + } + + return true; +} + +async function delete_(cardID: number): Promise { + const response = await api.delete(`/v1/cards/${cardID}`); + + if (response.status !== status.NoContent) { + processError(response, 'Failed to delete card'); + return false; + } + return true; +} + +export default { + create, + update, + delete: delete_ +}; diff --git a/frontend/src/lib/api/cardsTagsApi.ts b/frontend/src/lib/api/cardsTagsApi.ts new file mode 100644 index 0000000..02efe20 --- /dev/null +++ b/frontend/src/lib/api/cardsTagsApi.ts @@ -0,0 +1,57 @@ +import api, { processError } from '../utils/api'; +import status from '../utils/status'; + +async function create( + cardId: number, + tagId: number, + optionId: number | null, + value: string | null +): Promise { + const response = await api.post(`/v1/cards/${cardId}/tags/${tagId}`, { + option_id: optionId, + value: value + }); + + if (response.status !== status.Created) { + processError(response, 'Failed to create card tag'); + return false; + } + + return true; +} + +async function update( + cardId: number, + tagId: number, + optionId: number | null, + value: string | null +): Promise { + const response = await api.put(`/v1/cards/${cardId}/tags/${tagId}`, { + option_id: optionId, + value: value + }); + + if (response.status !== status.NoContent) { + processError(response, 'Failed to update card tag'); + return false; + } + + return true; +} + +async function delete_(cardId: number, tagId: number): Promise { + const response = await api.delete(`/v1/cards/${cardId}/tags/${tagId}`); + + if (response.status !== status.NoContent) { + processError(response, 'Failed to delete tag'); + return false; + } + + return true; +} + +export default { + create, + update, + delete: delete_ +}; diff --git a/frontend/src/lib/api/projectTagsApi.ts b/frontend/src/lib/api/projectTagsApi.ts new file mode 100644 index 0000000..8b1d3ff --- /dev/null +++ b/frontend/src/lib/api/projectTagsApi.ts @@ -0,0 +1,57 @@ +import type Project from '$lib/types/Project'; +import type ProjectTag from '$lib/types/ProjectTag'; +import TagOption from '$lib/types/TagOption'; +import api, { processError } from '$lib/utils/api'; +import status from '$lib/utils/status'; + +async function create(project: Project, title: string, type: number): Promise { + const response = await api.post(`/v1/tags/`, { + project_id: project.id, + title, + type + }); + + if (response.status !== status.Created) { + processError(response, 'Failed to create tag'); + return null; + } + + return response.data.id; +} + +async function update(tag: ProjectTag): Promise { + const response = await api.put(`/v1/tags/${tag.id}`, tag); + + if (response.status !== status.NoContent) { + processError(response, 'Failed to update tag'); + return false; + } + return true; +} + +async function delete_(tagId: number): Promise { + const response = await api.delete(`/v1/tags/${tagId}`); + + if (response.status !== status.NoContent) { + processError(response, 'Failed to delete tag'); + return false; + } + return true; +} + +async function getOptions(projectTag: ProjectTag): Promise { + const response = await api.get(`/v1/tags/${projectTag.id}/options`); + + if (response.status !== status.OK) { + processError(response, 'Failed to get tag options'); + return []; + } + + return TagOption.parseAll(response.data, projectTag); +} + +export default { + create, + update, + delete: delete_ +}; diff --git a/frontend/src/lib/api/projects.ts b/frontend/src/lib/api/projectsApi.ts similarity index 54% rename from frontend/src/lib/api/projects.ts rename to frontend/src/lib/api/projectsApi.ts index 23a1cbf..a3eef55 100644 --- a/frontend/src/lib/api/projects.ts +++ b/frontend/src/lib/api/projectsApi.ts @@ -1,21 +1,22 @@ import type Card from '$lib/types/Card'; import type Project from '$lib/types/Project'; +import ProjectTag from '$lib/types/ProjectTag'; import api, { processError } from '$lib/utils/api'; import { parseCards } from '$lib/utils/parser'; import status from '$lib/utils/status'; -export async function getProjectAPI(projectId: number): Promise { +async function get(projectId: number): Promise { const response = await api.get(`/v1/projects/${projectId}`); if (response.status !== status.OK) { processError(response, 'Failed to fetch project'); - return Promise.reject(); + return null; } return response.data; } -export async function getProjectCardsAPI(projectId: number): Promise { +async function getCards(projectId: number): Promise { const response = await api.get(`/v1/projects/${projectId}/cards`); if (response.status !== status.OK) { @@ -25,3 +26,16 @@ export async function getProjectCardsAPI(projectId: number): Promise { return parseCards(response.data); } + +async function getTags(project: Project): Promise { + const response = await api.get(`/v1/projects/${project.id}/tags`); + + if (response.status !== status.OK) { + processError(response, 'Failed to fetch tags'); + return []; + } + + const projectTags: ProjectTag[] = ProjectTag.parseAll(response.data, project); + + return projectTags; +} diff --git a/frontend/src/lib/api/tags.ts b/frontend/src/lib/api/tags.ts deleted file mode 100644 index 0b582e8..0000000 --- a/frontend/src/lib/api/tags.ts +++ /dev/null @@ -1,17 +0,0 @@ -import type TagOption from '$lib/types/TagOption'; -import api, { processError } from '$lib/utils/api'; -import status from '$lib/utils/status'; - -export async function updateTagAPI(option: TagOption): Promise { - const response = - option.value === '' - ? await api.delete(`/v1/tags/${option.tag_id}/options/${option.id}`) - : await api.put(`/v1/tags/${option.tag_id}/options/${option.id}`, option); - - if (response.status !== status.NoContent) { - processError(response, 'Failed to update tag option'); - return false; - } - - return true; -} diff --git a/frontend/src/lib/api/tagsOptions.ts b/frontend/src/lib/api/tagsOptions.ts new file mode 100644 index 0000000..88673c4 --- /dev/null +++ b/frontend/src/lib/api/tagsOptions.ts @@ -0,0 +1,44 @@ +import type ProjectTag from '$lib/types/ProjectTag'; +import api, { processError } from '$lib/utils/api'; +import status from '$lib/utils/status'; + +async function create(projectTagId: number, value: string): Promise { + const response = await api.post(`/v1/tags/${projectTagId}/options`, { + value + }); + + if (response.status !== status.Created) { + processError(response, 'Failed to create tag option'); + return null; + } + + return response.data.id; +} + +async function update(tagOptionId: number, projectTagId: number, value: string): Promise { + const response = await api.put(`/v1/tags/${projectTagId}/options/${tagOptionId}`, { + value + }); + + if (response.status !== status.NoContent) { + processError(response, 'Failed to update tag option'); + return false; + } + return true; +} + +async function delete_(tagOptionId: number, projectTagId: number): Promise { + const response = await api.delete(`/v1/tags/${projectTagId}/options/${tagOptionId}`); + + if (response.status !== status.NoContent) { + processError(response, 'Failed to delete tag option'); + return false; + } + return true; +} + +export default { + create, + update, + delete: delete_ +};