chore: 🤖 setup API concerns for project resource

This commit is contained in:
Randall Morey committed 2020-05-19 16:33:31 -04:00
1 parent 818b2dca45
commit ae535c607e
19 files changed
+279 -2

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
import RESTAdapter from '@ember-data/adapter/rest';
import config from 'ember-get-config';
import { get } from '@ember/object';
export default class ApplicationAdapter extends RESTAdapter {
// =attributes
/**
* Sets namespace to the `api.namespace` string in the application's config,
* if set.
* @type {string}
*/
namespace = get(config, 'api.namespace');
}
+14
View File
@@ -0,0 +1,14 @@
import ApplicationAdapter from './application';
import ScopeAdapterMixin from '../mixins/scope-adapter';
export default class ProjectAdapter extends ApplicationAdapter.extend(
ScopeAdapterMixin
) {
// =attributes
/**
* Project is scoped to org only, technically.
* @type {boolean}
*/
includeProject = false;
}
@@ -0,0 +1,36 @@
import BaseModel from '../../models/base';
import { attr } from '@ember-data/model';
/**
* Project contains all fields related to a Project resource
*/
export default class GeneratedProjectModel extends BaseModel {
// =attributes
@attr('string', {
description: 'Optional name for identification purposes',
})
name;
@attr('string', {
description: 'Optional user-set description for identification purposes',
})
description;
@attr('date', {
description: 'The time this resource was created\nOutput only.',
readOnly: true,
})
created_time;
@attr('date', {
description: 'The time this resource was last updated\nOutput only.',
readOnly: true,
})
updated_time;
@attr('boolean', {
description: 'Whether the resource is disabled',
})
disabled;
}
+3
View File
@@ -0,0 +1,3 @@
import Model from '@ember-data/model';
export default class BaseModel extends Model {}
+3
View File
@@ -0,0 +1,3 @@
import GeneratedProjectModel from '../generated/models/project';
export default class OrgModel extends GeneratedProjectModel {}
@@ -0,0 +1,66 @@
import RESTSerializer from '@ember-data/serializer/rest';
import { underscore } from '@ember/string';
import { copy } from '@ember/object/internals';
/**
* Manages serialization/normalization of data to/from the API.
*/
export default class ApplicationSerializer extends RESTSerializer {
// =methods
/**
* Generates an underscored key for the attribute.
* @param {string} attr
* @return {string}
*/
keyForAttribute(attr) {
return underscore(attr);
}
/**
* If an attribute is annotated as readOnly in the model, don't serialize it.
* Otherwise delegate to default attribute serializer.
* @method serializeAttribute
* @param {Snapshot} snapshot
* @param {Object} json
* @param {String} key
* @param {Object} attribute
*/
serializeAttribute(snapshot, json, key, { options }) {
return options.readOnly ? null : super.serializeAttribute(...arguments);
}
/**
* In our API, array payloads are always rooted under the same key `items`.
* Ember Data normally expects them to be rooted under their model name,
* e.g. `{hostCatalogs: [...]}`. This method makes this transformation to
* accommodate Ember Data.
*
* @override
* @param {object} store
* @param {object} primaryModelClass
* @param {object} payload
* @param {?string} id
* @param {string} requestType
* @return {object}
*/
normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
// Setup a new payload data structure.
const transformedPayload = {};
// Find the Ember-data-expected root key name.
const payloadKey = this.payloadKeyFromModelName(
primaryModelClass.modelName
);
// Copy the data rooted under `items` in the existing payload into the new
// payload under the expected root key name.
transformedPayload[payloadKey] = copy(payload.items, true);
// Return the result of normalizing the transformed payload.
return super.normalizeArrayResponse(
store,
primaryModelClass,
transformedPayload,
id,
requestType
);
}
}
+1
View File
@@ -0,0 +1 @@
export { default } from 'api/adapters/application';
+1
View File
@@ -0,0 +1 @@
export { default } from 'api/adapters/project';
+1
View File
@@ -0,0 +1 @@
export { default } from 'api/models/base';
+1
View File
@@ -0,0 +1 @@
export { default } from 'api/models/project';
@@ -0,0 +1 @@
export { default } from 'api/serializers/application';
+1
View File
@@ -41,6 +41,7 @@
"ember-data": "^3.16.0",
"ember-disable-prototype-extensions": "^1.1.3",
"ember-export-application-global": "^2.0.1",
"ember-get-config": "^0.2.4",
"ember-load-initializers": "^2.1.1",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-qunit": "^4.6.0",
@@ -4,7 +4,7 @@ import { inject as service } from '@ember/service';
export default class ApplicationRoute extends Route {
@service scope;
afterModel() {
//console.log(this.scope);
model() {
this.store.findAll('project');
}
}
@@ -21,6 +21,10 @@ module.exports = function (environment) {
// Here you can pass flags/options to your application instance
// when it is created
},
api: {
namespace: '/api/addon/v1',
},
};
if (environment === 'development') {
@@ -0,0 +1,20 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import config from 'ember-get-config';
module('Unit | Adapter | application', function (hooks) {
setupTest(hooks);
test('its namespace is equal to the configured namespace', function (assert) {
assert.expect(2);
const adapter = this.owner.lookup('adapter:application');
assert.ok(adapter.namespace);
assert.equal(adapter.namespace, config.api.namespace);
});
test('it generates correct URL prefixes', function (assert) {
assert.expect(1);
const adapter = this.owner.lookup('adapter:application');
assert.equal(adapter.urlPrefix(), config.api.namespace);
});
});
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import config from 'ember-get-config';
module('Unit | Adapter | project', function (hooks) {
setupTest(hooks);
test('it generates URL prefixes without project scope', function (assert) {
assert.expect(1);
const service = this.owner.lookup('service:scope');
service.org = { id: 1 };
service.project = { id: 2 };
const adapter = this.owner.lookup('adapter:project');
assert.equal(adapter.urlPrefix(), `${config.api.namespace}/orgs/1`);
});
});
+13
View File
@@ -0,0 +1,13 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Model | base', function (hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function (assert) {
let store = this.owner.lookup('service:store');
let model = store.createRecord('base', {});
assert.ok(model);
});
});
@@ -0,0 +1,12 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Model | project', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
let store = this.owner.lookup('service:store');
let model = store.createRecord('project', {});
assert.ok(model);
});
});
@@ -0,0 +1,70 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Serializer | application', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('application');
assert.ok(serializer);
});
test('it serializes records', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const record = store.createRecord('project', {
name: 'Project',
description: 'Description',
});
const serializedRecord = record.serialize();
assert.deepEqual(serializedRecord, {
name: 'Project',
description: 'Description',
disabled: false,
});
});
test('it normalizes array records from an `items` root key', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('project');
const projectModelClass = store.createRecord('project').constructor;
const payload = {
items: [
{ id: 1, name: 'Project 1' },
{ id: 2, name: 'Project 2' },
{ id: 3, name: 'Project 3' },
],
};
const normalizedArray = serializer.normalizeArrayResponse(
store,
projectModelClass,
payload
);
assert.deepEqual(normalizedArray, {
included: [],
data: [
{
id: '1',
type: 'project',
attributes: { name: 'Project 1' },
relationships: {},
},
{
id: '2',
type: 'project',
attributes: { name: 'Project 2' },
relationships: {},
},
{
id: '3',
type: 'project',
attributes: { name: 'Project 3' },
relationships: {},
},
],
});
});
});