Commit e227a5c8 authored by Adrien PARROT's avatar Adrien PARROT
Browse files

API connection ok (CRUD on users)

parent c235e637
......@@ -9,26 +9,48 @@ sap.ui.controller("conceptmapper.userPage", {
* @memberOf conceptmapper.userPage
*/
onInit : function() {
var oData = {
"names" : [ {
username : "aparrot",
password : "toto",
firstname : "adrien",
lastname : "parrot",
address : "142",
email : "adrien.parrot@caramail.fr",
} , {
username : "mpondi",
password : "toto",
firstname : "marcy",
lastname : "pondi",
address : "142",
email : "marcy.pondi@caramail.fr",
} ]
};
var oModel = new sap.ui.model.json.JSONModel(oData);
sap.ui.getCore().setModel(oModel, "user");
$.ajax({
type:"GET",
url:"http://127.0.0.1:5000/users",
dataType:"json",
async: false,
header: {
"Content-Type": "application/json",
},
success: function(oData, response, xhr) {
//debugger;
var oModel = new sap.ui.model.json.JSONModel(oData);
sap.ui.getCore().setModel(oModel, "user");
console.log("connection");
},
error : function(error) {
alert("pb in the connection");
console.log(error);
}
});
// var oData1 = {
// "names" : [ {
// username : "aparrot",
// password : "toto",
// firstname : "adrien",
// lastname : "parrot",
// address : "142",
// email : "adrien.parrot@caramail.fr",
// } , {
// username : "mpondi",
// password : "toto",
// firstname : "marcy",
// lastname : "pondi",
// address : "142",
// email : "marcy.pondi@caramail.fr",
// } ]
// };
//
//debugger;
// var oModel = new sap.ui.model.json.JSONModel(oData);
// sap.ui.getCore().setModel(oModel, "user");
},
/******* CREATE *******/
......@@ -80,9 +102,10 @@ sap.ui.controller("conceptmapper.userPage", {
icon: "sap-icon://accept",
press: function () {
var content = oSimpleForm.getContent();
var oData = {};
oData.username = content[2].getValue();
oData.password = content[4].getValue();
oData.firstname = content[6].getValue();
......@@ -92,10 +115,33 @@ sap.ui.controller("conceptmapper.userPage", {
if (oData.username !== "" && oData.password !== ""
&& oData.firstname && oData.lastname && oData.address && oData.email){
var oModel = sap.ui.getCore().getModel('user');
var aData = oModel.getProperty("/names");
aData.push(oData);
oModel.setProperty("/names", aData);
$.ajax({
type:"POST",
url:"http://127.0.0.1:5000/user/" + oData.username,
dataType:"json",
data: oData,
async: false,
header: {
"Content-Type": "application/json",
},
success: function(data, response, xhr) {
// front update
var oModel = sap.ui.getCore().getModel('user');
var aData = oModel.getProperty("/names");
aData.push(oData);
oModel.setProperty("/names", aData);
// backend creation
sap.m.MessageToast.show("New user : '" + oData.username + "' created");
console.log("connection");
},
error : function(error) {
sap.m.MessageToast.show("Connexion error");
console.log(error);
}
});
} else {
sap.m.MessageToast.show("Nothing created!! \nYou have to file all the fields");
}
......@@ -146,6 +192,7 @@ sap.ui.controller("conceptmapper.userPage", {
}), new sap.m.Label({
text : "{i18n>username}"
}), new sap.ui.commons.TextField({
enabled: false,
value : user[0].getProperty('text')
}), new sap.m.Label({
text : "{i18n>password}"
......@@ -188,11 +235,37 @@ sap.ui.controller("conceptmapper.userPage", {
oData.lastname = content[8].getValue();
oData.address = content[10].getValue();
oData.email = content[12].getValue();
var oModel = sap.ui.getCore().getModel('user');
var aData = oModel.getProperty("/names");
aData.splice(idx[0], 1, oData);
oModel.setProperty("/names", aData);
$.ajax({
type:"PUT",
url:"http://127.0.0.1:5000/user/" + oData.username,
dataType:"json",
data: oData,
async: false,
header: {
"Content-Type": "application/json",
},
success: function(data, response, xhr) {
// front update
var oModel = sap.ui.getCore().getModel('user');
var aData = oModel.getProperty("/names");
aData.splice(idx[0], 1, oData);
oModel.setProperty("/names", aData);
// backend creation
sap.m.MessageToast.show("New user : '" + oData.username + " 'updated");
console.log("connection");
},
error : function(error) {
sap.m.MessageToast.show("Connexion error");
console.log(error);
}
});
oUpdateDialog.close();
}
......@@ -210,10 +283,7 @@ sap.ui.controller("conceptmapper.userPage", {
oUpdateDialog.open();
},
/******* DELETE *******/
openDeleteDialog : function (oEvt) {
var button_press = oEvt.getParameters().id;
......@@ -241,12 +311,39 @@ sap.ui.controller("conceptmapper.userPage", {
icon: "sap-icon://accept",
press: function () {
var oModel = sap.ui.getCore().getModel('user');
var aData = oModel.getProperty("/names");
aData.splice(idx[0], 1);
oModel.setProperty("/names", aData);
var URL = "http://127.0.0.1:5000/user/";
URL = URL + oTable_ref.getRows()[idx[0]].getCells()[0].getProperty('text');
$.ajax({
type:"DELETE",
url:URL,
dataType:"json",
async: false,
header: {
"Content-Type": "application/json",
},
success: function(data, response, xhr) {
// front update
var oModel = sap.ui.getCore().getModel('user');
var aData = oModel.getProperty("/names");
aData.splice(idx[0], 1);
oModel.setProperty("/names", aData);
oDeleteDialog.close();
// backend creation
sap.m.MessageToast.show("New user : '" + oData.username + " 'updated");
console.log("connection");
},
error : function(error) {
sap.m.MessageToast.show("Connexion error");
console.log(error);
}
});
oDeleteDialog.close();
}
}
})
);
......
......@@ -17,7 +17,7 @@
// create an app
var app = new sap.m.App({
initialPage : "idHomePage"
initialPage : "idUserPage"
});
// Adding all the pages to the app
......
Supports Markdown
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