a7228cf83d
Repositories are a class of services to help with CRUD actions, most of the functionality is reused across various Models. This creates a new repository service that centralizes all this reused functionality. Inheritance via ember `Service.extend` is used as opposed to decorating via Mixins. 1. Move all repository services (and their tests) to a services/repository folder 2. Standardize on a singular name format 'node vs nodes' 3. Create a new 'repository' service to centralize functionality. This should be extended by 'repository' services
61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
import RepositoryService from 'consul-ui/services/repository';
|
|
import { Promise } from 'rsvp';
|
|
import isFolder from 'consul-ui/utils/isFolder';
|
|
import { get, set } from '@ember/object';
|
|
import { PRIMARY_KEY } from 'consul-ui/models/kv';
|
|
|
|
const modelName = 'kv';
|
|
export default RepositoryService.extend({
|
|
getModelName: function() {
|
|
return modelName;
|
|
},
|
|
getPrimaryKey: function() {
|
|
return PRIMARY_KEY;
|
|
},
|
|
// this one gives you the full object so key,values and meta
|
|
findBySlug: function(key, dc) {
|
|
if (isFolder(key)) {
|
|
const id = JSON.stringify([dc, key]);
|
|
let item = get(this, 'store').peekRecord(this.getModelName(), id);
|
|
if (!item) {
|
|
item = this.create();
|
|
set(item, 'Key', key);
|
|
set(item, 'Datacenter', dc);
|
|
}
|
|
return Promise.resolve(item);
|
|
}
|
|
return get(this, 'store').queryRecord(this.getModelName(), {
|
|
id: key,
|
|
dc: dc,
|
|
});
|
|
},
|
|
// this one only gives you keys
|
|
// https://www.consul.io/api/kv.html
|
|
findAllBySlug: function(key, dc) {
|
|
if (key === '/') {
|
|
key = '';
|
|
}
|
|
return this.get('store')
|
|
.query(this.getModelName(), {
|
|
id: key,
|
|
dc: dc,
|
|
separator: '/',
|
|
})
|
|
.then(function(items) {
|
|
return items.filter(function(item) {
|
|
return key !== get(item, 'Key');
|
|
});
|
|
})
|
|
.catch(e => {
|
|
if (e.errors && e.errors[0] && e.errors[0].status == '404') {
|
|
const id = JSON.stringify([dc, key]);
|
|
const record = get(this, 'store').peekRecord(this.getModelName(), id);
|
|
if (record) {
|
|
record.destroyRecord();
|
|
}
|
|
}
|
|
throw e;
|
|
});
|
|
},
|
|
});
|