feat: add favorites support

This commit is contained in:
Jovan Krunić
2021-09-10 15:39:23 +00:00
committed by Rainer Killinger
parent 293ed6ba5f
commit e9452d6520
20 changed files with 621 additions and 135 deletions

View File

@@ -15,18 +15,20 @@
import {Injectable} from '@angular/core';
import {Client} from '@openstapps/api/lib/client';
import {
SCFacet,
SCIndexableThings,
SCMultiSearchRequest,
SCMultiSearchResponse,
SCSearchRequest,
SCSearchResponse,
SCSearchValueFilter,
SCThing,
SCThingOriginType,
SCThings,
SCThingsField,
SCThingType,
SCSaveableThing,
} from '@openstapps/core';
import {SCSaveableThing} from '@openstapps/core';
import {chunk, fromPairs, toPairs} from 'lodash-es';
import {environment} from '../../../environments/environment';
import {StorageProvider} from '../storage/storage.provider';
@@ -110,6 +112,31 @@ export class DataProvider {
};
}
/**
* Create a facet from data
*
* @param items Data to generate facet for
* @param field Field for which to generate facet
*/
static facetForField(items: SCThing[], field: SCThingsField): SCFacet {
const bucketMap = new Map<string, number>();
const facet: SCFacet = {buckets: [], field: field};
for (const item of items) {
const value =
typeof bucketMap.get(item.type) === 'undefined'
? 1
: (bucketMap.get(item.type) as number) + 1;
bucketMap.set(item.type, value);
}
for (const [key, value] of bucketMap.entries()) {
facet.buckets.push({key: key, count: value});
}
return facet;
}
/**
* TODO
*
@@ -128,6 +155,28 @@ export class DataProvider {
this.storageProvider = storageProvider;
}
/**
* Create savable thing from an indexable thing
*
* @param item An indexable to create savable thing from
* @param type The type (falls back to the type of the indexable thing)
*/
static createSaveable(
item: SCIndexableThings,
type?: SCThingType,
): SCSaveableThing {
return {
data: item,
name: item.name,
origin: {
created: new Date().toISOString(),
type: SCThingOriginType.User,
},
type: typeof type === 'undefined' ? item.type : type,
uid: item.uid,
};
}
/**
* Delete a data item
*
@@ -234,28 +283,12 @@ export class DataProvider {
/**
* Save a data item
*
* @param item Data item that needs to be saved
* @param [type] Savable type (e.g. 'favorite'); if nothing is provided then type of the thing is used
* @param item An item that needs to be saved
*/
async put(
item: SCIndexableThings,
type?: SCThingType,
): Promise<SCSaveableThing> {
const savableItem: 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)
async put(item: SCIndexableThings): Promise<SCSaveableThing> {
return this.storageProvider.put<SCSaveableThing>(
this.getDataKey(item.uid),
savableItem,
DataProvider.createSaveable(item, item.type),
);
}