Small improbvements to the API

This commit is contained in:
Scharel Clemens 2018-11-25 14:07:37 +01:00
parent 030d36cb71
commit 7f27a87aa3

View file

@ -15,13 +15,14 @@ Item {
property bool unencryptedConnection property bool unencryptedConnection
property var model: ListModel { } property var model: ListModel { }
property var json: [ ]
//property string file: StandardPaths.data + "/" + uuid + ".json" //property string file: StandardPaths.data + "/" + uuid + ".json"
//property bool saveFile: false //property bool saveFile: false
property bool busy: false property bool busy: false
property int status: 204 property int status: 204
property string statusText: "No Content" property string statusText: "No Content"
//TODO: put content into an array onStatusChanged: {
console.log("Network status: " + statusText + " (" + status + ")")
}
ConfigurationGroup { ConfigurationGroup {
id: account id: account
@ -72,50 +73,63 @@ Item {
apiReq.onreadystatechange = function() { apiReq.onreadystatechange = function() {
if (apiReq.readyState === XMLHttpRequest.DONE) { if (apiReq.readyState === XMLHttpRequest.DONE) {
if (apiReq.status === 200) { if (apiReq.status === 200) {
//console.log("Successfull API request!") //console.log("Network status: " + apiReq.statusText + " (" + apiReq.status + ")")
console.log("Network status: " + apiReq.statusText + " (" + apiReq.status + ")")
json = JSON.parse(apiReq.responseText) var json = JSON.parse(apiReq.responseText)
switch(method) { switch(method) {
case "GET": case "GET":
if (Array.isArray(json)) { if (Array.isArray(json)) {
console.log("Received all notes via API: " + endpoint) console.log("Received all notes via API: " + endpoint)
model.clear() //model.clear()
for (var element in json) { for (var element in json) {
model.append(json[element]) model.set(element, json[element])
model.setProperty(element, "date", getDisplayDate(json[element].modified)) model.setProperty(element, "date", getDisplayDate(json[element].modified))
} }
element++
while (model.count > element) {
model.remove(element)
}
update = new Date() update = new Date()
} }
else { else {
console.log("Received a single note via API: " + endpoint) console.log("Received a single note via API: " + endpoint)
var noteModified = false
//json.date = getDisplayDate(json.modified)
for (var i = 0; i < model.count; i++) { for (var i = 0; i < model.count; i++) {
var listItem = model.get(i) var listItem = model.get(i)
if (listItem.id === json.id){ if (listItem.id === json.id){
model.set(i, json) model.set(i, json)
model.setProperty(i, "date", getDisplayDate(json.modified)) model.setProperty(i, "date", getDisplayDate(json.modified))
noteModified = true
} }
} }
if (!noteModified) {
//json.date = getDisplayDate(json.modified)
model.set(i, json)
model.setProperty(i, "date", getDisplayDate(json.modified))
}
} }
break; break;
case "POST": case "POST":
console.log("Created a note via API: " + endpoint) console.log("Created a note via API: " + endpoint)
model.append(json) model.set(model.count, json)
model.setProperty(model.count-1, "date", getDisplayDate(json.modified))
model.move(model.count-1, 0, 1) model.move(model.count-1, 0, 1)
break; break;
case "PUT": case "PUT":
console.log("Updated a note via API: " + endpoint) console.log("Updated a note via API: " + endpoint)
for (var i = 0; i < model.count; i++) { for (i = 0; i < model.count; i++) {
var listItem = model.get(i) listItem = model.get(i)
if (listItem.id === json.id){ if (listItem.id === json.id){
model.set(i, json) model.set(i, json)
model.setProperty(i, "date", getDisplayDate(json.modified))
} }
} }
break; break;
case "DELETE": case "DELETE":
console.log("Deleted a note via API: " + endpoint) console.log("Deleted a note via API: " + endpoint)
for (var i = 0; i < model.count; i++) { for (i = 0; i < model.count; i++) {
var listItem = model.get(i) listItem = model.get(i)
if (listItem.id === data.id){ if (listItem.id === data.id){
model.remove(i) model.remove(i)
} }
@ -136,10 +150,10 @@ Item {
console.log("Note does not exist!") console.log("Note does not exist!")
}*/ }*/
else { else {
console.log("Network error: " + apiReq.statusText + " (" + apiReq.status + ")") //console.log("Network error: " + apiReq.statusText + " (" + apiReq.status + ")")
} }
status = apiReq.status
statusText = apiReq.statusText statusText = apiReq.statusText
status = apiReq.status
//model.sync() //model.sync()
busy = false busy = false
} }