/* * Copyright (C) 2018, 2019 StApps * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ import {Injectable} from '@angular/core'; import {Client} from '@openstapps/api/lib/client'; import {SCSearchQuery, SCSearchResponse, SCThingOriginType, SCThings, SCThingType} from '@openstapps/core'; import {SCSaveableThing} from '@openstapps/core'; import {StorageProvider} from '../storage/storage.provider'; import {StAppsWebHttpClient} from './stapps-web-http-client.provider'; export enum DataScope { Local = 'local', Remote = 'remote', } /* Generated class for the DataProvider provider. See https://angular.io/guide/dependency-injection for more info on providers and Angular DI. */ @Injectable() export class DataProvider { private _storagePrefix: string = 'stapps.data'; // @TODO: get backendUrl and appVersion and storagePrefix from config (module) appVersion: string = '1.0.6'; backendUrl: string = 'http://localhost:3000'; client: Client; storageProvider: StorageProvider; constructor(stAppsWebHttpClient: StAppsWebHttpClient, storageProvider: StorageProvider) { this.client = new Client(stAppsWebHttpClient, this.backendUrl, this.appVersion); this.storageProvider = storageProvider; } get storagePrefix(): string { return this._storagePrefix; } set storagePrefix(storagePrefix) { this._storagePrefix = storagePrefix; } /** * Provides key for storing data into the local database * * @param uid Unique identifier of a resource */ getDataKey(uid: string): string { return `${this.storagePrefix}.${uid}`; } /** * Provides a saveable thing from the local database using the provided UID */ async get(uid: string, scope: DataScope.Local): Promise>; /** * Provides a thing from the backend */ async get(uid: string, scope: DataScope.Remote): Promise>; /** * Provides a thing from both local database and backend */ async get(uid: string): Promise>>; /** * Provides a thing from the local database only, backend only or both, depending on the scope * * @param uid Unique identifier of a thing * @param scope From where data should be provided */ async get(uid: string, scope?: DataScope): Promise | Map>> { if (scope === DataScope.Local) { return this.storageProvider.get>(this.getDataKey(uid)); } if (scope === DataScope.Remote) { return this.client.getThing(uid); } const map: Map> = new Map(); map.set(DataScope.Local, await this.get(uid, DataScope.Local)); map.set(DataScope.Remote, await this.get(uid, DataScope.Remote)); return map; } /** * Provides all things saved in the local database */ async getAll(): Promise>> { return this.storageProvider.search>(this.storagePrefix); } /** * Save a data item * * @param item Data item that needs to be saved * @param [type] Saveable type (e.g. 'favorite'); if nothing is provided then type of the thing is used */ async put(item: SCThings, type?: SCThingType): Promise> { const saveableItem: SCSaveableThing = { data: item, name: item.name, origin: { created: new Date().toISOString(), type: SCThingOriginType.User, }, type: (typeof type === 'undefined') ? item.type : type, uid: item.uid, }; // @TODO: Implementation for saving item into the backend (user's account) return (await this.storageProvider.put>(this.getDataKey(item.uid), saveableItem)); } /** * Delete a data item * * @param uid Unique identifier of the saved data item */ async delete(uid: string): Promise { return this.storageProvider.delete(this.getDataKey(uid)); } /** * Delete all the previously saved data items */ async deleteAll(): Promise { const keys = Array.from((await this.getAll()).keys()); return this.storageProvider.delete(...keys); } /** * Searches the backend using the provided query and returns response * * @param query - query to send to the backend */ async search(query: SCSearchQuery): Promise { return (await this.client.search(query)); } /** * Provides information if something with an UID is saved as a data item * * @param uid Unique identifier of the saved data item */ async isSaved(uid: string): Promise { return this.storageProvider.has(this.getDataKey(uid)); } }