Skip to content

Instantly share code, notes, and snippets.

@fhightower
Created May 8, 2018 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhightower/906fbfdf1cf4d007585d57618c1e1ccf to your computer and use it in GitHub Desktop.
Save fhightower/906fbfdf1cf4d007585d57618c1e1ccf to your computer and use it in GitHub Desktop.
Generic datastore service for using ThreatConnect's datastore
import { Injectable } from '@angular/core';
import {
SpacesLoggingService,
SpacesMessagesService
} from 'spaces-ng';
import { TcExchangeDbService } from 'threatconnect-ng';
@Injectable()
export class DatastoreService {
public domain: string = 'organization'
public typeName: string = 'massIndicatorEnrichments';
constructor(
private exchangeDB: TcExchangeDbService,
private logging: SpacesLoggingService,
private messages: SpacesMessagesService
) {}
public getEntries(searchCommand: string) {
this.exchangeDB.read(this.domain, this.typeName, searchCommand)
.subscribe(
response => {
let entries: Array<{
name: string;
link: string;
}>;
for (var i = response.hits.hits.length - 1; i >= 0; i--) {
entries.push({
name: response.hits.hits[i]._source.name,
link: response.hits.hits[i]._source.link
});
}
return entries;
},
err => {
this.logging.error('Error', err);
this.messages.showError('Failed', 'Unable to retrieve the entries from the datastore: ' + err);
}
);
}
public save(searchCommand: string, linkName: string, link: string) {
let jsonifiedNoteText = JSON.parse('{"name": "' + linkName + '", "link": "' + link + '"}');
this.exchangeDB.create(this.domain, this.typeName, searchCommand, jsonifiedNoteText)
.subscribe(
response => {
this.messages.showSuccess('Success', 'Entry saved');
// if an entry is being updated, simply update the note's text rather than adding a new entry
if (searchCommand !== '') {
// handle entry updates here...
} else {
// handle new item creation here...
}
},
err => {
this.logging.error('Error', err);
this.messages.showError('Failed', 'Unable to save entry in the datastore: ' + err);
}
);
}
public delete(entryID: string) {
this.exchangeDB.delete(this.domain, this.typeName, entryID)
.subscribe(
response => {
this.messages.showSuccess('Success', 'Entry deleted');
// handle entry deletion here...
},
err => {
this.logging.error('Error', err);
this.messages.showError('Failed', 'Unable to delete entry from the datastore: ' + err);
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment