Rewrite API

This commit is contained in:
Brieuc Dubois 2024-01-06 20:24:09 +01:00
parent 1156bfca40
commit 3fbf0cc66d
7 changed files with 224 additions and 124 deletions

View File

@ -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<Card> {
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<boolean> {
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<void> {
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<boolean> {
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<void> {
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<void> {
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();
}
}

View File

@ -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<number | null> {
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<boolean> {
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<boolean> {
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_
};

View File

@ -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<boolean> {
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<boolean> {
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<boolean> {
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_
};

View File

@ -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<number | null> {
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<boolean> {
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<boolean> {
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<TagOption[]> {
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_
};

View File

@ -1,21 +1,22 @@
import type Card from '$lib/types/Card'; import type Card from '$lib/types/Card';
import type Project from '$lib/types/Project'; import type Project from '$lib/types/Project';
import ProjectTag from '$lib/types/ProjectTag';
import api, { processError } from '$lib/utils/api'; import api, { processError } from '$lib/utils/api';
import { parseCards } from '$lib/utils/parser'; import { parseCards } from '$lib/utils/parser';
import status from '$lib/utils/status'; import status from '$lib/utils/status';
export async function getProjectAPI(projectId: number): Promise<Project> { async function get(projectId: number): Promise<number | null> {
const response = await api.get(`/v1/projects/${projectId}`); const response = await api.get(`/v1/projects/${projectId}`);
if (response.status !== status.OK) { if (response.status !== status.OK) {
processError(response, 'Failed to fetch project'); processError(response, 'Failed to fetch project');
return Promise.reject(); return null;
} }
return response.data; return response.data;
} }
export async function getProjectCardsAPI(projectId: number): Promise<Card[]> { async function getCards(projectId: number): Promise<Card[]> {
const response = await api.get(`/v1/projects/${projectId}/cards`); const response = await api.get(`/v1/projects/${projectId}/cards`);
if (response.status !== status.OK) { if (response.status !== status.OK) {
@ -25,3 +26,16 @@ export async function getProjectCardsAPI(projectId: number): Promise<Card[]> {
return parseCards(response.data); return parseCards(response.data);
} }
async function getTags(project: Project): Promise<ProjectTag[]> {
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;
}

View File

@ -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<boolean> {
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;
}

View File

@ -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<number | null> {
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<boolean> {
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<boolean> {
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_
};