Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • justJanne/meteroid
1 result
Select Git revision
Show changes
Commits on Source (7)
Showing
with 427 additions and 69 deletions
......@@ -22,10 +22,12 @@
* THE SOFTWARE.
*/
package de.chaosdorf.mete
package de.chaosdorf.mete.model
import kotlinx.serialization.Serializable
@Serializable
@JvmInline
value class BarcodeId(val value: Long)
value class BarcodeId(val value: Long) {
override fun toString() = value.toString()
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2013-2023 Chaosdorf e.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.chaosdorf.mete.model
interface BarcodeModel {
val barcodeId: BarcodeId
val drinkId: DrinkId
}
......@@ -22,10 +22,12 @@
* THE SOFTWARE.
*/
package de.chaosdorf.mete
package de.chaosdorf.mete.model
import kotlinx.serialization.Serializable
@Serializable
@JvmInline
value class DrinkId(val value: Long)
value class DrinkId(val value: Long) {
override fun toString() = value.toString()
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2013-2023 Chaosdorf e.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.chaosdorf.mete.model
import java.math.BigDecimal
interface DrinkModel {
val drinkId: DrinkId
val name: String
val active: Boolean
val volume: BigDecimal
val caffeine: Int?
val price: BigDecimal
val logoUrl: String?
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2013-2023 Chaosdorf e.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.chaosdorf.mete.model
import java.math.BigDecimal
interface MeteApi {
suspend fun getManifest(): PwaManifest?
suspend fun listTransactions(user: UserId? = null): TransactionSummaryModel
suspend fun listBarcodes(): List<BarcodeModel>
suspend fun getBarcode(id: BarcodeId): BarcodeModel?
suspend fun listDrinks(): List<DrinkModel>
suspend fun getDrink(id: DrinkId): DrinkModel?
suspend fun listUsers(): List<UserModel>
suspend fun getUser(id: UserId): UserModel?
suspend fun deposit(id: UserId, amount: BigDecimal)
suspend fun withdraw(id: UserId, amount: BigDecimal)
suspend fun purchase(id: UserId, drink: DrinkId)
suspend fun purchase(id: UserId, barcode: BarcodeId)
}
......@@ -22,8 +22,8 @@
* THE SOFTWARE.
*/
package de.chaosdorf.mete.v1
package de.chaosdorf.mete.model
interface MeteApiFactory<T> {
fun newInstance(baseUrl: String): T
interface MeteApiFactory {
fun newInstance(baseUrl: String): MeteApi
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2013-2023 Chaosdorf e.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.chaosdorf.mete.model
import java.math.BigDecimal
interface TransactionSummaryModel {
val payments: BigDecimal
val deposits: BigDecimal
val total: BigDecimal
val entries: List<TransactionModel>
}
......@@ -22,7 +22,7 @@
* THE SOFTWARE.
*/
package de.chaosdorf.mete
package de.chaosdorf.mete.model
import kotlinx.serialization.Serializable
......
......@@ -22,7 +22,7 @@
* THE SOFTWARE.
*/
package de.chaosdorf.mete
package de.chaosdorf.mete.model
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
......
......@@ -22,10 +22,12 @@
* THE SOFTWARE.
*/
package de.chaosdorf.mete
package de.chaosdorf.mete.model
import kotlinx.serialization.Serializable
@Serializable
@JvmInline
value class AuditEntryId(val value: Long)
value class TransactionId(val value: Long) {
override fun toString() = value.toString()
}
......@@ -22,19 +22,14 @@
* THE SOFTWARE.
*/
package de.chaosdorf.mete.v1
package de.chaosdorf.mete.model
import de.chaosdorf.mete.AuditEntryId
import de.chaosdorf.mete.DrinkId
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.math.BigDecimal
@Serializable
data class AuditEntryModelV1(
val id: AuditEntryId,
@SerialName("created_at")
val createdAt: Instant,
val difference: Double,
val drink: DrinkId?
)
interface TransactionModel {
val transactionId: TransactionId
val difference: BigDecimal
val drinkId: DrinkId?
val timestamp: Instant
}
......@@ -22,10 +22,12 @@
* THE SOFTWARE.
*/
package de.chaosdorf.mete
package de.chaosdorf.mete.model
import kotlinx.serialization.Serializable
@Serializable
@JvmInline
value class UserId(val value: Long)
value class UserId(val value: Long) {
override fun toString() = value.toString()
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2013-2023 Chaosdorf e.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.chaosdorf.mete.model
import java.math.BigDecimal
interface UserModel {
val userId: UserId
val active: Boolean
val name: String
val email: String?
val balance: BigDecimal
val audit: Boolean
val redirect: Boolean
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2013-2023 Chaosdorf e.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.chaosdorf.mete.util
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.math.BigDecimal
object BigDecimalSerializer : KSerializer<BigDecimal> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("BigDecimal", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: BigDecimal) =
encoder.encodeString(value.toPlainString())
override fun deserialize(decoder: Decoder): BigDecimal =
BigDecimal(decoder.decodeString())
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2013-2023 Chaosdorf e.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.chaosdorf.mete.util
import okhttp3.Interceptor
import okhttp3.Response
object Code204Interceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response = chain.proceed(request)
return if (response.code == 204) {
response.newBuilder().code(200).build()
} else {
response
}
}
}
......@@ -24,12 +24,16 @@
package de.chaosdorf.mete.v1
import de.chaosdorf.mete.BarcodeId
import de.chaosdorf.mete.DrinkId
import de.chaosdorf.mete.model.BarcodeId
import de.chaosdorf.mete.model.BarcodeModel
import de.chaosdorf.mete.model.DrinkId
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class BarcodeModelV1(
val id: BarcodeId,
val drink: DrinkId,
)
internal data class BarcodeModelV1(
@SerialName("id")
override val barcodeId: BarcodeId,
@SerialName("drink")
override val drinkId: DrinkId,
) : BarcodeModel
......@@ -24,32 +24,42 @@
package de.chaosdorf.mete.v1
import de.chaosdorf.mete.DrinkId
import de.chaosdorf.mete.model.DrinkId
import de.chaosdorf.mete.model.DrinkModel
import de.chaosdorf.mete.util.BigDecimalSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.math.BigDecimal
@Serializable
data class DrinkModelV1(
val id: DrinkId,
val name: String,
internal data class DrinkModelV1(
@SerialName("id")
override val drinkId: DrinkId,
@SerialName("name")
override val name: String,
@SerialName("active")
override val active: Boolean,
@SerialName("bottle_size")
val bottleSize: Double,
val caffeine: Int?,
val price: Double,
@Serializable(with = BigDecimalSerializer::class)
override val volume: BigDecimal,
@SerialName("caffeine")
override val caffeine: Int?,
@SerialName("price")
@Serializable(with = BigDecimalSerializer::class)
override val price: BigDecimal,
@SerialName("logo_file_name")
val logoFileName: String,
val logoFileName: String?,
@SerialName("created_at")
val createdAt: Instant,
@SerialName("updated_at")
val updatedAt: Instant,
@SerialName("logo_content_type")
val logoContentType: String,
val logoContentType: String?,
@SerialName("logo_file_size")
val logoFileSize: Long,
val logoFileSize: Long?,
@SerialName("logo_updated_at")
val logoUpdatedAt: Instant,
val logoUpdatedAt: Instant?,
@SerialName("logo_url")
val logoUrl: String,
val active: Boolean
)
override val logoUrl: String?,
) : DrinkModel
......@@ -24,52 +24,71 @@
package de.chaosdorf.mete.v1
import de.chaosdorf.mete.DrinkId
import de.chaosdorf.mete.PwaManifest
import de.chaosdorf.mete.UserId
import de.chaosdorf.mete.model.BarcodeId
import de.chaosdorf.mete.model.DrinkId
import de.chaosdorf.mete.model.MeteApi
import de.chaosdorf.mete.model.PwaManifest
import de.chaosdorf.mete.model.UserId
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
import java.math.BigDecimal
interface MeteApiV1 {
internal interface MeteApiV1 : MeteApi {
@GET("manifest.json")
suspend fun getManifest(): PwaManifest?
override suspend fun getManifest(): PwaManifest?
@GET("api/v1/audits.json")
suspend fun getAudits(
@Query("user") user: Long? = null
): AuditResponseV1
override suspend fun listTransactions(
@Query("user") user: UserId?
): TransactionSummaryModelV1
@GET("api/v1/barcodes.json")
suspend fun listBarcodes(): List<BarcodeModelV1>
override suspend fun listBarcodes(): List<BarcodeModelV1>
@GET("api/v1/barcodes/{id}.json")
suspend fun getBarcode(): BarcodeModelV1?
override suspend fun getBarcode(
@Path("id") id: BarcodeId
): BarcodeModelV1?
@GET("api/v1/drinks.json")
suspend fun listDrinks(): List<DrinkModelV1>
override suspend fun listDrinks(): List<DrinkModelV1>
@GET("api/v1/drinks/{id}.json")
suspend fun getDrink(@Path("id") id: DrinkId): DrinkModelV1?
override suspend fun getDrink(
@Path("id") id: DrinkId
): DrinkModelV1?
@GET("api/v1/users.json")
suspend fun listUsers(): List<UserModelV1>
override suspend fun listUsers(): List<UserModelV1>
@GET("api/v1/users/{id}.json")
suspend fun getUser(@Path("id") id: UserId): UserModelV1?
override suspend fun getUser(
@Path("id") id: UserId
): UserModelV1?
@GET("api/v1/users/{id}/deposit.json")
suspend fun deposit(@Path("id") id: UserId)
override suspend fun deposit(
@Path("id") id: UserId,
@Query("amount") amount: BigDecimal
)
@GET("api/v1/users/{id}/payment.json")
suspend fun payment(@Path("id") id: UserId)
override suspend fun withdraw(
@Path("id") id: UserId,
@Query("amount") amount: BigDecimal
)
@GET("api/v1/users/{id}/buy.json")
suspend fun buy(@Path("id") id: UserId)
override suspend fun purchase(
@Path("id") id: UserId,
@Query("drink") drink: DrinkId
)
@GET("api/v1/users/{id}/buy_barcode.json")
suspend fun buyWithBarcode(@Path("id") id: UserId)
@GET("api/v1/users/stats.json")
suspend fun getStats()
override suspend fun purchase(
@Path("id") id: UserId,
@Query("barcode") barcode: BarcodeId
)
}
......@@ -25,19 +25,28 @@
package de.chaosdorf.mete.v1
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import de.chaosdorf.mete.model.MeteApi
import de.chaosdorf.mete.model.MeteApiFactory
import de.chaosdorf.mete.util.Code204Interceptor
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.create
object MeteApiV1Factory : MeteApiFactory<MeteApiV1> {
object MeteApiV1Factory : MeteApiFactory {
private val json = Json {
ignoreUnknownKeys = true
}
override fun newInstance(baseUrl: String): MeteApiV1 {
private val httpClient = OkHttpClient.Builder()
.addInterceptor(Code204Interceptor)
.build()
override fun newInstance(baseUrl: String): MeteApi {
val contentType = "application/json".toMediaType()
val retrofit = Retrofit.Builder()
.client(httpClient)
.baseUrl(baseUrl)
.addConverterFactory(json.asConverterFactory(contentType))
.build()
......
/*
* The MIT License (MIT)
*
* Copyright (c) 2013-2023 Chaosdorf e.V.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.chaosdorf.mete.v1
import de.chaosdorf.mete.model.DrinkId
import de.chaosdorf.mete.model.TransactionId
import de.chaosdorf.mete.model.TransactionModel
import de.chaosdorf.mete.util.BigDecimalSerializer
import kotlinx.datetime.Instant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.math.BigDecimal
@Serializable
internal data class TransactionModelV1(
@SerialName("id")
override val transactionId: TransactionId,
@SerialName("difference")
@Serializable(with = BigDecimalSerializer::class)
override val difference: BigDecimal,
@SerialName("drink")
override val drinkId: DrinkId?,
@SerialName("created_at")
override val timestamp: Instant
) : TransactionModel