@@ -2,26 +2,69 @@ package com.ctrlhub.core.datacapture
2
2
3
3
import com.ctrlhub.core.api.response.PaginatedList
4
4
import com.ctrlhub.core.datacapture.response.FormSchema
5
+ import com.ctrlhub.core.extractPaginationFromMeta
5
6
import com.ctrlhub.core.router.Router
6
7
import com.ctrlhub.core.router.request.RequestParameters
7
8
import io.ktor.client.HttpClient
9
+ import io.ktor.client.call.body
10
+ import kotlinx.serialization.json.*
8
11
9
- /* *
10
- * A router that interacts with the form schemas realm of the Ctrl Hub API
11
- */
12
- class FormSchemasRouter (httpClient : HttpClient ): Router(httpClient) {
13
-
14
- /* *
15
- * Get all form schemas for a given form and organisation
16
- *
17
- * @param organisationId String The organisation ID to retrieve all for schemas for
18
- * @param formId String The form ID to retrieve all schemas for
19
- *
20
- * @return A paginated response of all form schemas
21
- */
22
- suspend fun all (organisationId : String , formId : String , requestParameters : RequestParameters = RequestParameters ()): PaginatedList <FormSchema > {
12
+ class FormSchemasRouter (httpClient : HttpClient ) : Router(httpClient) {
13
+
14
+ suspend fun all (
15
+ organisationId : String ,
16
+ formId : String ,
17
+ requestParameters : RequestParameters = RequestParameters ()
18
+ ): PaginatedList <FormSchema > {
23
19
val endpoint = " /v3/orgs/${organisationId} /data-capture/forms/{$formId }/schemas"
24
20
25
- return fetchPaginatedJsonApiResources(endpoint, requestParameters.toMap(), FormSchema ::class .java)
21
+ val response = performGet(endpoint, requestParameters.toMap())
22
+ val jsonContent = Json .parseToJsonElement(response.body<String >()).jsonObject
23
+
24
+ val dataArray = jsonContent[" data" ]?.jsonArray ? : JsonArray (emptyList())
25
+ val formSchemas = dataArray.mapNotNull { item ->
26
+ item.jsonObjectOrNull()?.let { instantiateFormSchemaFromJson(it) }
27
+ }
28
+
29
+ return PaginatedList (
30
+ data = formSchemas,
31
+ pagination = extractPaginationFromMeta(jsonContent)
32
+ )
26
33
}
34
+
35
+ suspend fun one (
36
+ organisationId : String ,
37
+ formId : String ,
38
+ schemaId : String ,
39
+ requestParameters : RequestParameters = RequestParameters ()
40
+ ): FormSchema {
41
+ val endpoint = " /v3/orgs/$organisationId /data-capture/forms/$formId /schemas/$schemaId "
42
+
43
+ val response = performGet(endpoint, requestParameters.toMap())
44
+ val jsonContent = Json .parseToJsonElement(response.body<String >()).jsonObject
45
+
46
+ val dataObject = jsonContent[" data" ]?.jsonObject
47
+ ? : throw IllegalStateException (" Missing data object" )
48
+
49
+ return instantiateFormSchemaFromJson(dataObject)
50
+ }
51
+
52
+ private fun instantiateFormSchemaFromJson (json : JsonObject ): FormSchema {
53
+ val id = json[" id" ]?.jsonPrimitive?.content
54
+ ? : throw IllegalStateException (" Missing id" )
55
+
56
+ val attributes = json[" attributes" ]?.jsonObject ? : JsonObject (emptyMap())
57
+ val model = attributes[" model" ]?.jsonObjectOrNull()
58
+ val views = attributes[" views" ]?.jsonObjectOrNull()
59
+
60
+ return FormSchema (
61
+ id = id,
62
+ modelConfig = model,
63
+ viewsConfig = views,
64
+ modelConfigStr = model?.toString() ? : " " ,
65
+ viewsConfigStr = views?.toString() ? : " "
66
+ )
67
+ }
68
+
69
+ private fun JsonElement.jsonObjectOrNull (): JsonObject ? = this as ? JsonObject
27
70
}
0 commit comments