Add inc/dec

This commit is contained in:
Jake Barnby
2025-07-18 12:48:19 +12:00
parent 151e7a021f
commit 39fdc6e2eb
11 changed files with 267 additions and 7 deletions
+3 -3
View File
@@ -2,7 +2,7 @@
![Maven Central](https://img.shields.io/maven-central/v/io.appwrite/sdk-for-android.svg?color=green&style=flat-square)
![License](https://img.shields.io/github/license/appwrite/sdk-for-android.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.7.0-blue.svg?style=flat-square)
![Version](https://img.shields.io/badge/api%20version-1.7.4-blue.svg?style=flat-square)
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
@@ -38,7 +38,7 @@ repositories {
Next, add the dependency to your project's `build.gradle(.kts)` file:
```groovy
implementation("io.appwrite:sdk-for-android:8.1.0")
implementation("io.appwrite:sdk-for-android:8.2.0")
```
### Maven
@@ -49,7 +49,7 @@ Add this to your project's `pom.xml` file:
<dependency>
<groupId>io.appwrite</groupId>
<artifactId>sdk-for-android</artifactId>
<version>8.1.0</version>
<version>8.2.0</version>
</dependency>
</dependencies>
```
@@ -4,6 +4,7 @@ import io.appwrite.services.Databases;
Client client = new Client(context)
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
.setAdmin("") //
.setSession("") // The user session to authenticate with
.setKey("") //
.setJWT("<YOUR_JWT>"); // Your secret JSON Web Token
@@ -0,0 +1,27 @@
import io.appwrite.Client;
import io.appwrite.coroutines.CoroutineCallback;
import io.appwrite.services.Databases;
Client client = new Client(context)
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
.setProject("<YOUR_PROJECT_ID>"); // Your project ID
Databases databases = new Databases(client);
databases.decrementDocumentAttribute(
"<DATABASE_ID>", // databaseId
"<COLLECTION_ID>", // collectionId
"<DOCUMENT_ID>", // documentId
"", // attribute
0, // value (optional)
0, // min (optional)
new CoroutineCallback<>((result, error) -> {
if (error != null) {
error.printStackTrace();
return;
}
Log.d("Appwrite", result.toString());
})
);
@@ -0,0 +1,27 @@
import io.appwrite.Client;
import io.appwrite.coroutines.CoroutineCallback;
import io.appwrite.services.Databases;
Client client = new Client(context)
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
.setProject("<YOUR_PROJECT_ID>"); // Your project ID
Databases databases = new Databases(client);
databases.incrementDocumentAttribute(
"<DATABASE_ID>", // databaseId
"<COLLECTION_ID>", // collectionId
"<DOCUMENT_ID>", // documentId
"", // attribute
0, // value (optional)
0, // max (optional)
new CoroutineCallback<>((result, error) -> {
if (error != null) {
error.printStackTrace();
return;
}
Log.d("Appwrite", result.toString());
})
);
@@ -4,6 +4,7 @@ import io.appwrite.services.Databases
val client = Client(context)
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
.setAdmin("") //
.setSession("") // The user session to authenticate with
.setKey("") //
.setJWT("<YOUR_JWT>") // Your secret JSON Web Token
@@ -0,0 +1,18 @@
import io.appwrite.Client
import io.appwrite.coroutines.CoroutineCallback
import io.appwrite.services.Databases
val client = Client(context)
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
.setProject("<YOUR_PROJECT_ID>") // Your project ID
val databases = Databases(client)
val result = databases.decrementDocumentAttribute(
databaseId = "<DATABASE_ID>",
collectionId = "<COLLECTION_ID>",
documentId = "<DOCUMENT_ID>",
attribute = "",
value = 0, // (optional)
min = 0, // (optional)
)
@@ -0,0 +1,18 @@
import io.appwrite.Client
import io.appwrite.coroutines.CoroutineCallback
import io.appwrite.services.Databases
val client = Client(context)
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1") // Your API Endpoint
.setProject("<YOUR_PROJECT_ID>") // Your project ID
val databases = Databases(client)
val result = databases.incrementDocumentAttribute(
databaseId = "<DATABASE_ID>",
collectionId = "<COLLECTION_ID>",
documentId = "<DOCUMENT_ID>",
attribute = "",
value = 0, // (optional)
max = 0, // (optional)
)
+1 -1
View File
@@ -87,7 +87,7 @@ class Client @JvmOverloads constructor(
"x-sdk-name" to "Android",
"x-sdk-platform" to "client",
"x-sdk-language" to "android",
"x-sdk-version" to "8.1.0",
"x-sdk-version" to "8.2.0",
"x-appwrite-response-format" to "1.7.0"
)
config = mutableMapOf()
@@ -14,7 +14,9 @@ enum class ImageFormat(val value: String) {
@SerializedName("heic")
HEIC("heic"),
@SerializedName("avif")
AVIF("avif");
AVIF("avif"),
@SerializedName("gif")
GIF("gif");
override fun toString() = value
}
@@ -13,6 +13,12 @@ data class Document<T>(
@SerializedName("\$id")
val id: String,
/**
* Document automatically incrementing ID.
*/
@SerializedName("\$sequence")
val sequence: Long,
/**
* Collection ID.
*/
@@ -51,6 +57,7 @@ data class Document<T>(
) {
fun toMap(): Map<String, Any> = mapOf(
"\$id" to id as Any,
"\$sequence" to sequence as Any,
"\$collectionId" to collectionId as Any,
"\$databaseId" to databaseId as Any,
"\$createdAt" to createdAt as Any,
@@ -62,6 +69,7 @@ data class Document<T>(
companion object {
operator fun invoke(
id: String,
sequence: Long,
collectionId: String,
databaseId: String,
createdAt: String,
@@ -70,6 +78,7 @@ data class Document<T>(
data: Map<String, Any>
) = Document<Map<String, Any>>(
id,
sequence,
collectionId,
databaseId,
createdAt,
@@ -84,6 +93,7 @@ data class Document<T>(
nestedType: Class<T>
) = Document<T>(
id = map["\$id"] as String,
sequence = (map["\$sequence"] as Number).toLong(),
collectionId = map["\$collectionId"] as String,
databaseId = map["\$databaseId"] as String,
createdAt = map["\$createdAt"] as String,
@@ -212,7 +212,7 @@ class Databases(client: Client) : Service(client) {
)
/**
* Create or update a Document. Before using this route, you should create a new collection resource using either a [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) API or directly from your database console.
* **WARNING: Experimental Feature** - This endpoint is experimental and not yet officially supported. It may be subject to breaking changes or removal in future versions.Create or update a Document. Before using this route, you should create a new collection resource using either a [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) API or directly from your database console.
*
* @param databaseId Database ID.
* @param collectionId Collection ID.
@@ -257,7 +257,7 @@ class Databases(client: Client) : Service(client) {
}
/**
* Create or update a Document. Before using this route, you should create a new collection resource using either a [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) API or directly from your database console.
* **WARNING: Experimental Feature** - This endpoint is experimental and not yet officially supported. It may be subject to breaking changes or removal in future versions.Create or update a Document. Before using this route, you should create a new collection resource using either a [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection) API or directly from your database console.
*
* @param databaseId Database ID.
* @param collectionId Collection ID.
@@ -388,4 +388,160 @@ class Databases(client: Client) : Service(client) {
}
/**
* Decrement a specific attribute of a document by a given value.
*
* @param databaseId Database ID.
* @param collectionId Collection ID.
* @param documentId Document ID.
* @param attribute Attribute key.
* @param value Value to decrement the attribute by. The value must be a number.
* @param min Minimum value for the attribute. If the current value is lesser than this value, an exception will be thrown.
* @return [io.appwrite.models.Document<T>]
*/
@JvmOverloads
suspend fun <T> decrementDocumentAttribute(
databaseId: String,
collectionId: String,
documentId: String,
attribute: String,
value: Double? = null,
min: Double? = null,
nestedType: Class<T>,
): io.appwrite.models.Document<T> {
val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}/{attribute}/decrement"
.replace("{databaseId}", databaseId)
.replace("{collectionId}", collectionId)
.replace("{documentId}", documentId)
.replace("{attribute}", attribute)
val apiParams = mutableMapOf<String, Any?>(
"value" to value,
"min" to min,
)
val apiHeaders = mutableMapOf<String, String>(
"content-type" to "application/json",
)
val converter: (Any) -> io.appwrite.models.Document<T> = {
@Suppress("UNCHECKED_CAST")
io.appwrite.models.Document.from(map = it as Map<String, Any>, nestedType)
}
return client.call(
"PATCH",
apiPath,
apiHeaders,
apiParams,
responseType = classOf(),
converter,
)
}
/**
* Decrement a specific attribute of a document by a given value.
*
* @param databaseId Database ID.
* @param collectionId Collection ID.
* @param documentId Document ID.
* @param attribute Attribute key.
* @param value Value to decrement the attribute by. The value must be a number.
* @param min Minimum value for the attribute. If the current value is lesser than this value, an exception will be thrown.
* @return [io.appwrite.models.Document<T>]
*/
@JvmOverloads
@Throws(AppwriteException::class)
suspend fun decrementDocumentAttribute(
databaseId: String,
collectionId: String,
documentId: String,
attribute: String,
value: Double? = null,
min: Double? = null,
): io.appwrite.models.Document<Map<String, Any>> = decrementDocumentAttribute(
databaseId,
collectionId,
documentId,
attribute,
value,
min,
nestedType = classOf(),
)
/**
* Increment a specific attribute of a document by a given value.
*
* @param databaseId Database ID.
* @param collectionId Collection ID.
* @param documentId Document ID.
* @param attribute Attribute key.
* @param value Value to increment the attribute by. The value must be a number.
* @param max Maximum value for the attribute. If the current value is greater than this value, an error will be thrown.
* @return [io.appwrite.models.Document<T>]
*/
@JvmOverloads
suspend fun <T> incrementDocumentAttribute(
databaseId: String,
collectionId: String,
documentId: String,
attribute: String,
value: Double? = null,
max: Double? = null,
nestedType: Class<T>,
): io.appwrite.models.Document<T> {
val apiPath = "/databases/{databaseId}/collections/{collectionId}/documents/{documentId}/{attribute}/increment"
.replace("{databaseId}", databaseId)
.replace("{collectionId}", collectionId)
.replace("{documentId}", documentId)
.replace("{attribute}", attribute)
val apiParams = mutableMapOf<String, Any?>(
"value" to value,
"max" to max,
)
val apiHeaders = mutableMapOf<String, String>(
"content-type" to "application/json",
)
val converter: (Any) -> io.appwrite.models.Document<T> = {
@Suppress("UNCHECKED_CAST")
io.appwrite.models.Document.from(map = it as Map<String, Any>, nestedType)
}
return client.call(
"PATCH",
apiPath,
apiHeaders,
apiParams,
responseType = classOf(),
converter,
)
}
/**
* Increment a specific attribute of a document by a given value.
*
* @param databaseId Database ID.
* @param collectionId Collection ID.
* @param documentId Document ID.
* @param attribute Attribute key.
* @param value Value to increment the attribute by. The value must be a number.
* @param max Maximum value for the attribute. If the current value is greater than this value, an error will be thrown.
* @return [io.appwrite.models.Document<T>]
*/
@JvmOverloads
@Throws(AppwriteException::class)
suspend fun incrementDocumentAttribute(
databaseId: String,
collectionId: String,
documentId: String,
attribute: String,
value: Double? = null,
max: Double? = null,
): io.appwrite.models.Document<Map<String, Any>> = incrementDocumentAttribute(
databaseId,
collectionId,
documentId,
attribute,
value,
max,
nestedType = classOf(),
)
}