From 667a5f92094ce28f8b02ea2c26fc072b014128e7 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Wed, 22 Jan 2025 09:10:52 +0100 Subject: [PATCH] api store and viewmodel --- src/stores/admin/user/api.ts | 57 +++++++++++++++++++++++++ src/viewmodels/admin/user/api.models.ts | 26 +++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/stores/admin/user/api.ts create mode 100644 src/viewmodels/admin/user/api.models.ts diff --git a/src/stores/admin/user/api.ts b/src/stores/admin/user/api.ts new file mode 100644 index 0000000..a5401ed --- /dev/null +++ b/src/stores/admin/user/api.ts @@ -0,0 +1,57 @@ +import { defineStore } from "pinia"; +import type { RoleViewModel } from "@/viewmodels/admin/user/role.models"; +import { http } from "@/serverCom"; +import type { PermissionObject } from "@/types/permissionTypes"; +import type { AxiosResponse } from "axios"; + +export const useRoleStore = defineStore("role", { + state: () => { + return { + roles: [] as Array, + loading: null as null | "loading" | "success" | "failed", + }; + }, + actions: { + fetchRoles() { + this.loading = "loading"; + http + .get("/admin/role") + .then((result) => { + this.roles = result.data; + this.loading = "success"; + }) + .catch((err) => { + this.loading = "failed"; + }); + }, + fetchRoleById(id: number): Promise> { + return http.get(`/admin/role/${id}`); + }, + async createRole(role: string): Promise> { + const result = await http.post("/admin/role", { + role: role, + }); + this.fetchRoles(); + return result; + }, + async updateActiveRole(id: number, role: string): Promise> { + const result = await http.patch(`/admin/role/${id}`, { + role: role, + }); + this.fetchRoles(); + return result; + }, + async updateActiveRolePermissions(role: number, permission: PermissionObject): Promise> { + const result = await http.patch(`/admin/role/${role}/permissions`, { + permissions: permission, + }); + this.fetchRoles(); + return result; + }, + async deleteRole(role: number): Promise> { + const result = await http.delete(`/admin/role/${role}`); + this.fetchRoles(); + 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; +}