Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
working on new db system
Browse files Browse the repository at this point in the history
  • Loading branch information
fulton committed May 5, 2020
1 parent 19b2851 commit 3c2df9e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
27 changes: 7 additions & 20 deletions src/main/java/com/andromeda/araserver/pages/UpdateDB.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.andromeda.araserver.pages

import com.andromeda.araserver.util.CosmosClients
import com.andromeda.araserver.util.DatabaseClient
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.microsoft.azure.documentdb.*
import org.json.JSONObject


class UpdateDB {
Expand All @@ -13,35 +12,23 @@ class UpdateDB {
val actions = mainVal.split("&")
var id:String? = null
var newVal:String? = null
var key:String? = null
var prop:String? = null
for (i in actions) when {
i.startsWith("id=") -> id = i.replace("id=", "")
i.startsWith("newval=") -> newVal = i.replace("newval=", "")
i.startsWith("prop=") -> prop = i.replace("prop=", "")
i.startsWith("user=") -> key = i.replace("user=", "")

}
val dbLink = System.getenv("IOTDB")
val client = DocumentClient("https://ara-account-data.documents.azure.com:443/", dbLink, ConnectionPolicy(), ConsistencyLevel.Session)
update(client, id!!, prop!!, newVal!!)
update(id!!, prop!!, key, newVal!!)
return "ok"
}

fun update(
client: DocumentClient,
id: String,
prop: String,
newVal: Any
){

val options = FeedOptions()
options.enableCrossPartitionQuery = true
val queryResults: FeedResponse<Document> = client.queryDocuments("/dbs/Ara-android-database/colls/Ara-android-collection", "SELECT * FROM c", options)
queryResults.queryIterable.forEach{
println(it.id)
if (it.id == id){
(it.get("document") as JSONObject).putOpt(prop, newVal)
println("got it")
client.replaceDocument(it, RequestOptions())}}
fun update(id: String, prop: String, user: String?, newVal: Any){
DatabaseClient().updateWithProp(user!!, id,prop, newVal )
}

fun arrayUpdate(url: String, postData:String): String {
Expand All @@ -58,7 +45,7 @@ class UpdateDB {

}
if(key.equals("")) throw SecurityException("not a valid user")
update(CosmosClients.client, id!!, prop!!, newVal)
update(id!!, prop!!, key, newVal)
return "ok"
}
fun fromJson(jsontxt: String?): Any {
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/andromeda/araserver/persona/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ class Main {
val yes = json.getInt("yes")
val no = json.getInt("no")
if (json.getString("name") == params.word){
if(LocaleData.containsNoWord(params.input)){ UpdateDB().update(CosmosClients.client, it.id, "no", no + 1)
if(LocaleData.containsNoWord(params.input)){ UpdateDB().update(
CosmosClients.client,
it.id,
"no",,
no + 1
)
println("no")
}
else UpdateDB().update(CosmosClients.client, it.id, "yes", yes + 1)
UpdateDB().update(CosmosClients.client, it.id, "total", total + 1)
else UpdateDB().update(CosmosClients.client, it.id, "yes",, yes + 1)
UpdateDB().update(CosmosClients.client, it.id, "total",, total + 1)
if (total + 1 == 11) addToMainDataBase(params.word, yes >= no)
return Gson().toJson(outputModel)
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/andromeda/araserver/skills/Reminders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class Reminders {
}
fun update(mainUrl:String): String {
val apiParams = ParseUrl().parseEditReminder(mainUrl, "/reminderu/")
UpdateDB().update(CosmosClients.client,apiParams.id, "header", apiParams.reminder.header)
apiParams.reminder.body?.let { UpdateDB().update(CosmosClients.client,apiParams.id, "body", it) }
apiParams.reminder.time?.let { UpdateDB().update(CosmosClients.client,apiParams.id, "time", it) }
UpdateDB().update(CosmosClients.client, apiParams.id, "header",, apiParams.reminder.header)
apiParams.reminder.body?.let { UpdateDB().update(CosmosClients.client, apiParams.id, "body",, it) }
apiParams.reminder.time?.let { UpdateDB().update(CosmosClients.client, apiParams.id, "time",, it) }

return "ok"
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/andromeda/araserver/util/DatabaseClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,18 @@ class DatabaseClient {
database.getCollection(userId).deleteOne(Filters.eq("_id", id))
}

fun updateWithProp(userId: String, id: String, prop:String, data: Any){
var any:Document? = null
val find = database.getCollection(userId).find(Filters.eq("_id", id))
find.forEach {
try {
any = it.get("document") as Document
any!![prop] = data
} catch (e: Exception) {
e.printStackTrace()
}
}
database.getCollection(userId).updateMany(Filters.eq("_id", id), Updates.set("document", any))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestNewCosmosDB {
val testData = OutputModel("test", "test", "test", "test", "test", "test")
var id = 123.toString()
NewDoc().newDoc("test", testData, id)
UpdateDB().update(CosmosClients.client, id, "title", "test2")
UpdateDB().update(CosmosClients.client, id, "title",, "test2")
DeleteDoc().delDoc(CosmosClients.client, "test", id)
}
}
Expand Down

0 comments on commit 3c2df9e

Please sign in to comment.