feat(shared): add KMP create.php (text & doc) and delegate Android createNote*
This commit is contained in:
@@ -388,7 +388,6 @@ suspend fun createNote(
|
||||
* Skapar notis med rubrik/brödtext/bild/radius.
|
||||
* Returnerar true om servern svarar { ok: true }.
|
||||
*/
|
||||
|
||||
suspend fun createNoteDoc(
|
||||
lat: Double,
|
||||
lon: Double,
|
||||
@@ -402,25 +401,23 @@ suspend fun createNoteDoc(
|
||||
if (Auth.idToken == null || Auth.userId == null) {
|
||||
return false
|
||||
}
|
||||
val payload = CreateDocBody(
|
||||
lat = lat, lon = lon,
|
||||
title = title, body = body,
|
||||
imageId = imageId, radiusM = radiusM,
|
||||
return KlottrApiShared.createDoc(
|
||||
baseUrl = ApiConfig.SERVER_API_BASE_URL,
|
||||
apiKey = ApiConfig.SERVER_API_KEY,
|
||||
bearerToken = Auth.idToken,
|
||||
lat = lat,
|
||||
lon = lon,
|
||||
title = title,
|
||||
body = body,
|
||||
imageId = imageId,
|
||||
radiusM = radiusM,
|
||||
createdAt = createdAt,
|
||||
userId = Auth.userId,
|
||||
clientRequestId = clientRequestId
|
||||
)
|
||||
val resp = try { Api.svc.createDoc(payload, idempotencyKey = clientRequestId) } catch (_: Throwable) { return false }
|
||||
if (!resp.isSuccessful) return false
|
||||
val okField = resp.body()?.get("ok")
|
||||
return when (okField) {
|
||||
is Boolean -> okField
|
||||
is Number -> okField.toInt() != 0
|
||||
is String -> okField.equals("true", true) || okField == "1" || okField.equals("ok", true)
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Raderar (soft delete) en not du äger. Returnerar true om { ok: true }.
|
||||
*/
|
||||
|
||||
@@ -52,6 +52,30 @@ object KlottrApiShared {
|
||||
val lon: Double
|
||||
)
|
||||
|
||||
@kotlinx.serialization.Serializable
|
||||
private data class CreateBodyShared(
|
||||
val lat: Double,
|
||||
val lon: Double,
|
||||
val text: String,
|
||||
val createdAt: Long,
|
||||
val userId: String? = null,
|
||||
val clientRequestId: String? = null
|
||||
)
|
||||
|
||||
@kotlinx.serialization.Serializable
|
||||
private data class CreateDocBodyShared(
|
||||
val lat: Double,
|
||||
val lon: Double,
|
||||
val title: String,
|
||||
val body: String,
|
||||
val imageId: Long? = null,
|
||||
val radiusM: Int = 20,
|
||||
val createdAt: Long,
|
||||
val userId: String? = null,
|
||||
val clientRequestId: String? = null
|
||||
)
|
||||
|
||||
|
||||
private fun ensureSlash(u: String): String = if (u.endsWith("/")) u else "$u/"
|
||||
|
||||
/**
|
||||
@@ -190,5 +214,100 @@ object KlottrApiShared {
|
||||
userId = userId
|
||||
)
|
||||
}
|
||||
/**
|
||||
* KMP-variant av create.php (textbaserad). Returnerar true om { ok: true }.
|
||||
*/
|
||||
suspend fun create(
|
||||
baseUrl: String,
|
||||
apiKey: String,
|
||||
bearerToken: String?,
|
||||
lat: Double,
|
||||
lon: Double,
|
||||
text: String,
|
||||
createdAt: Long,
|
||||
userId: String?,
|
||||
clientRequestId: String?
|
||||
): Boolean {
|
||||
val url = ensureSlash(baseUrl) + "create.php"
|
||||
val body = CreateBodyShared(
|
||||
lat = lat, lon = lon, text = text, createdAt = createdAt,
|
||||
userId = userId, clientRequestId = clientRequestId
|
||||
)
|
||||
|
||||
val resp = try {
|
||||
client.post(url) {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(body)
|
||||
header("X-API-Key", apiKey)
|
||||
if (bearerToken != null) header(HttpHeaders.Authorization, "Bearer $bearerToken")
|
||||
if (clientRequestId != null) header("X-Idempotency-Key", clientRequestId)
|
||||
}
|
||||
} catch (_: Throwable) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (resp.status.value != 200) return false
|
||||
|
||||
val obj = try { resp.body<JsonObject>() } catch (_: Throwable) { return false }
|
||||
val okPrim = obj["ok"]?.jsonPrimitive
|
||||
return when {
|
||||
okPrim == null -> true
|
||||
okPrim.isString -> okPrim.content.equals("true", true) || okPrim.content == "1" || okPrim.content.equals("ok", true)
|
||||
okPrim.booleanOrNull != null -> okPrim.boolean
|
||||
okPrim.longOrNull != null -> okPrim.long != 0L
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* KMP-variant av create.php (dokumentvarianten). Returnerar true om { ok: true }.
|
||||
*/
|
||||
suspend fun createDoc(
|
||||
baseUrl: String,
|
||||
apiKey: String,
|
||||
bearerToken: String?,
|
||||
lat: Double,
|
||||
lon: Double,
|
||||
title: String,
|
||||
body: String,
|
||||
imageId: Long?,
|
||||
radiusM: Int,
|
||||
createdAt: Long,
|
||||
userId: String?,
|
||||
clientRequestId: String?
|
||||
): Boolean {
|
||||
val url = ensureSlash(baseUrl) + "create.php"
|
||||
val payload = CreateDocBodyShared(
|
||||
lat = lat, lon = lon,
|
||||
title = title, body = body,
|
||||
imageId = imageId, radiusM = radiusM,
|
||||
createdAt = createdAt,
|
||||
userId = userId,
|
||||
clientRequestId = clientRequestId
|
||||
)
|
||||
|
||||
val resp = try {
|
||||
client.post(url) {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(payload)
|
||||
header("X-API-Key", apiKey)
|
||||
if (bearerToken != null) header(HttpHeaders.Authorization, "Bearer $bearerToken")
|
||||
if (clientRequestId != null) header("X-Idempotency-Key", clientRequestId)
|
||||
}
|
||||
} catch (_: Throwable) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (resp.status.value != 200) return false
|
||||
|
||||
val obj = try { resp.body<JsonObject>() } catch (_: Throwable) { return false }
|
||||
val okPrim = obj["ok"]?.jsonPrimitive
|
||||
return when {
|
||||
okPrim == null -> true
|
||||
okPrim.isString -> okPrim.content.equals("true", true) || okPrim.content == "1" || okPrim.content.equals("ok", true)
|
||||
okPrim.booleanOrNull != null -> okPrim.boolean
|
||||
okPrim.longOrNull != null -> okPrim.long != 0L
|
||||
else -> true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user