Initial commit
This commit is contained in:
202
src/stores/collectionsStore.ts
Normal file
202
src/stores/collectionsStore.ts
Normal file
@@ -0,0 +1,202 @@
|
||||
/**
|
||||
* Chrono Manager - Collections Store
|
||||
*/
|
||||
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import { collectionService } from '../services/collectionService';
|
||||
import type {
|
||||
SourceSelector,
|
||||
ListFilter,
|
||||
ListSort,
|
||||
} from '../types/common';
|
||||
import { CollectionObject } from '../models/collection';
|
||||
import type { ServiceObject } from '../models/service';
|
||||
import type { CollectionInterface } from '../types/collection';
|
||||
|
||||
export const useCollectionsStore = defineStore('chronoCollectionsStore', () => {
|
||||
// State
|
||||
const collections = ref<CollectionObject[]>([]);
|
||||
|
||||
// Actions
|
||||
|
||||
/**
|
||||
* Retrieve collections from the server
|
||||
*/
|
||||
async function list(
|
||||
sources?: SourceSelector,
|
||||
filter?: ListFilter,
|
||||
sort?: ListSort,
|
||||
uid?: string
|
||||
): Promise<CollectionObject[]> {
|
||||
try {
|
||||
const response = await collectionService.list({ sources, filter, sort, uid });
|
||||
|
||||
// Flatten the nested response into a flat array
|
||||
const flatCollections: CollectionObject[] = [];
|
||||
Object.entries(response).forEach(([_providerId, providerCollections]) => {
|
||||
Object.entries(providerCollections).forEach(([_serviceId, serviceCollections]) => {
|
||||
Object.values(serviceCollections).forEach((collection: CollectionInterface) => {
|
||||
flatCollections.push(new CollectionObject().fromJson(collection));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully retrieved', flatCollections.length, 'collections:', flatCollections.map(c => ({
|
||||
id: c.id,
|
||||
label: c.label,
|
||||
service: c.service,
|
||||
provider: c.provider
|
||||
})));
|
||||
|
||||
collections.value = flatCollections;
|
||||
return flatCollections;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to retrieve collections:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a specific collection
|
||||
*/
|
||||
async function fetch(
|
||||
provider: string,
|
||||
service: string,
|
||||
identifier: string | number,
|
||||
uid?: string
|
||||
): Promise<CollectionObject | null> {
|
||||
try {
|
||||
const response = await collectionService.fetch({ provider, service, identifier, uid });
|
||||
|
||||
return new CollectionObject().fromJson(response);
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to fetch collection:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fresh collection object with default values
|
||||
*/
|
||||
function fresh(): CollectionObject {
|
||||
return new CollectionObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new collection
|
||||
*/
|
||||
async function create(
|
||||
service: ServiceObject,
|
||||
collection: CollectionObject,
|
||||
options?: string[],
|
||||
uid?: string
|
||||
): Promise<CollectionObject | null> {
|
||||
try {
|
||||
if (service.provider === null || service.id === null) {
|
||||
throw new Error('Invalid service object, must have a provider and identifier');
|
||||
}
|
||||
|
||||
const response = await collectionService.create({
|
||||
provider: service.provider,
|
||||
service: service.id,
|
||||
data: collection.toJson(),
|
||||
options,
|
||||
uid
|
||||
});
|
||||
|
||||
const createdCollection = new CollectionObject().fromJson(response);
|
||||
collections.value.push(createdCollection);
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully created collection');
|
||||
|
||||
return createdCollection;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to create collection:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify an existing collection
|
||||
*/
|
||||
async function modify(
|
||||
collection: CollectionObject,
|
||||
uid?: string
|
||||
): Promise<CollectionObject | null> {
|
||||
try {
|
||||
if (!collection.provider || !collection.service || !collection.id) {
|
||||
throw new Error('Collection must have provider, service, and id');
|
||||
}
|
||||
|
||||
const response = await collectionService.modify({
|
||||
provider: collection.provider,
|
||||
service: collection.service,
|
||||
identifier: collection.id,
|
||||
data: collection.toJson(),
|
||||
uid
|
||||
});
|
||||
|
||||
const modifiedCollection = new CollectionObject().fromJson(response);
|
||||
const index = collections.value.findIndex(c => c.id === collection.id);
|
||||
if (index !== -1) {
|
||||
collections.value[index] = modifiedCollection;
|
||||
}
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully modified collection');
|
||||
|
||||
return modifiedCollection;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to modify collection:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a collection
|
||||
*/
|
||||
async function destroy(
|
||||
collection: CollectionObject,
|
||||
uid?: string
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
if (!collection.provider || !collection.service || !collection.id) {
|
||||
throw new Error('Collection must have provider, service, and id');
|
||||
}
|
||||
|
||||
const response = await collectionService.destroy({
|
||||
provider: collection.provider,
|
||||
service: collection.service,
|
||||
identifier: collection.id,
|
||||
uid
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
const index = collections.value.findIndex(c => c.id === collection.id);
|
||||
if (index !== -1) {
|
||||
collections.value.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully destroyed collection');
|
||||
|
||||
return response.success;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to destroy collection:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
collections,
|
||||
|
||||
// Actions
|
||||
list,
|
||||
fetch,
|
||||
fresh,
|
||||
create,
|
||||
modify,
|
||||
destroy,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user