Skip to content

Commit

Permalink
start getting model api stuff going
Browse files Browse the repository at this point in the history
  • Loading branch information
MyMediaMagnet committed Mar 21, 2018
1 parent e4d3c97 commit 370df4d
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 27 deletions.
2 changes: 1 addition & 1 deletion dist/models/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var _Collection = require('../classes/collection/Collection');

var _Collection2 = _interopRequireDefault(_Collection);

var _Query2 = require('../classes/Query');
var _Query2 = require('./Query');

var _Query3 = _interopRequireDefault(_Query2);

Expand Down
152 changes: 152 additions & 0 deletions dist/models/Query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _Collection = require('../classes/collection/Collection');

var _Collection2 = _interopRequireDefault(_Collection);

var _axios = require('axios');

var _axios2 = _interopRequireDefault(_axios);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var pluralize = require('pluralize');

var Query = function () {

// @todo still thinking about how this class whould work
// Ideally it will be a nice way to interact with an api
// For example User.get(1) should maybe get a particular user from the api
// Any api routes would need to both have structure (CRUD) and also allow for flexible starting route names (not only 'api')
function Query() {
_classCallCheck(this, Query);

if (new.target === Query) {
throw new TypeError("Cannot construct Query instances directly");
}

this.base_url = '/';
this.api_route = 'api';
this.route = pluralize.plural(this.constructor.name).toLowerCase();
}

// Make sure any class extending this is able to collect data


_createClass(Query, [{
key: 'collect',
value: function collect(data) {
throw new Error('The collect method has not been properly implemented in the Query child class');
}

// Get the full base path for all api calls on this model

}, {
key: 'getFullPath',
value: function getFullPath() {
return this.base_url + this.api_route + '/' + this.route + '/';
}

// Do an index api call

}], [{
key: 'index',
value: function index(params) {
var _this = this;

return new Promise(function (resolve, reject) {
return _axios2.default.get(_this.getFullPath, { params: params }).then(function (_ref) {
var data = _ref.data;

resolve(_this.collect(data));
}).catch(function (e) {
reject(e);
});
});
}

// Get a particular item from the api

}, {
key: 'get',
value: function get(id, callback) {
var _this2 = this;

return new Promise(function (resolve, reject) {
return _axios2.default.post(_this2.getFullPath + 'get', params).then(function (_ref2) {
var data = _ref2.data;

resolve(new _this2(data));
}).catch(function (e) {
reject(e);
});
});
}

// Create an item of this model type

}, {
key: 'create',
value: function create(data, callback) {
var _this3 = this;

return new Promise(function (resolve, reject) {
return _axios2.default.post(_this3.getFullPath + 'get', params).then(function (_ref3) {
var data = _ref3.data;

resolve(new _this3(data));
}).catch(function (e) {
reject(e);
});
});
}

// Update this item containing the primary key

}, {
key: 'update',
value: function update(data, callback) {
var _this4 = this;

return new Promise(function (resolve, reject) {
return _axios2.default.post(_this4.getFullPath + 'get', params).then(function (_ref4) {
var data = _ref4.data;

resolve(new _this4(data));
}).catch(function (e) {
reject(e);
});
});
}

// Delete an item by id

}, {
key: 'delete',
value: function _delete(id, callback) {
var _this5 = this;

return new Promise(function (resolve, reject) {
return _axios2.default.post(_this5.getFullPath + 'delete', params).then(function (_ref5) {
var data = _ref5.data;

resolve(data);
}).catch(function (e) {
reject(e);
});
});
}
}]);

return Query;
}();

exports.default = Query;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
"coveralls": "^3.0.0",
"faker": "^4.1.0",
"istanbul": "^0.4.5",
"mocha": "^5.0.4"
"mocha": "^5.0.4",
"moxios": "^0.4.0",
"sinon": "^4.4.6"
},
"dependencies": {
"axios": "^0.18.0"
"axios": "^0.18.0",
"pluralize": "^7.0.0"
}
}
23 changes: 0 additions & 23 deletions src/classes/Query.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/models/Model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Collection from '../classes/collection/Collection'
import Query from '../classes/Query'
import Query from './Query'
class Model extends Query{

// Setup a new Model instance
Expand Down
87 changes: 87 additions & 0 deletions src/models/Query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import Collection from '../classes/collection/Collection'
import axios from 'axios'
let pluralize = require('pluralize')
class Query {


// @todo still thinking about how this class whould work
// Ideally it will be a nice way to interact with an api
// For example User.get(1) should maybe get a particular user from the api
// Any api routes would need to both have structure (CRUD) and also allow for flexible starting route names (not only 'api')
constructor() {
if (new.target === Query) {
throw new TypeError("Cannot construct Query instances directly");
}

this.base_url = '/'
this.api_route = 'api'
this.route = pluralize.plural(this.constructor.name).toLowerCase()
}

// Make sure any class extending this is able to collect data
collect(data) {
throw new Error('The collect method has not been properly implemented in the Query child class');
}

// Get the full base path for all api calls on this model
getFullPath() {
return this.base_url + this.api_route + '/' + this.route + '/'
}

// Do an index api call
static index(params) {
return new Promise((resolve, reject) => {
return axios.get(this.getFullPath, {params: params}).then(({data}) => {
resolve(this.collect(data))
}).catch((e) => {
reject(e)
})
})
}

// Get a particular item from the api
static get(id, callback) {
return new Promise((resolve, reject) => {
return axios.post(this.getFullPath + 'get', params).then(({data}) => {
resolve(new this(data))
}).catch((e) => {
reject(e)
})
})
}

// Create an item of this model type
static create(data, callback) {
return new Promise((resolve, reject) => {
return axios.post(this.getFullPath + 'get', params).then(({data}) => {
resolve(new this(data))
}).catch((e) => {
reject(e)
})
})
}

// Update this item containing the primary key
static update(data, callback) {
return new Promise((resolve, reject) => {
return axios.post(this.getFullPath + 'get', params).then(({data}) => {
resolve(new this(data))
}).catch((e) => {
reject(e)
})
})
}

// Delete an item by id
static delete(id, callback) {
return new Promise((resolve, reject) => {
return axios.post(this.getFullPath + 'delete', params).then(({data}) => {
resolve(data)
}).catch((e) => {
reject(e)
})
})
}
}

export default Query;
8 changes: 8 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe('#Model', function() {
expect(user.email).to.equal('jack@jones.ca');
expect(user).to.instanceof(User);
});

it('it creates api routes based on the extending model', function() {
let object = {id: 1, name: 'Jack', email: 'jack@jones.ca'}
let user = new User()
user.set(object)
expect(user.route).to.equal('users');
expect(user.getFullPath()).to.equal('/api/users/');
});
});

class User extends Model {
Expand Down
25 changes: 25 additions & 0 deletions test/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

var expect = require('chai').expect
var faker = require('faker')
let Where = require('../dist/classes/collection/Where').default
let axios = require('axios')
let moxios = require('moxios')
let sinon = require('sinon')
let Collection = require('../dist/classes/collection/Collection').default
let Model = require('../dist/models/Model').default
let Query = require('../dist/models/Query').default

describe('#Query', function() {

it('it cannot be called as a function', function() {
expect(() => Query()).to.throw(TypeError)
});

});

class User extends Model {
constructor(data) {
super(data)
}
}

0 comments on commit 370df4d

Please sign in to comment.