diff --git a/src/stores/admin/user/api.ts b/src/stores/admin/user/api.ts new file mode 100644 index 0000000..2836468 --- /dev/null +++ b/src/stores/admin/user/api.ts @@ -0,0 +1,57 @@ +import { defineStore } from "pinia"; +import type { ApiViewModel } from "@/viewmodels/admin/user/api.models"; +import { http } from "@/serverCom"; +import type { PermissionObject } from "@/types/permissionTypes"; +import type { AxiosResponse } from "axios"; + +export const useApiStore = defineStore("api", { + state: () => { + return { + apis: [] as Array, + loading: null as null | "loading" | "success" | "failed", + }; + }, + actions: { + fetchApis() { + this.loading = "loading"; + http + .get("/admin/api") + .then((result) => { + this.apis = result.data; + this.loading = "success"; + }) + .catch((err) => { + this.loading = "failed"; + }); + }, + fetchApiById(id: number): Promise> { + return http.get(`/admin/api/${id}`); + }, + async createApi(api: string): Promise> { + const result = await http.post("/admin/api", { + api: api, + }); + this.fetchApis(); + return result; + }, + async updateActiveApi(id: number, api: string): Promise> { + const result = await http.patch(`/admin/api/${id}`, { + api: api, + }); + this.fetchApis(); + return result; + }, + async updateActiveApiPermissions(api: number, permission: PermissionObject): Promise> { + const result = await http.patch(`/admin/api/${api}/permissions`, { + permissions: permission, + }); + this.fetchApis(); + return result; + }, + async deleteApi(api: number): Promise> { + const result = await http.delete(`/admin/api/${api}`); + this.fetchApis(); + return result; + }, + }, +}); diff --git a/src/viewmodels/admin/user/api.models.ts b/src/viewmodels/admin/user/api.models.ts new file mode 100644 index 0000000..cc3b3e6 --- /dev/null +++ b/src/viewmodels/admin/user/api.models.ts @@ -0,0 +1,26 @@ +import type { PermissionObject } from "@/types/permissionTypes"; + +export interface ApiViewModel { + id: number; + permissions: PermissionObject; + title: string; + createdAt: Date; + lastUsage?: Date; + expiry?: Date; +} + +export interface CreateApiViewModel { + title: string; + token: string; + expiry?: Date; +} + +export interface UpdateApiViewModel { + id: number; + title: string; + expiry?: Date; +} + +export interface DeleteApiViewModel { + id: number; +}