81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
|
|
import { IResource, ResourceType } from '../types/ResourceTypes';
|
||
|
|
import { EntityType } from '../types/CalendarTypes';
|
||
|
|
import { IApiRepository } from './IApiRepository';
|
||
|
|
|
||
|
|
interface RawResourceData {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
displayName: string;
|
||
|
|
type: string;
|
||
|
|
avatarUrl?: string;
|
||
|
|
color?: string;
|
||
|
|
isActive?: boolean;
|
||
|
|
metadata?: Record<string, any>;
|
||
|
|
[key: string]: unknown;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* MockResourceRepository - Loads resource data from local JSON file
|
||
|
|
*
|
||
|
|
* This repository implementation fetches mock resource data from a static JSON file.
|
||
|
|
* Used for development and testing instead of API calls.
|
||
|
|
*
|
||
|
|
* Data Source: data/mock-resources.json
|
||
|
|
*
|
||
|
|
* NOTE: Create/Update/Delete operations are not supported - throws errors.
|
||
|
|
* Only fetchAll() is implemented for loading initial mock data.
|
||
|
|
*/
|
||
|
|
export class MockResourceRepository implements IApiRepository<IResource> {
|
||
|
|
public readonly entityType: EntityType = 'Resource';
|
||
|
|
private readonly dataUrl = 'data/mock-resources.json';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Fetch all resources from mock JSON file
|
||
|
|
*/
|
||
|
|
public async fetchAll(): Promise<IResource[]> {
|
||
|
|
try {
|
||
|
|
const response = await fetch(this.dataUrl);
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error(`Failed to load mock resources: ${response.status} ${response.statusText}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
const rawData: RawResourceData[] = await response.json();
|
||
|
|
|
||
|
|
return this.processResourceData(rawData);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Failed to load resource data:', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* NOT SUPPORTED - MockResourceRepository is read-only
|
||
|
|
*/
|
||
|
|
public async sendCreate(resource: IResource): Promise<IResource> {
|
||
|
|
throw new Error('MockResourceRepository does not support sendCreate. Mock data is read-only.');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* NOT SUPPORTED - MockResourceRepository is read-only
|
||
|
|
*/
|
||
|
|
public async sendUpdate(id: string, updates: Partial<IResource>): Promise<IResource> {
|
||
|
|
throw new Error('MockResourceRepository does not support sendUpdate. Mock data is read-only.');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* NOT SUPPORTED - MockResourceRepository is read-only
|
||
|
|
*/
|
||
|
|
public async sendDelete(id: string): Promise<void> {
|
||
|
|
throw new Error('MockResourceRepository does not support sendDelete. Mock data is read-only.');
|
||
|
|
}
|
||
|
|
|
||
|
|
private processResourceData(data: RawResourceData[]): IResource[] {
|
||
|
|
return data.map((resource): IResource => ({
|
||
|
|
...resource,
|
||
|
|
type: resource.type as ResourceType,
|
||
|
|
syncStatus: 'synced' as const
|
||
|
|
}));
|
||
|
|
}
|
||
|
|
}
|