Move V0 responses to v0 package
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package gq.kirmanak.mealient.data.add
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
|
||||
interface AddRecipeDataSource {
|
||||
suspend fun addRecipe(recipe: AddRecipeRequest): String
|
||||
suspend fun addRecipe(recipe: AddRecipeRequestV0): String
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
package gq.kirmanak.mealient.data.add
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface AddRecipeRepo {
|
||||
|
||||
val addRecipeRequestFlow: Flow<AddRecipeRequest>
|
||||
val addRecipeRequestFlow: Flow<AddRecipeRequestV0>
|
||||
|
||||
suspend fun preserve(recipe: AddRecipeRequest)
|
||||
suspend fun preserve(recipe: AddRecipeRequestV0)
|
||||
|
||||
suspend fun clear()
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package gq.kirmanak.mealient.data.add.impl
|
||||
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorage
|
||||
import gq.kirmanak.mealient.extensions.toAddRecipeRequest
|
||||
import gq.kirmanak.mealient.extensions.toDraft
|
||||
@@ -20,10 +20,10 @@ class AddRecipeRepoImpl @Inject constructor(
|
||||
private val logger: Logger,
|
||||
) : AddRecipeRepo {
|
||||
|
||||
override val addRecipeRequestFlow: Flow<AddRecipeRequest>
|
||||
override val addRecipeRequestFlow: Flow<AddRecipeRequestV0>
|
||||
get() = addRecipeStorage.updates.map { it.toAddRecipeRequest() }
|
||||
|
||||
override suspend fun preserve(recipe: AddRecipeRequest) {
|
||||
override suspend fun preserve(recipe: AddRecipeRequestV0) {
|
||||
logger.v { "preserveRecipe() called with: recipe = $recipe" }
|
||||
addRecipeStorage.save(recipe.toDraft())
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package gq.kirmanak.mealient.data.auth.impl
|
||||
|
||||
import gq.kirmanak.mealient.data.auth.AuthDataSource
|
||||
import gq.kirmanak.mealient.datasource.MealieDataSource
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class AuthDataSourceImpl @Inject constructor(
|
||||
private val mealieDataSource: MealieDataSource,
|
||||
private val V0Source: MealieDataSourceV0,
|
||||
) : AuthDataSource {
|
||||
|
||||
override suspend fun authenticate(username: String, password: String, baseUrl: String): String =
|
||||
mealieDataSource.authenticate(baseUrl, username, password)
|
||||
V0Source.authenticate(baseUrl, username, password)
|
||||
}
|
||||
@@ -8,9 +8,9 @@ import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
||||
import gq.kirmanak.mealient.data.recipes.network.FullRecipeInfo
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeSummaryInfo
|
||||
import gq.kirmanak.mealient.datasource.MealieDataSource
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1
|
||||
import gq.kirmanak.mealient.extensions.runCatchingExceptCancel
|
||||
import gq.kirmanak.mealient.extensions.toFullRecipeInfo
|
||||
@@ -23,19 +23,19 @@ import javax.inject.Singleton
|
||||
class MealieDataSourceWrapper @Inject constructor(
|
||||
private val baseURLStorage: BaseURLStorage,
|
||||
private val authRepo: AuthRepo,
|
||||
private val source: MealieDataSource,
|
||||
private val v1Source: MealieDataSourceV1,
|
||||
private val V0source: MealieDataSourceV0,
|
||||
private val V1Source: MealieDataSourceV1,
|
||||
) : AddRecipeDataSource, RecipeDataSource, VersionDataSource {
|
||||
|
||||
override suspend fun addRecipe(recipe: AddRecipeRequest): String =
|
||||
withAuthHeader { token -> source.addRecipe(getUrl(), token, recipe) }
|
||||
override suspend fun addRecipe(recipe: AddRecipeRequestV0): String =
|
||||
withAuthHeader { token -> V0source.addRecipe(getUrl(), token, recipe) }
|
||||
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionInfo =
|
||||
runCatchingExceptCancel {
|
||||
source.getVersionInfo(baseUrl).toVersionInfo()
|
||||
V0source.getVersionInfo(baseUrl).toVersionInfo()
|
||||
}.getOrElse {
|
||||
if (it is NetworkError.NotMealie) {
|
||||
v1Source.getVersionInfo(baseUrl).toVersionInfo()
|
||||
V1Source.getVersionInfo(baseUrl).toVersionInfo()
|
||||
} else {
|
||||
throw it
|
||||
}
|
||||
@@ -45,9 +45,9 @@ class MealieDataSourceWrapper @Inject constructor(
|
||||
withAuthHeader { token ->
|
||||
val url = getUrl()
|
||||
if (isV1()) {
|
||||
v1Source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
||||
V1Source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
||||
} else {
|
||||
source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
||||
V0source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,9 +55,9 @@ class MealieDataSourceWrapper @Inject constructor(
|
||||
withAuthHeader { token ->
|
||||
val url = getUrl()
|
||||
if (isV1()) {
|
||||
v1Source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
||||
V1Source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
||||
} else {
|
||||
source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
||||
V0source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import gq.kirmanak.mealient.database.recipe.entity.RecipeEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeIngredientEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeInstructionEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealient.datasource.models.*
|
||||
import gq.kirmanak.mealient.datasource.v0.models.*
|
||||
import gq.kirmanak.mealient.datasource.v1.models.*
|
||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft
|
||||
|
||||
@@ -34,7 +34,7 @@ fun RecipeInstructionInfo.toRecipeInstructionEntity(remoteId: String) = RecipeIn
|
||||
text = text
|
||||
)
|
||||
|
||||
fun GetRecipeSummaryResponse.toRecipeSummaryInfo() = RecipeSummaryInfo(
|
||||
fun GetRecipeSummaryResponseV0.toRecipeSummaryInfo() = RecipeSummaryInfo(
|
||||
remoteId = remoteId.toString(),
|
||||
name = name,
|
||||
slug = slug,
|
||||
@@ -74,23 +74,23 @@ fun RecipeSummaryInfo.recipeEntity() = RecipeSummaryEntity(
|
||||
imageId = imageId,
|
||||
)
|
||||
|
||||
fun VersionResponse.toVersionInfo() = VersionInfo(version)
|
||||
fun VersionResponseV0.toVersionInfo() = VersionInfo(version)
|
||||
|
||||
fun VersionResponseV1.toVersionInfo() = VersionInfo(version)
|
||||
|
||||
fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequest(
|
||||
fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequestV0(
|
||||
name = recipeName,
|
||||
description = recipeDescription,
|
||||
recipeYield = recipeYield,
|
||||
recipeIngredient = recipeIngredients.map { AddRecipeIngredient(note = it) },
|
||||
recipeInstructions = recipeInstructions.map { AddRecipeInstruction(text = it) },
|
||||
settings = AddRecipeSettings(
|
||||
recipeIngredient = recipeIngredients.map { AddRecipeIngredientV0(note = it) },
|
||||
recipeInstructions = recipeInstructions.map { AddRecipeInstructionV0(text = it) },
|
||||
settings = AddRecipeSettingsV0(
|
||||
public = isRecipePublic,
|
||||
disableComments = areCommentsDisabled,
|
||||
)
|
||||
)
|
||||
|
||||
fun AddRecipeRequest.toDraft(): AddRecipeDraft = AddRecipeDraft(
|
||||
fun AddRecipeRequestV0.toDraft(): AddRecipeDraft = AddRecipeDraft(
|
||||
recipeName = name,
|
||||
recipeDescription = description,
|
||||
recipeYield = recipeYield,
|
||||
@@ -100,7 +100,7 @@ fun AddRecipeRequest.toDraft(): AddRecipeDraft = AddRecipeDraft(
|
||||
areCommentsDisabled = settings.disableComments,
|
||||
)
|
||||
|
||||
fun GetRecipeResponse.toFullRecipeInfo() = FullRecipeInfo(
|
||||
fun GetRecipeResponseV0.toFullRecipeInfo() = FullRecipeInfo(
|
||||
remoteId = remoteId.toString(),
|
||||
name = name,
|
||||
slug = slug,
|
||||
@@ -116,7 +116,7 @@ fun GetRecipeResponse.toFullRecipeInfo() = FullRecipeInfo(
|
||||
recipeInstructions = recipeInstructions.map { it.toRecipeInstructionInfo() }
|
||||
)
|
||||
|
||||
fun GetRecipeIngredientResponse.toRecipeIngredientInfo() = RecipeIngredientInfo(
|
||||
fun GetRecipeIngredientResponseV0.toRecipeIngredientInfo() = RecipeIngredientInfo(
|
||||
title = title,
|
||||
note = note,
|
||||
unit = unit,
|
||||
@@ -125,7 +125,7 @@ fun GetRecipeIngredientResponse.toRecipeIngredientInfo() = RecipeIngredientInfo(
|
||||
quantity = quantity
|
||||
)
|
||||
|
||||
fun GetRecipeInstructionResponse.toRecipeInstructionInfo() = RecipeInstructionInfo(
|
||||
fun GetRecipeInstructionResponseV0.toRecipeInstructionInfo() = RecipeInstructionInfo(
|
||||
title = title,
|
||||
text = text
|
||||
)
|
||||
|
||||
@@ -14,10 +14,10 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealient.R
|
||||
import gq.kirmanak.mealient.databinding.FragmentAddRecipeBinding
|
||||
import gq.kirmanak.mealient.databinding.ViewSingleInputBinding
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeIngredient
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeInstruction
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeSettings
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeIngredientV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeInstructionV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeSettingsV0
|
||||
import gq.kirmanak.mealient.extensions.checkIfInputIsEmpty
|
||||
import gq.kirmanak.mealient.extensions.collectWhenViewResumed
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
@@ -122,14 +122,14 @@ class AddRecipeFragment : Fragment(R.layout.fragment_add_recipe) {
|
||||
|
||||
private fun saveValues() = with(binding) {
|
||||
logger.v { "saveValues() called" }
|
||||
val instructions = parseInputRows(instructionsFlow).map { AddRecipeInstruction(text = it) }
|
||||
val ingredients = parseInputRows(ingredientsFlow).map { AddRecipeIngredient(note = it) }
|
||||
val settings = AddRecipeSettings(
|
||||
val instructions = parseInputRows(instructionsFlow).map { AddRecipeInstructionV0(text = it) }
|
||||
val ingredients = parseInputRows(ingredientsFlow).map { AddRecipeIngredientV0(note = it) }
|
||||
val settings = AddRecipeSettingsV0(
|
||||
public = publicRecipe.isChecked,
|
||||
disableComments = disableComments.isChecked,
|
||||
)
|
||||
viewModel.preserve(
|
||||
AddRecipeRequest(
|
||||
AddRecipeRequestV0(
|
||||
name = recipeNameInput.text.toString(),
|
||||
description = recipeDescriptionInput.text.toString(),
|
||||
recipeYield = recipeYieldInput.text.toString(),
|
||||
@@ -148,7 +148,7 @@ class AddRecipeFragment : Fragment(R.layout.fragment_add_recipe) {
|
||||
.filterNot { it.isBlank() }
|
||||
.toList()
|
||||
|
||||
private fun onSavedInputLoaded(request: AddRecipeRequest) = with(binding) {
|
||||
private fun onSavedInputLoaded(request: AddRecipeRequestV0) = with(binding) {
|
||||
logger.v { "onSavedInputLoaded() called with: request = $request" }
|
||||
recipeNameInput.setText(request.name)
|
||||
recipeDescriptionInput.setText(request.description)
|
||||
|
||||
@@ -4,7 +4,7 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.extensions.runCatchingExceptCancel
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
@@ -23,8 +23,8 @@ class AddRecipeViewModel @Inject constructor(
|
||||
private val _addRecipeResultChannel = Channel<Boolean>(Channel.UNLIMITED)
|
||||
val addRecipeResult: Flow<Boolean> get() = _addRecipeResultChannel.receiveAsFlow()
|
||||
|
||||
private val _preservedAddRecipeRequestChannel = Channel<AddRecipeRequest>(Channel.UNLIMITED)
|
||||
val preservedAddRecipeRequest: Flow<AddRecipeRequest>
|
||||
private val _preservedAddRecipeRequestChannel = Channel<AddRecipeRequestV0>(Channel.UNLIMITED)
|
||||
val preservedAddRecipeRequest: Flow<AddRecipeRequestV0>
|
||||
get() = _preservedAddRecipeRequestChannel.receiveAsFlow()
|
||||
|
||||
fun loadPreservedRequest() {
|
||||
@@ -47,7 +47,7 @@ class AddRecipeViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun preserve(request: AddRecipeRequest) {
|
||||
fun preserve(request: AddRecipeRequestV0) {
|
||||
logger.v { "preserve() called with: request = $request" }
|
||||
viewModelScope.launch { addRecipeRepo.preserve(request) }
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import by.kirich1409.viewbindingdelegate.viewBinding
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealient.R
|
||||
import gq.kirmanak.mealient.databinding.FragmentAuthenticationBinding
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.extensions.checkIfInputIsEmpty
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import gq.kirmanak.mealient.ui.OperationUiState
|
||||
|
||||
@@ -10,7 +10,7 @@ import by.kirich1409.viewbindingdelegate.viewBinding
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealient.R
|
||||
import gq.kirmanak.mealient.databinding.FragmentBaseUrlBinding
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.extensions.checkIfInputIsEmpty
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import gq.kirmanak.mealient.ui.OperationUiState
|
||||
|
||||
@@ -2,8 +2,8 @@ package gq.kirmanak.mealient.data.network
|
||||
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
||||
import gq.kirmanak.mealient.datasource.MealieDataSource
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
|
||||
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_AUTH_HEADER
|
||||
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_BASE_URL
|
||||
import gq.kirmanak.mealient.test.RecipeImplTestData.GET_CAKE_RESPONSE
|
||||
@@ -18,7 +18,7 @@ import org.junit.Test
|
||||
import java.io.IOException
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class MealieDataSourceWrapperTest {
|
||||
class MealieDataSourceV0WrapperTest {
|
||||
|
||||
@MockK
|
||||
lateinit var baseURLStorage: BaseURLStorage
|
||||
@@ -27,14 +27,14 @@ class MealieDataSourceWrapperTest {
|
||||
lateinit var authRepo: AuthRepo
|
||||
|
||||
@MockK
|
||||
lateinit var mealieDataSource: MealieDataSource
|
||||
lateinit var mealieDataSourceV0: MealieDataSourceV0
|
||||
|
||||
lateinit var subject: MealieDataSourceWrapper
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
subject = MealieDataSourceWrapper(baseURLStorage, authRepo, mealieDataSource)
|
||||
subject = MealieDataSourceWrapper(baseURLStorage, authRepo, mealieDataSourceV0)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -42,10 +42,10 @@ class MealieDataSourceWrapperTest {
|
||||
coEvery { baseURLStorage.requireBaseURL() } returns TEST_BASE_URL
|
||||
coEvery { authRepo.getAuthHeader() } returns null andThen TEST_AUTH_HEADER
|
||||
coEvery {
|
||||
mealieDataSource.requestRecipeInfo(eq(TEST_BASE_URL), isNull(), eq("cake"))
|
||||
mealieDataSourceV0.requestRecipeInfo(eq(TEST_BASE_URL), isNull(), eq("cake"))
|
||||
} throws NetworkError.Unauthorized(IOException())
|
||||
coEvery {
|
||||
mealieDataSource.requestRecipeInfo(eq(TEST_BASE_URL), eq(TEST_AUTH_HEADER), eq("cake"))
|
||||
mealieDataSourceV0.requestRecipeInfo(eq(TEST_BASE_URL), eq(TEST_AUTH_HEADER), eq("cake"))
|
||||
} returns GET_CAKE_RESPONSE
|
||||
subject.requestRecipeInfo("cake")
|
||||
coVerifyAll {
|
||||
@@ -6,7 +6,7 @@ import com.google.common.truth.Truth.assertThat
|
||||
import gq.kirmanak.mealient.data.recipes.db.RecipeStorage
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError.Unauthorized
|
||||
import gq.kirmanak.mealient.datasource.NetworkError.Unauthorized
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import gq.kirmanak.mealient.test.RecipeImplTestData.TEST_RECIPE_SUMMARIES
|
||||
import io.mockk.MockKAnnotations
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package gq.kirmanak.mealient.extensions
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeIngredient
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeInstruction
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeSettings
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeIngredientV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeInstructionV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeSettingsV0
|
||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft
|
||||
import org.junit.Test
|
||||
|
||||
@@ -22,19 +22,19 @@ class RemoteToLocalMappingsTest {
|
||||
areCommentsDisabled = true,
|
||||
)
|
||||
|
||||
val expected = AddRecipeRequest(
|
||||
val expected = AddRecipeRequestV0(
|
||||
name = "Recipe name",
|
||||
description = "Recipe description",
|
||||
recipeYield = "Recipe yield",
|
||||
recipeIngredient = listOf(
|
||||
AddRecipeIngredient(note = "Recipe ingredient 1"),
|
||||
AddRecipeIngredient(note = "Recipe ingredient 2")
|
||||
AddRecipeIngredientV0(note = "Recipe ingredient 1"),
|
||||
AddRecipeIngredientV0(note = "Recipe ingredient 2")
|
||||
),
|
||||
recipeInstructions = listOf(
|
||||
AddRecipeInstruction(text = "Recipe instruction 1"),
|
||||
AddRecipeInstruction(text = "Recipe instruction 2")
|
||||
AddRecipeInstructionV0(text = "Recipe instruction 1"),
|
||||
AddRecipeInstructionV0(text = "Recipe instruction 2")
|
||||
),
|
||||
settings = AddRecipeSettings(
|
||||
settings = AddRecipeSettingsV0(
|
||||
public = false,
|
||||
disableComments = true,
|
||||
)
|
||||
@@ -45,19 +45,19 @@ class RemoteToLocalMappingsTest {
|
||||
|
||||
@Test
|
||||
fun `when toDraft then fills fields correctly`() {
|
||||
val request = AddRecipeRequest(
|
||||
val request = AddRecipeRequestV0(
|
||||
name = "Recipe name",
|
||||
description = "Recipe description",
|
||||
recipeYield = "Recipe yield",
|
||||
recipeIngredient = listOf(
|
||||
AddRecipeIngredient(note = "Recipe ingredient 1"),
|
||||
AddRecipeIngredient(note = "Recipe ingredient 2")
|
||||
AddRecipeIngredientV0(note = "Recipe ingredient 1"),
|
||||
AddRecipeIngredientV0(note = "Recipe ingredient 2")
|
||||
),
|
||||
recipeInstructions = listOf(
|
||||
AddRecipeInstruction(text = "Recipe instruction 1"),
|
||||
AddRecipeInstruction(text = "Recipe instruction 2")
|
||||
AddRecipeInstructionV0(text = "Recipe instruction 1"),
|
||||
AddRecipeInstructionV0(text = "Recipe instruction 2")
|
||||
),
|
||||
settings = AddRecipeSettings(
|
||||
settings = AddRecipeSettingsV0(
|
||||
public = false,
|
||||
disableComments = true,
|
||||
)
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package gq.kirmanak.mealient.test
|
||||
|
||||
import gq.kirmanak.mealient.database.recipe.entity.*
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeIngredientResponse
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeInstructionResponse
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeSummaryResponse
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetRecipeIngredientResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetRecipeInstructionResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetRecipeResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetRecipeSummaryResponseV0
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
|
||||
object RecipeImplTestData {
|
||||
val RECIPE_SUMMARY_CAKE = GetRecipeSummaryResponse(
|
||||
val RECIPE_SUMMARY_CAKE = GetRecipeSummaryResponseV0(
|
||||
remoteId = 1,
|
||||
name = "Cake",
|
||||
slug = "cake",
|
||||
@@ -22,7 +22,7 @@ object RecipeImplTestData {
|
||||
dateUpdated = LocalDateTime.parse("2021-11-13T15:30:13"),
|
||||
)
|
||||
|
||||
val RECIPE_SUMMARY_PORRIDGE = GetRecipeSummaryResponse(
|
||||
val RECIPE_SUMMARY_PORRIDGE = GetRecipeSummaryResponseV0(
|
||||
remoteId = 2,
|
||||
name = "Porridge",
|
||||
slug = "porridge",
|
||||
@@ -59,7 +59,7 @@ object RecipeImplTestData {
|
||||
dateUpdated = LocalDateTime.parse("2021-10-13T17:35:23"),
|
||||
)
|
||||
|
||||
private val SUGAR_INGREDIENT = GetRecipeIngredientResponse(
|
||||
private val SUGAR_INGREDIENT = GetRecipeIngredientResponseV0(
|
||||
title = "Sugar",
|
||||
note = "2 oz of white sugar",
|
||||
unit = "",
|
||||
@@ -68,7 +68,7 @@ object RecipeImplTestData {
|
||||
quantity = 1
|
||||
)
|
||||
|
||||
val BREAD_INGREDIENT = GetRecipeIngredientResponse(
|
||||
val BREAD_INGREDIENT = GetRecipeIngredientResponseV0(
|
||||
title = "Bread",
|
||||
note = "2 oz of white bread",
|
||||
unit = "",
|
||||
@@ -77,7 +77,7 @@ object RecipeImplTestData {
|
||||
quantity = 2
|
||||
)
|
||||
|
||||
private val MILK_INGREDIENT = GetRecipeIngredientResponse(
|
||||
private val MILK_INGREDIENT = GetRecipeIngredientResponseV0(
|
||||
title = "Milk",
|
||||
note = "2 oz of white milk",
|
||||
unit = "",
|
||||
@@ -86,22 +86,22 @@ object RecipeImplTestData {
|
||||
quantity = 3
|
||||
)
|
||||
|
||||
val MIX_INSTRUCTION = GetRecipeInstructionResponse(
|
||||
val MIX_INSTRUCTION = GetRecipeInstructionResponseV0(
|
||||
title = "Mix",
|
||||
text = "Mix the ingredients"
|
||||
)
|
||||
|
||||
private val BAKE_INSTRUCTION = GetRecipeInstructionResponse(
|
||||
private val BAKE_INSTRUCTION = GetRecipeInstructionResponseV0(
|
||||
title = "Bake",
|
||||
text = "Bake the ingredients"
|
||||
)
|
||||
|
||||
private val BOIL_INSTRUCTION = GetRecipeInstructionResponse(
|
||||
private val BOIL_INSTRUCTION = GetRecipeInstructionResponseV0(
|
||||
title = "Boil",
|
||||
text = "Boil the ingredients"
|
||||
)
|
||||
|
||||
val GET_CAKE_RESPONSE = GetRecipeResponse(
|
||||
val GET_CAKE_RESPONSE = GetRecipeResponseV0(
|
||||
remoteId = 1,
|
||||
name = "Cake",
|
||||
slug = "cake",
|
||||
@@ -117,7 +117,7 @@ object RecipeImplTestData {
|
||||
recipeInstructions = listOf(MIX_INSTRUCTION, BAKE_INSTRUCTION)
|
||||
)
|
||||
|
||||
val GET_PORRIDGE_RESPONSE = GetRecipeResponse(
|
||||
val GET_PORRIDGE_RESPONSE = GetRecipeResponseV0(
|
||||
remoteId = 2,
|
||||
name = "Porridge",
|
||||
slug = "porridge",
|
||||
|
||||
@@ -2,7 +2,7 @@ package gq.kirmanak.mealient.ui.add
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.coEvery
|
||||
@@ -61,21 +61,21 @@ class AddRecipeViewModelTest {
|
||||
|
||||
@Test
|
||||
fun `when preserve then doesn't update UI`() {
|
||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeRequest())
|
||||
subject.preserve(AddRecipeRequest())
|
||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeRequestV0())
|
||||
subject.preserve(AddRecipeRequestV0())
|
||||
coVerify(inverse = true) { addRecipeRepo.addRecipeRequestFlow }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when preservedAddRecipeRequest without loadPreservedRequest then empty`() = runTest {
|
||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeRequest())
|
||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeRequestV0())
|
||||
val actual = withTimeoutOrNull(10) { subject.preservedAddRecipeRequest.firstOrNull() }
|
||||
assertThat(actual).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when loadPreservedRequest then updates preservedAddRecipeRequest`() = runTest {
|
||||
val expected = AddRecipeRequest()
|
||||
val expected = AddRecipeRequestV0()
|
||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected)
|
||||
subject.loadPreservedRequest()
|
||||
assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected)
|
||||
@@ -83,7 +83,7 @@ class AddRecipeViewModelTest {
|
||||
|
||||
@Test
|
||||
fun `when clear then updates preservedAddRecipeRequest`() = runTest {
|
||||
val expected = AddRecipeRequest()
|
||||
val expected = AddRecipeRequestV0()
|
||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected)
|
||||
subject.clear()
|
||||
assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected)
|
||||
|
||||
@@ -6,6 +6,9 @@ import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0Impl
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieServiceV0
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1Impl
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieServiceV1
|
||||
@@ -52,7 +55,7 @@ interface DataSourceModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideMealieService(retrofit: Retrofit): MealieService =
|
||||
fun provideMealieService(retrofit: Retrofit): MealieServiceV0 =
|
||||
retrofit.create()
|
||||
|
||||
@Provides
|
||||
@@ -71,7 +74,7 @@ interface DataSourceModule {
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
fun bindMealieDataSource(mealientDataSourceImpl: MealieDataSourceImpl): MealieDataSource
|
||||
fun bindMealieDataSource(mealientDataSourceImpl: MealieDataSourceV0Impl): MealieDataSourceV0
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource
|
||||
|
||||
sealed class NetworkError(cause: Throwable) : RuntimeException(cause) {
|
||||
class Unauthorized(cause: Throwable) : NetworkError(cause)
|
||||
@@ -1,54 +0,0 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeRequest(
|
||||
@SerialName("name") val name: String = "",
|
||||
@SerialName("description") val description: String = "",
|
||||
@SerialName("image") val image: String = "",
|
||||
@SerialName("recipeYield") val recipeYield: String = "",
|
||||
@SerialName("recipeIngredient") val recipeIngredient: List<AddRecipeIngredient> = emptyList(),
|
||||
@SerialName("recipeInstructions") val recipeInstructions: List<AddRecipeInstruction> = emptyList(),
|
||||
@SerialName("slug") val slug: String = "",
|
||||
@SerialName("filePath") val filePath: String = "",
|
||||
@SerialName("tags") val tags: List<String> = emptyList(),
|
||||
@SerialName("categories") val categories: List<String> = emptyList(),
|
||||
@SerialName("notes") val notes: List<AddRecipeNote> = emptyList(),
|
||||
@SerialName("extras") val extras: Map<String, String> = emptyMap(),
|
||||
@SerialName("assets") val assets: List<String> = emptyList(),
|
||||
@SerialName("settings") val settings: AddRecipeSettings = AddRecipeSettings(),
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeSettings(
|
||||
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||
@SerialName("disableComments") val disableComments: Boolean = false,
|
||||
@SerialName("landscapeView") val landscapeView: Boolean = true,
|
||||
@SerialName("public") val public: Boolean = true,
|
||||
@SerialName("showAssets") val showAssets: Boolean = true,
|
||||
@SerialName("showNutrition") val showNutrition: Boolean = true,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeNote(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String = "",
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeInstruction(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String = "",
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeIngredient(
|
||||
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||
@SerialName("food") val food: String? = null,
|
||||
@SerialName("note") val note: String = "",
|
||||
@SerialName("quantity") val quantity: Int = 1,
|
||||
@SerialName("title") val title: String? = null,
|
||||
@SerialName("unit") val unit: String? = null,
|
||||
)
|
||||
@@ -1,7 +0,0 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ErrorDetail(@SerialName("detail") val detail: String? = null)
|
||||
@@ -1,7 +0,0 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetTokenResponse(@SerialName("access_token") val accessToken: String)
|
||||
@@ -1,14 +0,0 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class VersionResponse(
|
||||
@SerialName("production")
|
||||
val production: Boolean,
|
||||
@SerialName("version")
|
||||
val version: String,
|
||||
@SerialName("demoStatus")
|
||||
val demoStatus: Boolean,
|
||||
)
|
||||
@@ -1,16 +1,16 @@
|
||||
package gq.kirmanak.mealient.datasource
|
||||
package gq.kirmanak.mealient.datasource.v0
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeSummaryResponse
|
||||
import gq.kirmanak.mealient.datasource.models.VersionResponse
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetRecipeResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetRecipeSummaryResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.VersionResponseV0
|
||||
|
||||
interface MealieDataSource {
|
||||
interface MealieDataSourceV0 {
|
||||
|
||||
suspend fun addRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
recipe: AddRecipeRequest,
|
||||
recipe: AddRecipeRequestV0,
|
||||
): String
|
||||
|
||||
/**
|
||||
@@ -24,18 +24,18 @@ interface MealieDataSource {
|
||||
|
||||
suspend fun getVersionInfo(
|
||||
baseUrl: String,
|
||||
): VersionResponse
|
||||
): VersionResponseV0
|
||||
|
||||
suspend fun requestRecipes(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
start: Int,
|
||||
limit: Int,
|
||||
): List<GetRecipeSummaryResponse>
|
||||
): List<GetRecipeSummaryResponseV0>
|
||||
|
||||
suspend fun requestRecipeInfo(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
slug: String,
|
||||
): GetRecipeResponse
|
||||
): GetRecipeResponseV0
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package gq.kirmanak.mealient.datasource
|
||||
package gq.kirmanak.mealient.datasource.v0
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.*
|
||||
import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.v0.models.*
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.SerializationException
|
||||
@@ -14,14 +15,14 @@ import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class MealieDataSourceImpl @Inject constructor(
|
||||
class MealieDataSourceV0Impl @Inject constructor(
|
||||
private val logger: Logger,
|
||||
private val mealieService: MealieService,
|
||||
private val mealieServiceV0: MealieServiceV0,
|
||||
private val json: Json,
|
||||
) : MealieDataSource {
|
||||
) : MealieDataSourceV0 {
|
||||
|
||||
override suspend fun addRecipe(
|
||||
baseUrl: String, token: String?, recipe: AddRecipeRequest
|
||||
baseUrl: String, token: String?, recipe: AddRecipeRequestV0
|
||||
): String = makeCall(
|
||||
block = { addRecipe("$baseUrl/api/recipes/create", token, recipe) },
|
||||
logMethod = { "addRecipe" },
|
||||
@@ -36,11 +37,11 @@ class MealieDataSourceImpl @Inject constructor(
|
||||
logParameters = { "baseUrl = $baseUrl, username = $username, password = $password" }
|
||||
).map { it.accessToken }.getOrElse {
|
||||
val errorBody = (it as? HttpException)?.response()?.errorBody() ?: throw it
|
||||
val errorDetail = errorBody.decode<ErrorDetail>()
|
||||
throw if (errorDetail.detail == "Unauthorized") NetworkError.Unauthorized(it) else it
|
||||
val errorDetailV0 = errorBody.decode<ErrorDetailV0>()
|
||||
throw if (errorDetailV0.detail == "Unauthorized") NetworkError.Unauthorized(it) else it
|
||||
}
|
||||
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionResponse = makeCall(
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionResponseV0 = makeCall(
|
||||
block = { getVersion("$baseUrl/api/debug/version") },
|
||||
logMethod = { "getVersionInfo" },
|
||||
logParameters = { "baseUrl = $baseUrl" },
|
||||
@@ -56,7 +57,7 @@ class MealieDataSourceImpl @Inject constructor(
|
||||
|
||||
override suspend fun requestRecipes(
|
||||
baseUrl: String, token: String?, start: Int, limit: Int
|
||||
): List<GetRecipeSummaryResponse> = makeCall(
|
||||
): List<GetRecipeSummaryResponseV0> = makeCall(
|
||||
block = { getRecipeSummary("$baseUrl/api/recipes/summary", token, start, limit) },
|
||||
logMethod = { "requestRecipes" },
|
||||
logParameters = { "baseUrl = $baseUrl, token = $token, start = $start, limit = $limit" }
|
||||
@@ -67,19 +68,19 @@ class MealieDataSourceImpl @Inject constructor(
|
||||
|
||||
override suspend fun requestRecipeInfo(
|
||||
baseUrl: String, token: String?, slug: String
|
||||
): GetRecipeResponse = makeCall(
|
||||
): GetRecipeResponseV0 = makeCall(
|
||||
block = { getRecipe("$baseUrl/api/recipes/$slug", token) },
|
||||
logMethod = { "requestRecipeInfo" },
|
||||
logParameters = { "baseUrl = $baseUrl, token = $token, slug = $slug" }
|
||||
).getOrThrowUnauthorized()
|
||||
|
||||
private suspend inline fun <T> makeCall(
|
||||
crossinline block: suspend MealieService.() -> T,
|
||||
crossinline block: suspend MealieServiceV0.() -> T,
|
||||
crossinline logMethod: () -> String,
|
||||
crossinline logParameters: () -> String,
|
||||
): Result<T> {
|
||||
logger.v { "${logMethod()} called with: ${logParameters()}" }
|
||||
return mealieService.runCatching { block() }
|
||||
return mealieServiceV0.runCatching { block() }
|
||||
.onFailure { logger.e(it) { "${logMethod()} request failed with: ${logParameters()}" } }
|
||||
.onSuccess { logger.d { "${logMethod()} request succeeded with ${logParameters()}" } }
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource
|
||||
package gq.kirmanak.mealient.datasource.v0
|
||||
|
||||
import gq.kirmanak.mealient.datasource.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
|
||||
import gq.kirmanak.mealient.datasource.models.*
|
||||
import gq.kirmanak.mealient.datasource.v0.models.*
|
||||
import retrofit2.http.*
|
||||
|
||||
interface MealieService {
|
||||
interface MealieServiceV0 {
|
||||
|
||||
@FormUrlEncoded
|
||||
@POST
|
||||
@@ -12,19 +12,19 @@ interface MealieService {
|
||||
@Url url: String,
|
||||
@Field("username") username: String,
|
||||
@Field("password") password: String,
|
||||
): GetTokenResponse
|
||||
): GetTokenResponseV0
|
||||
|
||||
@POST
|
||||
suspend fun addRecipe(
|
||||
@Url url: String,
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
@Body addRecipeRequest: AddRecipeRequest,
|
||||
@Body addRecipeRequestV0: AddRecipeRequestV0,
|
||||
): String
|
||||
|
||||
@GET
|
||||
suspend fun getVersion(
|
||||
@Url url: String,
|
||||
): VersionResponse
|
||||
): VersionResponseV0
|
||||
|
||||
@GET
|
||||
suspend fun getRecipeSummary(
|
||||
@@ -32,11 +32,11 @@ interface MealieService {
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
@Query("start") start: Int,
|
||||
@Query("limit") limit: Int,
|
||||
): List<GetRecipeSummaryResponse>
|
||||
): List<GetRecipeSummaryResponseV0>
|
||||
|
||||
@GET
|
||||
suspend fun getRecipe(
|
||||
@Url url: String,
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
): GetRecipeResponse
|
||||
): GetRecipeResponseV0
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeIngredientV0(
|
||||
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||
@SerialName("food") val food: String? = null,
|
||||
@SerialName("note") val note: String = "",
|
||||
@SerialName("quantity") val quantity: Int = 1,
|
||||
@SerialName("title") val title: String? = null,
|
||||
@SerialName("unit") val unit: String? = null,
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeInstructionV0(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String = "",
|
||||
)
|
||||
@@ -0,0 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeNoteV0(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String = "",
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeRequestV0(
|
||||
@SerialName("name") val name: String = "",
|
||||
@SerialName("description") val description: String = "",
|
||||
@SerialName("image") val image: String = "",
|
||||
@SerialName("recipeYield") val recipeYield: String = "",
|
||||
@SerialName("recipeIngredient") val recipeIngredient: List<AddRecipeIngredientV0> = emptyList(),
|
||||
@SerialName("recipeInstructions") val recipeInstructions: List<AddRecipeInstructionV0> = emptyList(),
|
||||
@SerialName("slug") val slug: String = "",
|
||||
@SerialName("filePath") val filePath: String = "",
|
||||
@SerialName("tags") val tags: List<String> = emptyList(),
|
||||
@SerialName("categories") val categories: List<String> = emptyList(),
|
||||
@SerialName("notes") val notes: List<AddRecipeNoteV0> = emptyList(),
|
||||
@SerialName("extras") val extras: Map<String, String> = emptyMap(),
|
||||
@SerialName("assets") val assets: List<String> = emptyList(),
|
||||
@SerialName("settings") val settings: AddRecipeSettingsV0 = AddRecipeSettingsV0(),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class AddRecipeSettingsV0(
|
||||
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||
@SerialName("disableComments") val disableComments: Boolean = false,
|
||||
@SerialName("landscapeView") val landscapeView: Boolean = true,
|
||||
@SerialName("public") val public: Boolean = true,
|
||||
@SerialName("showAssets") val showAssets: Boolean = true,
|
||||
@SerialName("showNutrition") val showNutrition: Boolean = true,
|
||||
)
|
||||
@@ -0,0 +1,7 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ErrorDetailV0(@SerialName("detail") val detail: String? = null)
|
||||
@@ -1,10 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetRecipeIngredientResponse(
|
||||
data class GetRecipeIngredientResponseV0(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("note") val note: String = "",
|
||||
@SerialName("unit") val unit: String = "",
|
||||
@@ -1,10 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetRecipeInstructionResponse(
|
||||
data class GetRecipeInstructionResponseV0(
|
||||
@SerialName("title") val title: String = "",
|
||||
@SerialName("text") val text: String,
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
@@ -6,7 +6,7 @@ import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetRecipeResponse(
|
||||
data class GetRecipeResponseV0(
|
||||
@SerialName("id") val remoteId: Int,
|
||||
@SerialName("name") val name: String,
|
||||
@SerialName("slug") val slug: String,
|
||||
@@ -18,6 +18,6 @@ data class GetRecipeResponse(
|
||||
@SerialName("dateAdded") val dateAdded: LocalDate,
|
||||
@SerialName("dateUpdated") val dateUpdated: LocalDateTime,
|
||||
@SerialName("recipeYield") val recipeYield: String = "",
|
||||
@SerialName("recipeIngredient") val recipeIngredients: List<GetRecipeIngredientResponse>,
|
||||
@SerialName("recipeInstructions") val recipeInstructions: List<GetRecipeInstructionResponse>,
|
||||
@SerialName("recipeIngredient") val recipeIngredients: List<GetRecipeIngredientResponseV0>,
|
||||
@SerialName("recipeInstructions") val recipeInstructions: List<GetRecipeInstructionResponseV0>,
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
package gq.kirmanak.mealient.datasource.models
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
@@ -6,7 +6,7 @@ import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetRecipeSummaryResponse(
|
||||
data class GetRecipeSummaryResponseV0(
|
||||
@SerialName("id") val remoteId: Int,
|
||||
@SerialName("name") val name: String,
|
||||
@SerialName("slug") val slug: String,
|
||||
@@ -0,0 +1,7 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class GetTokenResponseV0(@SerialName("access_token") val accessToken: String)
|
||||
@@ -0,0 +1,11 @@
|
||||
package gq.kirmanak.mealient.datasource.v0.models
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class VersionResponseV0(
|
||||
@SerialName("production") val production: Boolean,
|
||||
@SerialName("version") val version: String,
|
||||
@SerialName("demoStatus") val demoStatus: Boolean,
|
||||
)
|
||||
@@ -1,6 +1,6 @@
|
||||
package gq.kirmanak.mealient.datasource.v1
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
||||
@@ -10,7 +10,7 @@ interface MealieDataSourceV1 {
|
||||
suspend fun addRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
recipe: AddRecipeRequest,
|
||||
recipe: AddRecipeRequestV0,
|
||||
): String
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package gq.kirmanak.mealient.datasource.v1
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
||||
@@ -24,7 +24,7 @@ class MealieDataSourceV1Impl @Inject constructor(
|
||||
override suspend fun addRecipe(
|
||||
baseUrl: String,
|
||||
token: String?,
|
||||
recipe: AddRecipeRequest
|
||||
recipe: AddRecipeRequestV0
|
||||
): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package gq.kirmanak.mealient.datasource.v1
|
||||
|
||||
import gq.kirmanak.mealient.datasource.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
|
||||
import gq.kirmanak.mealient.datasource.models.*
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetTokenResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipesResponseV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
||||
@@ -15,13 +17,13 @@ interface MealieServiceV1 {
|
||||
@Url url: String,
|
||||
@Field("username") username: String,
|
||||
@Field("password") password: String,
|
||||
): GetTokenResponse
|
||||
): GetTokenResponseV0
|
||||
|
||||
@POST
|
||||
suspend fun addRecipe(
|
||||
@Url url: String,
|
||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||
@Body addRecipeRequest: AddRecipeRequest,
|
||||
@Body addRecipeRequestV0: AddRecipeRequestV0,
|
||||
): String
|
||||
|
||||
@GET
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package gq.kirmanak.mealient.datasource
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import gq.kirmanak.mealient.datasource.models.GetTokenResponse
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.models.VersionResponse
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0Impl
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieServiceV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.GetTokenResponseV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.VersionResponseV0
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import gq.kirmanak.mealient.test.toJsonResponseBody
|
||||
import io.mockk.MockKAnnotations
|
||||
@@ -21,25 +22,25 @@ import java.io.IOException
|
||||
import java.net.ConnectException
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class MealieDataSourceImplTest {
|
||||
class MealieDataSourceV0ImplTest {
|
||||
|
||||
@MockK
|
||||
lateinit var service: MealieService
|
||||
lateinit var service: MealieServiceV0
|
||||
|
||||
@MockK(relaxUnitFun = true)
|
||||
lateinit var logger: Logger
|
||||
|
||||
lateinit var subject: MealieDataSourceImpl
|
||||
lateinit var subject: MealieDataSourceV0Impl
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockKAnnotations.init(this)
|
||||
subject = MealieDataSourceImpl(logger, service, Json.Default)
|
||||
subject = MealieDataSourceV0Impl(logger, service, Json)
|
||||
}
|
||||
|
||||
@Test(expected = NetworkError.NotMealie::class)
|
||||
fun `when getVersionInfo and getVersion throws HttpException then NotMealie`() = runTest {
|
||||
val error = HttpException(Response.error<VersionResponse>(404, "".toJsonResponseBody()))
|
||||
val error = HttpException(Response.error<VersionResponseV0>(404, "".toJsonResponseBody()))
|
||||
coEvery { service.getVersion(any()) } throws error
|
||||
subject.getVersionInfo(TEST_BASE_URL)
|
||||
}
|
||||
@@ -60,14 +61,14 @@ class MealieDataSourceImplTest {
|
||||
|
||||
@Test
|
||||
fun `when getVersionInfo and getVersion returns result then result`() = runTest {
|
||||
val versionResponse = VersionResponse(true, "v0.5.6", true)
|
||||
val versionResponse = VersionResponseV0(true, "v0.5.6", true)
|
||||
coEvery { service.getVersion(any()) } returns versionResponse
|
||||
assertThat(subject.getVersionInfo(TEST_BASE_URL)).isSameInstanceAs(versionResponse)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when authentication is successful then token is correct`() = runTest {
|
||||
coEvery { service.getToken(any(), any(), any()) } returns GetTokenResponse(TEST_TOKEN)
|
||||
coEvery { service.getToken(any(), any(), any()) } returns GetTokenResponseV0(TEST_TOKEN)
|
||||
assertThat(callAuthenticate()).isEqualTo(TEST_TOKEN)
|
||||
}
|
||||
|
||||
@@ -76,7 +77,7 @@ class MealieDataSourceImplTest {
|
||||
val body = "{\"detail\":\"Unauthorized\"}".toJsonResponseBody()
|
||||
coEvery {
|
||||
service.getToken(any(), any(), any())
|
||||
} throws HttpException(Response.error<GetTokenResponse>(401, body))
|
||||
} throws HttpException(Response.error<GetTokenResponseV0>(401, body))
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
@@ -85,7 +86,7 @@ class MealieDataSourceImplTest {
|
||||
val body = "{\"detail\":\"Something\"}".toJsonResponseBody()
|
||||
coEvery {
|
||||
service.getToken(any(), any(), any())
|
||||
} throws HttpException(Response.error<GetTokenResponse>(401, body))
|
||||
} throws HttpException(Response.error<GetTokenResponseV0>(401, body))
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
@@ -94,7 +95,7 @@ class MealieDataSourceImplTest {
|
||||
val body = "".toJsonResponseBody()
|
||||
coEvery {
|
||||
service.getToken(any(), any(), any())
|
||||
} throws HttpException(Response.error<GetTokenResponse>(401, body))
|
||||
} throws HttpException(Response.error<GetTokenResponseV0>(401, body))
|
||||
callAuthenticate()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user