api store and viewmodels

This commit is contained in:
Julian Krauser 2025-01-22 09:11:39 +01:00
parent 131b3747de
commit ee42625d66
2 changed files with 83 additions and 0 deletions

View file

@ -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<ApiViewModel>,
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<AxiosResponse<any, any>> {
return http.get(`/admin/api/${id}`);
},
async createApi(api: string): Promise<AxiosResponse<any, any>> {
const result = await http.post("/admin/api", {
api: api,
});
this.fetchApis();
return result;
},
async updateActiveApi(id: number, api: string): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/api/${id}`, {
api: api,
});
this.fetchApis();
return result;
},
async updateActiveApiPermissions(api: number, permission: PermissionObject): Promise<AxiosResponse<any, any>> {
const result = await http.patch(`/admin/api/${api}/permissions`, {
permissions: permission,
});
this.fetchApis();
return result;
},
async deleteApi(api: number): Promise<AxiosResponse<any, any>> {
const result = await http.delete(`/admin/api/${api}`);
this.fetchApis();
return result;
},
},
});

View file

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