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

Commit

Permalink
added delete feature
Browse files Browse the repository at this point in the history
  • Loading branch information
fulton committed Feb 21, 2020
1 parent b5ebee9 commit f39e84d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
7 changes: 0 additions & 7 deletions src/main/java/com/andromeda/araserver/pages/ApiStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,4 @@ public String apiMain(String mainUri, KeyWord keyWord, Parser parse) {
return new MsSql().getSkills(term, searchterm, keyWord, parse);//ParseApi(searchterm);
}

private static Connection getConnection() throws SQLException {
//get a SQL connection (Note these creds are for test DB)
String dbUrl = "jdbc:postgresql://" + "ec2-54-221-214-3.compute-1.amazonaws.com" + ':' + "5432" + "/d40qc3ivndkhlh" + "?ssl=true" + "&sslfactory=org.postgresql.ssl.NonValidatingFactory";
return DriverManager.getConnection(dbUrl,
"ztpysbtdtzkeyt", "344eac98ba90d4b8b94166fbdc368bc1b3e887c20bce983cf469b2d1b64293a6");
}

}
17 changes: 14 additions & 3 deletions src/main/java/com/andromeda/araserver/util/CosmosClients.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package com.andromeda.araserver.util

import com.microsoft.azure.documentdb.ConnectionPolicy
import com.microsoft.azure.documentdb.ConsistencyLevel
import com.microsoft.azure.documentdb.DocumentClient
import com.microsoft.azure.documentdb.*
import com.microsoft.azure.documentdb.Document

object CosmosClients {
private val dbLink = System.getenv("IOTDB")
val client = DocumentClient("https://ara-account-data.documents.azure.com:443/", dbLink, ConnectionPolicy(), ConsistencyLevel.Session)
fun getKeyAsPK(userId: String): PartitionKey {
return PartitionKey(getKey(userId))
}
fun getKey(userId:String): String {
return "user-$userId"
}
fun feedResponse(options: FeedOptions, key: String, client: DocumentClient): FeedResponse<Document> {
options.partitionKey = CosmosClients.getKeyAsPK(key)
val queryResults: FeedResponse<Document> =
client.queryDocuments("/dbs/Ara-android-database/colls/Ara-android-collection", "SELECT * FROM c", options)
return queryResults
}

}
12 changes: 7 additions & 5 deletions src/main/java/com/andromeda/araserver/util/DeleteDoc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.andromeda.araserver.util

import com.microsoft.azure.cosmosdb.ConnectionPolicy
import com.microsoft.azure.documentdb.*
import com.microsoft.azure.documentdb.Document

class DeleteDoc {

Expand All @@ -17,19 +18,20 @@ class DeleteDoc {
delDoc(CosmosClients.client, key!!, id!!)
return ""
}
private fun delDoc(client: DocumentClient, key: String, id:String): String {
fun delDoc(client: DocumentClient, key: String, id:String): String {
val options = FeedOptions()
options.enableCrossPartitionQuery = true
val queryResults: FeedResponse<com.microsoft.azure.documentdb.Document> = client.queryDocuments("/dbs/Ara-android-database/colls/Ara-android-collection", "SELECT * FROM c", options)
val queryResults: FeedResponse<Document> = CosmosClients.feedResponse(options, key, client)
queryResults.queryIterator.forEach {
if (it.id == id){
val ro = RequestOptions()
println(it.resourceId)
println(it.selfLink)
ro.partitionKey = PartitionKey("user-$key")
client.deleteDocument("dbs/Ara-android-database/colls/Ara-android-collection/${it.resourceId}", ro)
ro.partitionKey = PartitionKey(CosmosClients.getKey(key))
client.deleteDocument(it.selfLink, ro)
}
}
return ""
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import org.junit.Test
class TestNewCosmosDB {
@Test
fun runTest(){
if (System.getenv().contains("IOTDB")){
val testData = OutputModel("test", "test", "test","test", "test", "test")
//NewDoc().newDoc("test", testData, 123.toString())
NewDoc().newDoc("test", testData, 123.toString())
//DeleteDoc().delDoc(CosmosClients.client, "test", 123.toString())
}
}
}

0 comments on commit f39e84d

Please sign in to comment.