-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsaved-graph.service.ts
87 lines (67 loc) · 2.7 KB
/
saved-graph.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {Injectable} from "@angular/core";
import {Logger} from "@aws-amplify/core";
import {NotificationService} from "./notification.service";
import {DataStore} from "@aws-amplify/datastore";
import {SavedGraph} from "../../models";
import {PreferenceService} from "../pref/preference.service";
const log = new Logger("state-history-service");
/**
* Encapsulates the mechanics of CRUD operations on Saved Graphs.
* These graphs are used by the analytics graphs.
*
* @see SavedGraph
*/
@Injectable({
providedIn: "root"
})
export class SavedGraphService {
constructor(private _notify: NotificationService, private _prefs: PreferenceService) {
}
private _groups: string[] = [];
public get groups(): string[] {
return this._groups;
}
public async create(type: string, title: string, state: any, forOwner = true,
forGroup = false): Promise<SavedGraph> {
return await DataStore.save(new SavedGraph({
type,
title,
state: JSON.stringify(state),
owner: forOwner ? await this._prefs.username : null,
group: forOwner ? this._groups[0] : null
}));
}
public async update(id: string, title: string, state: any): Promise<SavedGraph> {
const savedGraph = await this.get(id);
return await DataStore.save(SavedGraph.copyOf(savedGraph, m => {
m.title = title;
m.state = JSON.stringify(state);
}));
}
public async get(id: string): Promise<SavedGraph | null> {
const results = await DataStore.query(SavedGraph, q => q.id("eq", id));
if (results.length === 0) {
return null;
} else {
console.log(results);
return results[0];
}
}
public async delete(id: string): Promise<SavedGraph> {
const results = await DataStore.delete(SavedGraph, q => q.id("eq", id));
if (results.length === 0) {
throw Error("No such state history " + id);
} else {
return results[0];
}
}
public async listByOwner(): Promise<SavedGraph[]> {
const username = await this._prefs.username;
return await DataStore.query(SavedGraph, q => q.owner("eq", username));
}
public async listByGroup(): Promise<SavedGraph[]> {
return await DataStore.query(SavedGraph, q => q.group("eq", this._groups[0]));
}
public async init() {
}
}