Update API & types

This commit is contained in:
Brieuc Dubois 2024-01-06 22:36:30 +01:00
parent 3fbf0cc66d
commit 47de3a4bbc
4 changed files with 106 additions and 20 deletions

View File

@ -0,0 +1,52 @@
import type Project from '$lib/types/Project';
import type View from '$lib/types/View';
import api, { processError } from '$lib/utils/api';
import status from '$lib/utils/status';
async function create(project: Project): Promise<number | null> {
const response = await api.post('/views', {
project: project.id
});
if (response.status !== status.Created) {
processError(response, 'Failed to create view');
return null;
}
return response.data.id;
}
async function update(view: View): Promise<boolean> {
const response = await api.put(`/views/${view.id}`, {
project: view.project.id,
primary_tag_id: view.primaryTag?.id,
secondary_tag_id: view.secondaryTag?.id,
title: view.title,
sort_tag_id: view.sortTag?.id,
sort_direction: view.sortDirection
});
if (response.status !== status.OK) {
processError(response, 'Failed to update view');
return false;
}
return true;
}
async function delete_(viewId: number): Promise<boolean> {
const response = await api.delete(`/views/${viewId}`);
if (response.status !== status.OK) {
processError(response, 'Failed to delete view');
return false;
}
return true;
}
export default {
create,
update,
delete: delete_
};

View File

@ -45,6 +45,10 @@ export default class CardTag {
return new CardTag(card, tag, option, value); return new CardTag(card, tag, option, value);
} }
async delete() {
return cardsTagsApi.delete(this._card.id, this._tag.id);
}
static parse(json: any): CardTag | null; static parse(json: any): CardTag | null;
static parse(json: any, card: Card | null | undefined): CardTag | null; static parse(json: any, card: Card | null | undefined): CardTag | null;

View File

@ -2,25 +2,26 @@
import { get, writable } from 'svelte/store'; import { get, writable } from 'svelte/store';
import TagOption from './TagOption'; import TagOption from './TagOption';
import Project from './Project';
const projectTags = writable([] as ProjectTag[]); const projectTags = writable([] as ProjectTag[]);
export default class ProjectTag { export default class ProjectTag {
private _id: number; private _id: number;
private _projectId: number; private _project: Project;
private _title: string; private _title: string;
private _type: number; private _type: number;
private _options: TagOption[]; private _options: TagOption[];
private constructor( private constructor(
id: number, id: number,
projectId: number, project: Project,
title: string, title: string,
type: number, type: number,
options: TagOption[] options: TagOption[]
) { ) {
this._id = id; this._id = id;
this._projectId = projectId; this._project = project;
this._title = title; this._title = title;
this._type = type; this._type = type;
this._options = options; this._options = options;
@ -30,8 +31,8 @@ export default class ProjectTag {
return this._id; return this._id;
} }
get projectId(): number { get project(): Project {
return this._projectId; return this._project;
} }
get title(): string { get title(): string {
@ -60,25 +61,36 @@ export default class ProjectTag {
return null; return null;
} }
static parse(json: any): ProjectTag | null { static parse(json: any): ProjectTag | null;
static parse(json: any, project: Project | null | undefined): ProjectTag | null;
static parse(json: any, project?: Project | null): ProjectTag | null {
if (!json) return null; if (!json) return null;
const projectTag = new ProjectTag(json.id, json.project_id, json.title, json.type, []); if (!project) project = Project.fromId(json.project);
if (!project) return null;
const projectTag = new ProjectTag(json.id, json.project, json.title, json.type, []);
const options = TagOption.parseAll(json.options, projectTag); const options = TagOption.parseAll(json.options, projectTag);
projectTag._options = options; projectTag._options = options;
projectTags.update((projectTags) => [...projectTags, projectTag]);
return projectTag; return projectTag;
} }
static parseAll(json: any): ProjectTag[] { static parseAll(json: any): ProjectTag[];
static parseAll(json: any, project: Project | null): ProjectTag[];
static parseAll(json: any, project?: Project | null): ProjectTag[] {
if (!json) return []; if (!json) return [];
const projectTags: ProjectTag[] = []; const projectTags: ProjectTag[] = [];
for (const projectTag of json) { for (const projectTag of json) {
const parsed = ProjectTag.parse(projectTag); const parsed = ProjectTag.parse(projectTag, project);
if (parsed) projectTags.push(parsed); if (parsed) projectTags.push(parsed);
} }

View File

@ -1,15 +1,9 @@
// export default interface View { import { writable } from 'svelte/store';
// id: number;
// projectId: number;
// primaryTagId: number | null;
// secondaryTagId: number | null;
// title: string;
// sortTagId: number | null;
// sortDirection: number | null;
// }
import Project from './Project'; import Project from './Project';
import ProjectTag from './ProjectTag'; import ProjectTag from './ProjectTag';
import viewsApi from '$lib/api/viewsApi';
const views = writable([] as View[]);
export default class View { export default class View {
private _id: number; private _id: number;
@ -66,6 +60,26 @@ export default class View {
return this._sortDirection; return this._sortDirection;
} }
static async create(project: Project) {
const id = await viewsApi.create(project);
if (!id) return null;
const view = new View(id, project, null, null, 'New view', null, null);
views.update((views) => [...views, view]);
return view;
}
async delete(): Promise<boolean> {
if (!(await viewsApi.delete(this.id))) return false;
views.update((views) => views.filter((view) => view.id !== this.id));
return true;
}
static parse(json: any): View | null; static parse(json: any): View | null;
static parse(json: any, project: Project | null | undefined): View | null; static parse(json: any, project: Project | null | undefined): View | null;
@ -84,7 +98,7 @@ export default class View {
const sortTag = ProjectTag.fromId(json.sort_tag_id); const sortTag = ProjectTag.fromId(json.sort_tag_id);
if (!sortTag) return null; if (!sortTag) return null;
return new View( const view = new View(
json.id, json.id,
project, project,
primaryTag, primaryTag,
@ -93,5 +107,9 @@ export default class View {
sortTag, sortTag,
json.sort_direction json.sort_direction
); );
views.update((views) => [...views, view]);
return view;
} }
} }