Commit 36b97bc0 authored by Laura Fuentes Grau's avatar Laura Fuentes Grau

wip #210 added requestURL function to RestDataManager

parent 700c64fc
......@@ -52,57 +52,42 @@ class RestDataManager {
return object;
}
sendRequest(form, id, token, param, object = null, obj = null) {
switch (form) {
case 'load':
if (param === null) {
if (id != null) {
// load single object
return RestAPI.get(this.makeURL(this.url + '/' + id), token);
} else {
// load all objects
return RestAPI.get(this.makeURL(this.url), token);
requestURL(form, id, param, object = null){
switch(form){
case 'load/add':
if (param === null){
if(id != null){
return this.url + '/' + id;
}
}
else {
if (id != null) {
// load single object
return RestAPI.get(this.makeURL(this.url + '/' + id + '?' + param), token)
} else {
// load all objects
return RestAPI.get(this.makeURL(this.url) + '?' + param, token)
else {
return this.makeURL(this.url);
}
}
case 'add':
if (param === null) {
return RestAPI.post(this.makeURL(this.url), obj, token);
} else {
return RestAPI.post(this.makeURL(this.url) + "?" + param, obj, token);
}
case 'remove':
if (param === null) {
return RestAPI.delete(this.makeURL(this.url + '/' + object.id), token)
}
else {
return RestAPI.delete(this.makeURL(this.url + '/' + object.id + '?' + param))
else{
if(id != null){
return this.url + '/' + id + '?' + param;
}
else {
return this.makeURL(this.url) + '?' + param
}
}
case 'update':
if (param === null) {
return RestAPI.put(this.makeURL(this.url + '/' + object.id), obj, token);
case 'remove/update':
if(param === null){
return this.makeURL(this.url + '/' + object.id);
}
else {
return RestAPI.put(this.makeURL(this.url + '/' + object.id + '?' + param), obj, token);
else{
return this.makeURL(this.url + '/' + object.id + '?' + param);
}
default:
console.log("something went wrong");
break;
default:
console.log("something went wrong");
break;
}
}
load(id, token = null,param = null) {
if (id != null) {
// load single object
this.sendRequest('load',id,token,param).then(response => {
RestAPI.get(this.requestURL('load/add',id,param), token).then(response => {
const data = this.filterKeys(response[this.type]);
AppDispatcher.dispatch({
......@@ -121,7 +106,7 @@ class RestDataManager {
});
} else {
// load all objects
this.sendRequest('load',id,token,param).then(response => {
RestAPI.get(this.requestURL('load/add',id,param), token).then(response => {
const data = response[this.type + 's'].map(element => {
return this.filterKeys(element);
});
......@@ -148,7 +133,7 @@ class RestDataManager {
var obj = {};
obj[this.type] = this.filterKeys(object);
this.sendRequest('add',null,token,param,null,obj).then(response => {
RestAPI.post(this.requestURL('load/add',null,param), obj, token).then(response => {
AppDispatcher.dispatch({
type: this.type + 's/added',
data: response[this.type]
......@@ -162,7 +147,7 @@ class RestDataManager {
}
remove(object, token = null, param = null) {
this.sendRequest('remove',null,token,param,object).then(response => {
RestAPI.delete(this.requestURL('remove/update',null,param,object), token).then(response => {
AppDispatcher.dispatch({
type: this.type + 's/removed',
data: response[this.type],
......@@ -180,7 +165,7 @@ class RestDataManager {
var obj = {};
obj[this.type] = this.filterKeys(object);
this.sendRequest('update',null,token,param,object,obj).then(response => {
RestAPI.put(this.requestURL('remove/update',null,param,object), obj, token).then(response => {
AppDispatcher.dispatch({
type: this.type + 's/edited',
data: response[this.type]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment