2025-02-27 18:16:18 +01:00
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
|
2025-02-28 21:35:17 +01:00
|
|
|
// Define types for our data
|
|
|
|
interface StorageSpace {
|
|
|
|
id: number;
|
|
|
|
parent: {
|
|
|
|
valid: boolean;
|
|
|
|
int64: number;
|
|
|
|
} | null;
|
|
|
|
location: string | null;
|
2025-02-28 22:03:18 +01:00
|
|
|
name: string | null;
|
2025-02-28 21:35:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
interface StorageObject {
|
|
|
|
id: number;
|
|
|
|
storagespaceId: number;
|
|
|
|
name: string;
|
|
|
|
description: string | null;
|
|
|
|
serialnumber: string | null;
|
|
|
|
}
|
|
|
|
|
2025-02-27 18:16:18 +01:00
|
|
|
export const useStorageStore = defineStore('storage', {
|
|
|
|
state: () => ({
|
2025-02-28 21:35:17 +01:00
|
|
|
storageSpaces: [] as StorageSpace[],
|
|
|
|
objects: [] as StorageObject[],
|
2025-02-27 18:16:18 +01:00
|
|
|
loading: false,
|
|
|
|
error: null as string | null,
|
|
|
|
}),
|
|
|
|
|
|
|
|
getters: {
|
2025-02-28 21:35:17 +01:00
|
|
|
getStorageById: (state) => (id: number): StorageSpace | undefined => {
|
2025-02-27 18:16:18 +01:00
|
|
|
return state.storageSpaces.find(storage => storage.id === id);
|
|
|
|
},
|
|
|
|
|
2025-02-28 21:35:17 +01:00
|
|
|
getObjectsInStorage: (state) => (storageId: number): StorageObject[] => {
|
2025-02-27 18:16:18 +01:00
|
|
|
return state.objects.filter(obj => obj.storagespaceId === storageId);
|
|
|
|
},
|
|
|
|
|
2025-02-28 21:35:17 +01:00
|
|
|
getChildStorages: (state) => (parentId: number): StorageSpace[] => {
|
2025-02-27 18:16:18 +01:00
|
|
|
return state.storageSpaces.filter(storage =>
|
2025-02-28 21:35:17 +01:00
|
|
|
storage.parent && storage.parent.valid && storage.parent.int64 === parentId
|
2025-02-27 18:16:18 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2025-02-28 22:03:18 +01:00
|
|
|
async createStorageSpace(data: { name: string; location: string; parentId: number | null }): Promise<void> {
|
2025-02-28 21:42:57 +01:00
|
|
|
this.loading = true;
|
|
|
|
this.error = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const parentData = data.parentId ? { valid: true, int64: data.parentId } : null;
|
|
|
|
|
|
|
|
const response = await fetch('/api/v1/storageSpaces', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
2025-02-28 22:03:18 +01:00
|
|
|
name: data.name,
|
2025-02-28 21:42:57 +01:00
|
|
|
location: data.location,
|
|
|
|
parent: parentData
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Failed to create storage space');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh the storage spaces list
|
|
|
|
await this.fetchStorageSpaces();
|
|
|
|
} catch (error: any) {
|
|
|
|
this.error = error.message;
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2025-02-28 21:35:17 +01:00
|
|
|
async fetchStorageSpaces(): Promise<void> {
|
2025-02-27 18:16:18 +01:00
|
|
|
this.loading = true;
|
|
|
|
this.error = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch('/api/v1/storageSpaces', {
|
|
|
|
headers: {
|
|
|
|
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Failed to fetch storage spaces');
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await response.json();
|
2025-02-28 21:18:07 +01:00
|
|
|
this.storageSpaces = data.map((space: any) => ({
|
|
|
|
id: space.id,
|
|
|
|
parent: space.parent,
|
2025-02-28 22:03:18 +01:00
|
|
|
location: space.location,
|
|
|
|
name: space.name
|
2025-02-28 21:18:07 +01:00
|
|
|
}));
|
2025-02-27 18:16:18 +01:00
|
|
|
} catch (error: any) {
|
|
|
|
this.error = error.message;
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2025-02-28 21:35:17 +01:00
|
|
|
async fetchObjectsInStorage(storageId: number): Promise<void> {
|
2025-02-27 18:16:18 +01:00
|
|
|
this.loading = true;
|
|
|
|
this.error = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(`/api/v1/storageSpaces/${storageId}/objects`, {
|
|
|
|
headers: {
|
|
|
|
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Failed to fetch objects');
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await response.json();
|
2025-02-28 21:35:17 +01:00
|
|
|
this.objects = data as StorageObject[];
|
2025-02-27 18:16:18 +01:00
|
|
|
} catch (error: any) {
|
|
|
|
this.error = error.message;
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2025-02-28 21:35:17 +01:00
|
|
|
async fetchStorageHierarchy(rootStorageId: number): Promise<void> {
|
2025-02-27 18:16:18 +01:00
|
|
|
this.loading = true;
|
|
|
|
this.error = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// First, ensure we have all storage spaces
|
|
|
|
if (this.storageSpaces.length === 0) {
|
|
|
|
await this.fetchStorageSpaces();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then fetch all objects in the hierarchy
|
|
|
|
const response = await fetch(`/api/v1/storageSpaces/${rootStorageId}/hierarchy/objects`, {
|
|
|
|
headers: {
|
|
|
|
'Authorization': `Bearer ${localStorage.getItem('auth_token')}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Failed to fetch storage hierarchy');
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await response.json();
|
2025-02-28 21:35:17 +01:00
|
|
|
this.objects = data as StorageObject[];
|
2025-02-27 18:16:18 +01:00
|
|
|
} catch (error: any) {
|
|
|
|
this.error = error.message;
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|