Skip to content

Commit 4c7a45d

Browse files
committed
fix: allow assignee of vehicles to be returned
1 parent 25b7232 commit 4c7a45d

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.ctrlhub.core.api
2+
3+
/**
4+
* An interface that represents any entity that can be assigned
5+
* to something such as a vehicle or equipment
6+
*/
7+
interface Assignable

src/main/kotlin/com/ctrlhub/core/assets/vehicles/VehiclesRouter.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.ctrlhub.core.assets.vehicles
33
import com.ctrlhub.core.Api
44
import com.ctrlhub.core.assets.vehicles.VehicleRequestParameters
55
import com.ctrlhub.core.assets.vehicles.response.Vehicle
6+
import com.ctrlhub.core.iam.response.User
67
import com.ctrlhub.core.router.Router
78
import com.ctrlhub.core.router.request.FilterOption
89
import com.ctrlhub.core.router.request.JsonApiIncludes
@@ -51,15 +52,23 @@ class VehiclesRouter(httpClient: HttpClient) : Router(httpClient) {
5152
organisationId: String,
5253
requestParameters: VehicleRequestParameters = VehicleRequestParameters()
5354
): List<Vehicle> {
54-
return fetchJsonApiResources("/v3/orgs/$organisationId/assets/vehicles", requestParameters.toMap())
55+
return fetchJsonApiResources(
56+
"/v3/orgs/$organisationId/assets/vehicles",
57+
requestParameters.toMap(),
58+
User::class.java
59+
)
5560
}
5661

5762
suspend fun one(
5863
organisationId: String,
5964
vehicleId: String,
6065
requestParameters: VehicleRequestParameters = VehicleRequestParameters()
6166
): Vehicle {
62-
return fetchJsonApiResource("/v3/orgs/$organisationId/assets/vehicles/$vehicleId", requestParameters.toMap())
67+
return fetchJsonApiResource(
68+
"/v3/orgs/$organisationId/assets/vehicles/$vehicleId",
69+
requestParameters.toMap(),
70+
User::class.java
71+
)
6372
}
6473
}
6574

src/main/kotlin/com/ctrlhub/core/assets/vehicles/response/VehiclesResponse.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.ctrlhub.core.assets.vehicles.response
22

3+
import com.ctrlhub.core.api.Assignable
4+
import com.ctrlhub.core.iam.response.User
35
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
46
import com.fasterxml.jackson.annotation.JsonProperty
57
import com.github.jasminb.jsonapi.StringIdHandler
@@ -19,6 +21,9 @@ class Vehicle {
1921

2022
@Relationship("specification")
2123
var specification: VehicleSpecification? = null
24+
25+
@Relationship("assignee")
26+
var assignee: Assignable? = null
2227
}
2328

2429
@Type("vehicle-categories")

src/main/kotlin/com/ctrlhub/core/iam/response/WhoamiResponse.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.ctrlhub.core.iam.response
22

3+
import com.ctrlhub.core.api.Assignable
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
45
import com.fasterxml.jackson.annotation.JsonProperty
56
import com.github.jasminb.jsonapi.StringIdHandler
67
import com.github.jasminb.jsonapi.annotations.Id
78
import com.github.jasminb.jsonapi.annotations.Type
89

910
@Type("users")
10-
class User {
11+
class User : Assignable {
1112
@Id(StringIdHandler::class)
1213
lateinit var id: String
1314
lateinit var email: String

src/main/kotlin/com/ctrlhub/core/router/Router.kt

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,40 +46,54 @@ abstract class Router(val httpClient: HttpClient) {
4646
}
4747
}
4848

49-
protected suspend inline fun <reified T> fetchJsonApiResource(endpoint: String, queryParameters: Map<String, String> = emptyMap()): T {
49+
protected suspend inline fun <reified T> fetchJsonApiResource(
50+
endpoint: String,
51+
queryParameters: Map<String, String> = emptyMap(),
52+
vararg includedClasses: Class<*>
53+
): T {
5054
return try {
5155
val rawResponse = performGet(endpoint, queryParameters)
52-
val resourceConverter = ResourceConverter(getObjectMapper(), T::class.java).apply {
56+
57+
val resourceConverter = ResourceConverter(getObjectMapper(), T::class.java, *includedClasses).apply {
5358
enableSerializationOption(SerializationFeature.INCLUDE_RELATIONSHIP_ATTRIBUTES)
5459
}
55-
val jsonApiResponse = resourceConverter.readDocument<T>(rawResponse.body<ByteArray>(), T::class.java)
60+
61+
val jsonApiResponse = resourceConverter.readDocument<T>(
62+
rawResponse.body<ByteArray>(), T::class.java
63+
)
5664

5765
jsonApiResponse.get() ?: throw ApiException("Failed to parse response for $endpoint", Exception())
5866
} catch (e: ClientRequestException) {
5967
if (e.response.status == HttpStatusCode.Unauthorized) {
60-
throw UnauthorizedException("Unauthorised action: $endpoint", e.response, e)
68+
throw UnauthorizedException("Unauthorized action: $endpoint", e.response, e)
6169
}
62-
6370
throw ApiClientException("Request failed: $endpoint", e.response, e)
6471
} catch (e: Exception) {
6572
throw ApiException("Request failed: $endpoint", e)
6673
}
6774
}
6875

69-
protected suspend inline fun <reified T> fetchJsonApiResources(endpoint: String, queryParameters: Map<String, String> = emptyMap()): List<T> {
76+
protected suspend inline fun <reified T> fetchJsonApiResources(
77+
endpoint: String,
78+
queryParameters: Map<String, String> = emptyMap(),
79+
vararg includedClasses: Class<*>
80+
): List<T> {
7081
return try {
7182
val rawResponse = performGet(endpoint, queryParameters)
72-
val resourceConverter = ResourceConverter(getObjectMapper(), T::class.java).apply {
83+
84+
val resourceConverter = ResourceConverter(getObjectMapper(), T::class.java, *includedClasses).apply {
7385
enableSerializationOption(SerializationFeature.INCLUDE_RELATIONSHIP_ATTRIBUTES)
7486
}
75-
val jsonApiResponse = resourceConverter.readDocumentCollection<T>(rawResponse.body<ByteArray>(), T::class.java)
87+
88+
val jsonApiResponse = resourceConverter.readDocumentCollection<T>(
89+
rawResponse.body<ByteArray>(), T::class.java
90+
)
7691

7792
jsonApiResponse.get() ?: throw ApiException("Failed to parse response for $endpoint", Exception())
7893
} catch (e: ClientRequestException) {
7994
if (e.response.status == HttpStatusCode.Unauthorized) {
80-
throw UnauthorizedException("Unauthorised action: $endpoint", e.response, e)
95+
throw UnauthorizedException("Unauthorized action: $endpoint", e.response, e)
8196
}
82-
8397
throw ApiClientException("Request failed: $endpoint", e.response, e)
8498
} catch (e: Exception) {
8599
throw ApiException("Request failed: $endpoint", e)

0 commit comments

Comments
 (0)