diff --git a/build/app.js b/build/app.js index a31b76b..2bfc079 100644 --- a/build/app.js +++ b/build/app.js @@ -516,6 +516,17 @@ function GenericDao(model, qb) { value: function query(q) { return qb ? new qb(this, q) : new _QueryBuilder2['default'](this, q); } + }, { + key: 'getHeaders', + value: function getHeaders() {} + }, { + key: 'getOptions', + value: function getOptions() { + var opts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + opts.headers = this.getHeaders(); + return opts; + } }, { key: 'get', value: function get() { @@ -524,24 +535,32 @@ function GenericDao(model, qb) { var _this = this; var qb = arguments.length <= 0 || arguments[0] === undefined ? this.query() : arguments[0]; + var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; - var self = this; - return this.$http.get(this.url, { params: qb.opts }).then(function (data) { - if (!data.data) { - data.data = []; - } - return { - data: data.data.map(_this.build, _this), meta: { total: data.headers('X-Total-Count') } - }; + opts = this.getOptions(opts); + var params = _.merge(opts, { params: qb.opts }); + return this.$http.get(this.url, params).then(function (data) { + return _this.extractData(data); }); } + }, { + key: 'extractData', + value: function extractData(data) { + if (!data.data) { + data.data = []; + } + return { data: data.data.map(this.build, this) }; + } }, { key: 'count', value: function count() { var qb = arguments.length <= 0 || arguments[0] === undefined ? this.query() : arguments[0]; + var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; - var params = _.merge(qb.opts, { count: true }); - return this.$http.get(this.url, { params: params }); + opts = this.getOptions(); + var query = this.query(qb.opts); + query.count(); + return this.get(query, opts); } }, { key: 'build', @@ -555,36 +574,6 @@ function GenericDao(model, qb) { } return new model(this.$injector, this.url, data); } - }, { - key: 'post', - value: function post() { - // istanbul ignore next - - var _this2 = this; - - var qb = arguments.length <= 0 || arguments[0] === undefined ? this.query() : arguments[0]; - - var options = _.clone(qb.opts) || {}; - var condition; - switch (options.archived) { - case 'both': - condition = '(this.isArchived == false || this.isArchived == true) && (this.isDeleted == false)'; - break; - case 'true': - condition = '(this.isArchived == true) && (this.isDeleted == false)'; - break; - default: - condition = '(this.isArchived == false) && (this.isDeleted == false)'; - } - _.set(options, ['conditions', '$where'], condition); - return this.$http.post(this.url + '/filters', options).then(function (response) { - if (typeof response.data == 'number') { - return { meta: { total: response.headers('X-Total-Count') }, data: response.data }; - } else { - return { meta: { total: response.headers('X-Total-Count') }, data: response.data.map(_this2.build, _this2) }; - } - }); - } }, { key: 'create', value: function create(params) { @@ -615,19 +604,19 @@ function GenericDao(model, qb) { myClass.prototype['findById'] = myClass.prototype['getById'] = function (value) { // istanbul ignore next - var _this3 = this; + var _this2 = this; var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query : arguments[1]; return this.$http.get(this.url + '/' + value, { params: qb.opts }).then(function (data) { - return new model(_this3.$injector, _this3.url, data.data); + return new model(_this2.$injector, _this2.url, data.data); }); }; } else { myClass.prototype['selectBy' + _.capitalize(key)] = function (toSelect) { // istanbul ignore next - var _this4 = this; + var _this3 = this; var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query() : arguments[1]; @@ -650,7 +639,7 @@ function GenericDao(model, qb) { data.data = []; } return { - data: data.data.map(_this4.build, _this4), meta: { total: data.headers('X-Total-Count') } + data: data.data.map(_this3.build, _this3), meta: { total: data.headers('X-Total-Count') } }; }); }; @@ -729,6 +718,12 @@ var QueryBuilder = (function () { } return this; } + }, { + key: 'count', + value: function count() { + this.opts = this.opts || {}; + this.opts.count = true; + } }, { key: 'populate', value: function populate(populateArray) { diff --git a/dest/temp/GenericDao.js b/dest/temp/GenericDao.js index 01c57a4..dd7dc31 100644 --- a/dest/temp/GenericDao.js +++ b/dest/temp/GenericDao.js @@ -57,6 +57,17 @@ function GenericDao(model, qb) { value: function query(q) { return qb ? new qb(this, q) : new _QueryBuilder2['default'](this, q); } + }, { + key: 'getHeaders', + value: function getHeaders() {} + }, { + key: 'getOptions', + value: function getOptions() { + var opts = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; + + opts.headers = this.getHeaders(); + return opts; + } }, { key: 'get', value: function get() { @@ -65,24 +76,32 @@ function GenericDao(model, qb) { var _this = this; var qb = arguments.length <= 0 || arguments[0] === undefined ? this.query() : arguments[0]; + var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; - var self = this; - return this.$http.get(this.url, { params: qb.opts }).then(function (data) { - if (!data.data) { - data.data = []; - } - return { - data: data.data.map(_this.build, _this), meta: { total: data.headers('X-Total-Count') } - }; + opts = this.getOptions(opts); + var params = _.merge(opts, { params: qb.opts }); + return this.$http.get(this.url, params).then(function (data) { + return _this.extractData(data); }); } + }, { + key: 'extractData', + value: function extractData(data) { + if (!data.data) { + data.data = []; + } + return { data: data.data.map(this.build, this) }; + } }, { key: 'count', value: function count() { var qb = arguments.length <= 0 || arguments[0] === undefined ? this.query() : arguments[0]; + var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; - var params = _.merge(qb.opts, { count: true }); - return this.$http.get(this.url, { params: params }); + opts = this.getOptions(); + var query = this.query(qb.opts); + query.count(); + return this.get(query, opts); } }, { key: 'build', @@ -96,36 +115,6 @@ function GenericDao(model, qb) { } return new model(this.$injector, this.url, data); } - }, { - key: 'post', - value: function post() { - // istanbul ignore next - - var _this2 = this; - - var qb = arguments.length <= 0 || arguments[0] === undefined ? this.query() : arguments[0]; - - var options = _.clone(qb.opts) || {}; - var condition; - switch (options.archived) { - case 'both': - condition = '(this.isArchived == false || this.isArchived == true) && (this.isDeleted == false)'; - break; - case 'true': - condition = '(this.isArchived == true) && (this.isDeleted == false)'; - break; - default: - condition = '(this.isArchived == false) && (this.isDeleted == false)'; - } - _.set(options, ['conditions', '$where'], condition); - return this.$http.post(this.url + '/filters', options).then(function (response) { - if (typeof response.data == 'number') { - return { meta: { total: response.headers('X-Total-Count') }, data: response.data }; - } else { - return { meta: { total: response.headers('X-Total-Count') }, data: response.data.map(_this2.build, _this2) }; - } - }); - } }, { key: 'create', value: function create(params) { @@ -156,19 +145,19 @@ function GenericDao(model, qb) { myClass.prototype['findById'] = myClass.prototype['getById'] = function (value) { // istanbul ignore next - var _this3 = this; + var _this2 = this; var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query : arguments[1]; return this.$http.get(this.url + '/' + value, { params: qb.opts }).then(function (data) { - return new model(_this3.$injector, _this3.url, data.data); + return new model(_this2.$injector, _this2.url, data.data); }); }; } else { myClass.prototype['selectBy' + _.capitalize(key)] = function (toSelect) { // istanbul ignore next - var _this4 = this; + var _this3 = this; var qb = arguments.length <= 1 || arguments[1] === undefined ? this.query() : arguments[1]; @@ -191,7 +180,7 @@ function GenericDao(model, qb) { data.data = []; } return { - data: data.data.map(_this4.build, _this4), meta: { total: data.headers('X-Total-Count') } + data: data.data.map(_this3.build, _this3), meta: { total: data.headers('X-Total-Count') } }; }); }; diff --git a/dest/temp/QueryBuilder.js b/dest/temp/QueryBuilder.js index eb1f6d4..8969501 100644 --- a/dest/temp/QueryBuilder.js +++ b/dest/temp/QueryBuilder.js @@ -64,6 +64,12 @@ var QueryBuilder = (function () { } return this; } + }, { + key: 'count', + value: function count() { + this.opts = this.opts || {}; + this.opts.count = true; + } }, { key: 'populate', value: function populate(populateArray) { diff --git a/dest/temp/specs/angular-dao.specs.js b/dest/temp/specs/angular-dao.specs.js index 110ec77..f78a7f4 100644 --- a/dest/temp/specs/angular-dao.specs.js +++ b/dest/temp/specs/angular-dao.specs.js @@ -45,11 +45,6 @@ describe('Angular DAO', function () { httpBackend.flush(); }); - it('should make a post query', function () { - httpBackend.expectPOST('http://MOCKURL.com/model1/filters').respond(); - ModelManager.post(); - }); - it('should provide with a query builder', function () { var qb = ModelManager.query(); expect(qb).toBeDefined(); @@ -412,7 +407,7 @@ describe('Angular DAO', function () { return [204, undefined, { 'X-Total-Count': 0 }]; }); ModelManager.get().then(function (data) { - expect(data).toEqual({ data: [], meta: { total: '0' } }); + expect(data).toEqual({ data: [] }); }); httpBackend.flush(); }); diff --git a/package.json b/package.json index cf8bc32..12aab9e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "author": "flogou ", "name": "angular-orm", - "version": "1.1.5", + "version": "1.2.0", "description": "", "homepage": "", "dependencies": {}, diff --git a/src/GenericDao.js b/src/GenericDao.js index f4a7372..5310dce 100644 --- a/src/GenericDao.js +++ b/src/GenericDao.js @@ -33,21 +33,34 @@ export default function GenericDao(model, qb){ return qb ? new qb(this, q) : new QueryBuilder(this, q); } - get(qb = this.query()){ - var self = this; - return this.$http.get(this.url, {params: qb.opts}).then((data)=>{ - if (!data.data) { - data.data = [] - } - return { - data: data.data.map(this.build, this), meta: {total: data.headers('X-Total-Count')} - }; - }) + getHeaders(){ + + } + + getOptions(opts={}){ + opts.headers = this.getHeaders(); + return opts; } - count(qb= this.query()){ - var params = _.merge(qb.opts, {count:true}); - return this.$http.get(this.url, {params: params}); + get(qb = this.query(), opts={}){ + opts = this.getOptions(opts) + let params = _.merge(opts, {params: qb.opts}) + return this.$http.get(this.url, params) + .then(data =>this.extractData(data)) + } + + extractData(data){ + if (!data.data) { + data.data = [] + } + return { data: data.data.map(this.build, this)} + } + + count(qb=this.query(), opts={}){ + opts = this.getOptions(); + let query = this.query(qb.opts) + query.count() + return this.get(query, opts); } build(data){ @@ -61,29 +74,6 @@ export default function GenericDao(model, qb){ return new model(this.$injector, this.url, data); } - post(qb = this.query()){ - var options = _.clone(qb.opts) || {}; - var condition; - switch (options.archived){ - case 'both': - condition = '(this.isArchived == false || this.isArchived == true) && (this.isDeleted == false)'; - break; - case 'true': - condition = '(this.isArchived == true) && (this.isDeleted == false)'; - break; - default: - condition = '(this.isArchived == false) && (this.isDeleted == false)'; - } - _.set(options, ['conditions', '$where'], condition); - return this.$http.post(this.url + '/filters', options).then((response)=>{ - if (typeof response.data == 'number') { - return {meta: {total: response.headers('X-Total-Count')}, data: response.data}; - } else { - return {meta: {total: response.headers('X-Total-Count')}, data: response.data.map(this.build, this)}; - } - }) - } - create(params){ return new this.model(this.$injector, this.url, params); } diff --git a/src/QueryBuilder.js b/src/QueryBuilder.js index f6c56fe..6ab81f4 100644 --- a/src/QueryBuilder.js +++ b/src/QueryBuilder.js @@ -40,6 +40,11 @@ export default class QueryBuilder { return this; } + count(){ + this.opts = this.opts || {}; + this.opts.count = true; + } + populate (populateArray) { if (populateArray) { this.opts = this.opts || {}; diff --git a/tst/specs/angular-dao.specs.js b/tst/specs/angular-dao.specs.js index a7846d0..929ee0c 100644 --- a/tst/specs/angular-dao.specs.js +++ b/tst/specs/angular-dao.specs.js @@ -45,11 +45,6 @@ describe('Angular DAO', function () { httpBackend.flush(); }); - it('should make a post query', function () { - httpBackend.expectPOST('http://MOCKURL.com/model1/filters').respond(); - ModelManager.post(); - }); - it('should provide with a query builder', function () { var qb = ModelManager.query(); expect(qb).toBeDefined(); @@ -419,7 +414,7 @@ describe('Angular DAO', function () { return [204, undefined, {'X-Total-Count': 0}] }); ModelManager.get().then(function (data) { - expect(data).toEqual({data: [], meta: {total: '0'}}) + expect(data).toEqual({data: []}) }); httpBackend.flush();