Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions app/common/models/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,66 @@

var RequestModel = (function () { // jshint ignore:line

var uri = 'https://abv.bg',
method = '',
body = '',
headers = [];

function RequestModel() {
this._uri = 'https://abv.bg';
this._method = '';
this._body = '';
this._headers = [];

}

Object.defineProperty(RequestModel.prototype, 'uri', {
get: function () {
return this._uri;
return uri;
},
set: function (value) {
this._uri = value;
}
uri = value;
},
enumerable: false
});

Object.defineProperty(RequestModel.prototype, 'method', {
get: function () {
return this._method;
return method;
},
set: function (value) {
this._method = value;
}
method = value;
},
enumerable: false
});

Object.defineProperty(RequestModel.prototype, 'body', {
get: function () {
return this._body;
return body;
},
set: function (value) {
this._body = value;
}
body = value;
},
enumerable: false
});

Object.defineProperty(RequestModel.prototype, 'headers', {
get: function () {
return this._headers;
return headers;
},
set: function (value) {
this._headers = value;
}
headers = value;
},
enumerable: false
});

RequestModel.prototype.reset = function (isHeaderAreaExpanded, defaultMethod) {
this.uri = '';
this.method = defaultMethod || AVAILABLE_METHODS[0];
this.body = '';
uri = '';
method = defaultMethod || AVAILABLE_METHODS[0];
body = '';
if (isHeaderAreaExpanded) {
this.headers = [{
name: '',
value: ''
}];
} else {
this.headers = [];
headers = [];
}
};

Expand Down
14 changes: 11 additions & 3 deletions app/common/services/request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

app.factory('Request', function () {
app.factory('Request', ['Storage', function (Storage) {

Storage.init();

return {

Expand All @@ -13,7 +15,7 @@ app.factory('Request', function () {
if (typeof (request.method) === 'undefined' ||
typeof (request.uri) === 'undefined' ||
typeof (request.headers) === 'undefined'
) {
) {
return;
}

Expand All @@ -28,6 +30,7 @@ app.factory('Request', function () {
url: request.uri,
data: verifiedReqBody,
beforeSend: function (xhr) {
xhr.time = Date.now();
var headers = request.headers;
for (var idx = 0; idx < headers.length; idx++) {
var header = headers[idx];
Expand All @@ -37,8 +40,13 @@ app.factory('Request', function () {
}
}
}
},
complete: function (xhr) {
Storage.addWithId(request.uri, request, function(err){

});
}
});
}
};
});
}]);
55 changes: 50 additions & 5 deletions app/common/services/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ app.factory('Storage', function () {
else {
db = new PouchDB(dbName);
}

//Debug only
window.PouchDB = db;
},

info: function (callback) {

if (typeof (db) === 'undefined') {
console.error('Database is not initialized');
callback('Database is not initialized');
Expand Down Expand Up @@ -54,13 +56,43 @@ app.factory('Storage', function () {
});
},

addWithId: function (id, object, callback) {
if (typeof (db) === 'undefined') {
console.error('Database is not initialized');
callback('Database is not initialized');
return;
}

if (typeof (object) === 'undefined') {
console.error('Object is not provided');
callback('Object is not provided');
return;
}

if (typeof (id) === 'undefined') {
console.error('ID is not provided');
callback('ID is not provided');
return;
}

object._id = id;

db.put(JSON.parse(JSON.stringify(object)))
.then(function (response) {
callback(null, response);
})
.catch(function (err) {
callback(err);
});
},

get: function (id, callback) {
if (typeof (db) === 'undefined') {
console.error('Database is not initialized');
callback('Database is not initialized');
return;
}

if (typeof (id) === 'undefined') {
console.error('ID is not provided');
callback('ID is not provided');
Expand All @@ -76,19 +108,32 @@ app.factory('Storage', function () {
});
},

getAll: function (callback) {

db.allDocs({
include_docs: true,
attachments: true
}).then(function (result) {
callback(null, result);
}).catch(function (err) {
callback(err);
console.log(err);
})
},

update: function (id, object, callback) {
if (typeof (db) === 'undefined') {
console.error('Database is not initialized');
callback('Database is not initialized');
return;
}

if (typeof (id) === 'undefined') {
console.error('ID is not provided');
callback('ID is not provided');
return;
}

if (typeof (object) === 'undefined') {
console.error('Object is not provided');
callback('Object is not provided');
Expand All @@ -114,7 +159,7 @@ app.factory('Storage', function () {
callback('Database is not initialized');
return;
}

if (typeof (id) === 'undefined') {
console.error('ID is not provided');
callback('ID is not provided');
Expand Down
11 changes: 6 additions & 5 deletions app/requester/requester.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ angular.module('RESTer.requester', ['ngRoute'])
.controller('RequesterController', ['$scope', 'Request','Storage',
function ($scope, Request, Storage) {

Storage.add();

$scope.methods = AVAILABLE_METHODS;

//Request
Expand All @@ -44,6 +42,10 @@ angular.module('RESTer.requester', ['ngRoute'])

//Response
$scope.response = new ResponseModel();

Storage.getAll(function(err, result){
console.log(result);
});

$scope.setMethod = function (methodType) {
if (typeof (methodType) === 'undefined') {
Expand All @@ -64,22 +66,21 @@ angular.module('RESTer.requester', ['ngRoute'])
return;
}

var requestTime = Date.now();
var request = Request.execute($scope.request);

if (typeof (request) === 'undefined') {
return;
}

request.done(function (data, statusText, xhr) {
var responseTime = (Date.now() - requestTime) + ' ms';
var responseTime = (Date.now() - xhr.time) + ' ms';
$scope.response.set(xhr, responseTime);
$scope.refreshResponseContainerDimentions();
$scope.$apply();
});

request.fail(function (xhr) {
var responseTime = (Date.now() - requestTime) + 'ms';
var responseTime = (Date.now() - xhr.time) + 'ms';
$scope.response.set(xhr, responseTime);
$scope.$apply();
});
Expand Down
Loading