Split v1 creation to create/update
This commit is contained in:
@@ -12,10 +12,7 @@ import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1
|
||||
import gq.kirmanak.mealient.extensions.toFullRecipeInfo
|
||||
import gq.kirmanak.mealient.extensions.toRecipeSummaryInfo
|
||||
import gq.kirmanak.mealient.extensions.toV0Request
|
||||
import gq.kirmanak.mealient.extensions.toV1Request
|
||||
import gq.kirmanak.mealient.extensions.*
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -32,7 +29,10 @@ class MealieDataSourceWrapper @Inject constructor(
|
||||
): String = makeCall { token, url, version ->
|
||||
when (version) {
|
||||
ServerVersion.V0 -> v0Source.addRecipe(url, token, recipe.toV0Request())
|
||||
ServerVersion.V1 -> v1Source.addRecipe(url, token, recipe.toV1Request())
|
||||
ServerVersion.V1 -> {
|
||||
val slug = v1Source.createRecipe(url, token, recipe.toV1CreateRequest())
|
||||
v1Source.updateRecipe(url, token, slug, recipe.toV1UpdateRequest(slug))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ data class FullRecipeInfo(
|
||||
val remoteId: String,
|
||||
val name: String,
|
||||
val slug: String,
|
||||
val image: String,
|
||||
val image: String?,
|
||||
val description: String,
|
||||
val recipeCategories: List<String>,
|
||||
val tags: List<String>,
|
||||
|
||||
@@ -206,7 +206,12 @@ private fun AddRecipeInstructionInfo.toV0Instruction() = AddRecipeInstructionV0(
|
||||
text = text,
|
||||
)
|
||||
|
||||
fun AddRecipeInfo.toV1Request() = AddRecipeRequestV1(
|
||||
|
||||
fun AddRecipeInfo.toV1CreateRequest() = CreateRecipeRequestV1(
|
||||
name = name,
|
||||
)
|
||||
|
||||
fun AddRecipeInfo.toV1UpdateRequest(slug: String) = UpdateRecipeRequestV1(
|
||||
name = name,
|
||||
description = description,
|
||||
image = image,
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
package gq.kirmanak.mealient.datasource.v1
|
||||
|
||||
import gq.kirmanak.mealient.datasource.v1.models.AddRecipeRequestV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.*
|
||||
|
||||
interface MealieDataSourceV1 {
|
||||
|
||||
suspend fun addRecipe(
|
||||
suspend fun createRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
recipe: AddRecipeRequestV1,
|
||||
recipe: CreateRecipeRequestV1,
|
||||
): String
|
||||
|
||||
suspend fun updateRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
slug: String,
|
||||
recipe: UpdateRecipeRequestV1,
|
||||
): String
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,16 +19,27 @@ class MealieDataSourceV1Impl @Inject constructor(
|
||||
private val json: Json,
|
||||
) : MealieDataSourceV1 {
|
||||
|
||||
override suspend fun addRecipe(
|
||||
override suspend fun createRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
recipe: AddRecipeRequestV1
|
||||
recipe: CreateRecipeRequestV1
|
||||
): String = networkRequestWrapper.makeCallAndHandleUnauthorized(
|
||||
block = { service.addRecipe("$baseUrl/api/recipes/create", token, recipe) },
|
||||
logMethod = { "addRecipe" },
|
||||
block = { service.createRecipe("$baseUrl/api/recipes", token, recipe) },
|
||||
logMethod = { "createRecipe" },
|
||||
logParameters = { "baseUrl = $baseUrl, token = $token, recipe = $recipe" }
|
||||
)
|
||||
|
||||
override suspend fun updateRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
slug: String,
|
||||
recipe: UpdateRecipeRequestV1
|
||||
): String = networkRequestWrapper.makeCallAndHandleUnauthorized(
|
||||
block = { service.updateRecipe("$baseUrl/api/recipes/$slug", token, recipe) },
|
||||
logMethod = { "addRecipe" },
|
||||
logParameters = { "baseUrl = $baseUrl, token = $token, slug = $slug, recipe = $recipe" }
|
||||
)
|
||||
|
||||
override suspend fun authenticate(
|
||||
baseUrl: String,
|
||||
username: String,
|
||||
|
||||
@@ -15,10 +15,17 @@ interface MealieServiceV1 {
|
||||
): GetTokenResponseV1
|
||||
|
||||
@POST
|
||||
suspend fun addRecipe(
|
||||
suspend fun createRecipe(
|
||||
@Url url: String,
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
@Body addRecipeRequestV0: AddRecipeRequestV1,
|
||||
@Body addRecipeRequest: CreateRecipeRequestV1,
|
||||
): String
|
||||
|
||||
@PUT
|
||||
suspend fun updateRecipe(
|
||||
@Url url: String,
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
@Body addRecipeRequest: UpdateRecipeRequestV1,
|
||||
): String
|
||||
|
||||
@GET
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package gq.kirmanak.mealient.datasource.v1.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class CreateRecipeRequestV1(
|
||||
@SerialName("name") val name: String,
|
||||
)
|
||||
@@ -10,7 +10,7 @@ data class GetRecipeResponseV1(
|
||||
@SerialName("id") val remoteId: String,
|
||||
@SerialName("name") val name: String,
|
||||
@SerialName("slug") val slug: String,
|
||||
@SerialName("image") val image: String,
|
||||
@SerialName("image") val image: String? = null,
|
||||
@SerialName("description") val description: String = "",
|
||||
@SerialName("recipeCategory") val recipeCategories: List<String>,
|
||||
@SerialName("tags") val tags: List<String>,
|
||||
|
||||
@@ -4,7 +4,7 @@ import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeRequestV1(
|
||||
data class UpdateRecipeRequestV1(
|
||||
@SerialName("name") val name: String = "",
|
||||
@SerialName("description") val description: String = "",
|
||||
@SerialName("image") val image: String = "",
|
||||
Reference in New Issue
Block a user