Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
FloGou committed Dec 20, 2016
1 parent e0c75f8 commit 7ac88a0
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 137 deletions.
83 changes: 39 additions & 44 deletions build/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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',
Expand All @@ -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) {
Expand Down Expand Up @@ -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];

Expand All @@ -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') }
};
});
};
Expand Down Expand Up @@ -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) {
Expand Down
77 changes: 33 additions & 44 deletions dest/temp/GenericDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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',
Expand All @@ -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) {
Expand Down Expand Up @@ -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];

Expand All @@ -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') }
};
});
};
Expand Down
6 changes: 6 additions & 0 deletions dest/temp/QueryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 1 addition & 6 deletions dest/temp/specs/angular-dao.specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "flogou <[email protected]>",
"name": "angular-orm",
"version": "1.1.5",
"version": "1.2.0",
"description": "",
"homepage": "",
"dependencies": {},
Expand Down
62 changes: 26 additions & 36 deletions src/GenericDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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);
}
Expand Down
Loading

0 comments on commit 7ac88a0

Please sign in to comment.