Move V0 responses to v0 package

This commit is contained in:
Kirill Kamakin
2022-10-29 18:15:10 +02:00
parent 3f2f945d99
commit 7702dbebd1
40 changed files with 261 additions and 240 deletions

View File

@@ -1,7 +1,7 @@
package gq.kirmanak.mealient.data.add package gq.kirmanak.mealient.data.add
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
interface AddRecipeDataSource { interface AddRecipeDataSource {
suspend fun addRecipe(recipe: AddRecipeRequest): String suspend fun addRecipe(recipe: AddRecipeRequestV0): String
} }

View File

@@ -1,13 +1,13 @@
package gq.kirmanak.mealient.data.add 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 import kotlinx.coroutines.flow.Flow
interface AddRecipeRepo { interface AddRecipeRepo {
val addRecipeRequestFlow: Flow<AddRecipeRequest> val addRecipeRequestFlow: Flow<AddRecipeRequestV0>
suspend fun preserve(recipe: AddRecipeRequest) suspend fun preserve(recipe: AddRecipeRequestV0)
suspend fun clear() suspend fun clear()

View File

@@ -2,7 +2,7 @@ package gq.kirmanak.mealient.data.add.impl
import gq.kirmanak.mealient.data.add.AddRecipeDataSource import gq.kirmanak.mealient.data.add.AddRecipeDataSource
import gq.kirmanak.mealient.data.add.AddRecipeRepo 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.datastore.recipe.AddRecipeStorage
import gq.kirmanak.mealient.extensions.toAddRecipeRequest import gq.kirmanak.mealient.extensions.toAddRecipeRequest
import gq.kirmanak.mealient.extensions.toDraft import gq.kirmanak.mealient.extensions.toDraft
@@ -20,10 +20,10 @@ class AddRecipeRepoImpl @Inject constructor(
private val logger: Logger, private val logger: Logger,
) : AddRecipeRepo { ) : AddRecipeRepo {
override val addRecipeRequestFlow: Flow<AddRecipeRequest> override val addRecipeRequestFlow: Flow<AddRecipeRequestV0>
get() = addRecipeStorage.updates.map { it.toAddRecipeRequest() } 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" } logger.v { "preserveRecipe() called with: recipe = $recipe" }
addRecipeStorage.save(recipe.toDraft()) addRecipeStorage.save(recipe.toDraft())
} }

View File

@@ -1,15 +1,15 @@
package gq.kirmanak.mealient.data.auth.impl package gq.kirmanak.mealient.data.auth.impl
import gq.kirmanak.mealient.data.auth.AuthDataSource 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.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class AuthDataSourceImpl @Inject constructor( class AuthDataSourceImpl @Inject constructor(
private val mealieDataSource: MealieDataSource, private val V0Source: MealieDataSourceV0,
) : AuthDataSource { ) : AuthDataSource {
override suspend fun authenticate(username: String, password: String, baseUrl: String): String = override suspend fun authenticate(username: String, password: String, baseUrl: String): String =
mealieDataSource.authenticate(baseUrl, username, password) V0Source.authenticate(baseUrl, username, password)
} }

View File

@@ -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.FullRecipeInfo
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
import gq.kirmanak.mealient.data.recipes.network.RecipeSummaryInfo import gq.kirmanak.mealient.data.recipes.network.RecipeSummaryInfo
import gq.kirmanak.mealient.datasource.MealieDataSource import gq.kirmanak.mealient.datasource.NetworkError
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
import gq.kirmanak.mealient.datasource.models.NetworkError import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1 import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1
import gq.kirmanak.mealient.extensions.runCatchingExceptCancel import gq.kirmanak.mealient.extensions.runCatchingExceptCancel
import gq.kirmanak.mealient.extensions.toFullRecipeInfo import gq.kirmanak.mealient.extensions.toFullRecipeInfo
@@ -23,19 +23,19 @@ import javax.inject.Singleton
class MealieDataSourceWrapper @Inject constructor( class MealieDataSourceWrapper @Inject constructor(
private val baseURLStorage: BaseURLStorage, private val baseURLStorage: BaseURLStorage,
private val authRepo: AuthRepo, private val authRepo: AuthRepo,
private val source: MealieDataSource, private val V0source: MealieDataSourceV0,
private val v1Source: MealieDataSourceV1, private val V1Source: MealieDataSourceV1,
) : AddRecipeDataSource, RecipeDataSource, VersionDataSource { ) : AddRecipeDataSource, RecipeDataSource, VersionDataSource {
override suspend fun addRecipe(recipe: AddRecipeRequest): String = override suspend fun addRecipe(recipe: AddRecipeRequestV0): String =
withAuthHeader { token -> source.addRecipe(getUrl(), token, recipe) } withAuthHeader { token -> V0source.addRecipe(getUrl(), token, recipe) }
override suspend fun getVersionInfo(baseUrl: String): VersionInfo = override suspend fun getVersionInfo(baseUrl: String): VersionInfo =
runCatchingExceptCancel { runCatchingExceptCancel {
source.getVersionInfo(baseUrl).toVersionInfo() V0source.getVersionInfo(baseUrl).toVersionInfo()
}.getOrElse { }.getOrElse {
if (it is NetworkError.NotMealie) { if (it is NetworkError.NotMealie) {
v1Source.getVersionInfo(baseUrl).toVersionInfo() V1Source.getVersionInfo(baseUrl).toVersionInfo()
} else { } else {
throw it throw it
} }
@@ -45,9 +45,9 @@ class MealieDataSourceWrapper @Inject constructor(
withAuthHeader { token -> withAuthHeader { token ->
val url = getUrl() val url = getUrl()
if (isV1()) { if (isV1()) {
v1Source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() } V1Source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
} else { } 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 -> withAuthHeader { token ->
val url = getUrl() val url = getUrl()
if (isV1()) { if (isV1()) {
v1Source.requestRecipeInfo(url, token, slug).toFullRecipeInfo() V1Source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
} else { } else {
source.requestRecipeInfo(url, token, slug).toFullRecipeInfo() V0source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
} }
} }

View File

@@ -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.RecipeIngredientEntity
import gq.kirmanak.mealient.database.recipe.entity.RecipeInstructionEntity import gq.kirmanak.mealient.database.recipe.entity.RecipeInstructionEntity
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity 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.datasource.v1.models.*
import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft
@@ -34,7 +34,7 @@ fun RecipeInstructionInfo.toRecipeInstructionEntity(remoteId: String) = RecipeIn
text = text text = text
) )
fun GetRecipeSummaryResponse.toRecipeSummaryInfo() = RecipeSummaryInfo( fun GetRecipeSummaryResponseV0.toRecipeSummaryInfo() = RecipeSummaryInfo(
remoteId = remoteId.toString(), remoteId = remoteId.toString(),
name = name, name = name,
slug = slug, slug = slug,
@@ -74,23 +74,23 @@ fun RecipeSummaryInfo.recipeEntity() = RecipeSummaryEntity(
imageId = imageId, imageId = imageId,
) )
fun VersionResponse.toVersionInfo() = VersionInfo(version) fun VersionResponseV0.toVersionInfo() = VersionInfo(version)
fun VersionResponseV1.toVersionInfo() = VersionInfo(version) fun VersionResponseV1.toVersionInfo() = VersionInfo(version)
fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequest( fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequestV0(
name = recipeName, name = recipeName,
description = recipeDescription, description = recipeDescription,
recipeYield = recipeYield, recipeYield = recipeYield,
recipeIngredient = recipeIngredients.map { AddRecipeIngredient(note = it) }, recipeIngredient = recipeIngredients.map { AddRecipeIngredientV0(note = it) },
recipeInstructions = recipeInstructions.map { AddRecipeInstruction(text = it) }, recipeInstructions = recipeInstructions.map { AddRecipeInstructionV0(text = it) },
settings = AddRecipeSettings( settings = AddRecipeSettingsV0(
public = isRecipePublic, public = isRecipePublic,
disableComments = areCommentsDisabled, disableComments = areCommentsDisabled,
) )
) )
fun AddRecipeRequest.toDraft(): AddRecipeDraft = AddRecipeDraft( fun AddRecipeRequestV0.toDraft(): AddRecipeDraft = AddRecipeDraft(
recipeName = name, recipeName = name,
recipeDescription = description, recipeDescription = description,
recipeYield = recipeYield, recipeYield = recipeYield,
@@ -100,7 +100,7 @@ fun AddRecipeRequest.toDraft(): AddRecipeDraft = AddRecipeDraft(
areCommentsDisabled = settings.disableComments, areCommentsDisabled = settings.disableComments,
) )
fun GetRecipeResponse.toFullRecipeInfo() = FullRecipeInfo( fun GetRecipeResponseV0.toFullRecipeInfo() = FullRecipeInfo(
remoteId = remoteId.toString(), remoteId = remoteId.toString(),
name = name, name = name,
slug = slug, slug = slug,
@@ -116,7 +116,7 @@ fun GetRecipeResponse.toFullRecipeInfo() = FullRecipeInfo(
recipeInstructions = recipeInstructions.map { it.toRecipeInstructionInfo() } recipeInstructions = recipeInstructions.map { it.toRecipeInstructionInfo() }
) )
fun GetRecipeIngredientResponse.toRecipeIngredientInfo() = RecipeIngredientInfo( fun GetRecipeIngredientResponseV0.toRecipeIngredientInfo() = RecipeIngredientInfo(
title = title, title = title,
note = note, note = note,
unit = unit, unit = unit,
@@ -125,7 +125,7 @@ fun GetRecipeIngredientResponse.toRecipeIngredientInfo() = RecipeIngredientInfo(
quantity = quantity quantity = quantity
) )
fun GetRecipeInstructionResponse.toRecipeInstructionInfo() = RecipeInstructionInfo( fun GetRecipeInstructionResponseV0.toRecipeInstructionInfo() = RecipeInstructionInfo(
title = title, title = title,
text = text text = text
) )

View File

@@ -14,10 +14,10 @@ import dagger.hilt.android.AndroidEntryPoint
import gq.kirmanak.mealient.R import gq.kirmanak.mealient.R
import gq.kirmanak.mealient.databinding.FragmentAddRecipeBinding import gq.kirmanak.mealient.databinding.FragmentAddRecipeBinding
import gq.kirmanak.mealient.databinding.ViewSingleInputBinding import gq.kirmanak.mealient.databinding.ViewSingleInputBinding
import gq.kirmanak.mealient.datasource.models.AddRecipeIngredient import gq.kirmanak.mealient.datasource.v0.models.AddRecipeIngredientV0
import gq.kirmanak.mealient.datasource.models.AddRecipeInstruction import gq.kirmanak.mealient.datasource.v0.models.AddRecipeInstructionV0
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
import gq.kirmanak.mealient.datasource.models.AddRecipeSettings import gq.kirmanak.mealient.datasource.v0.models.AddRecipeSettingsV0
import gq.kirmanak.mealient.extensions.checkIfInputIsEmpty import gq.kirmanak.mealient.extensions.checkIfInputIsEmpty
import gq.kirmanak.mealient.extensions.collectWhenViewResumed import gq.kirmanak.mealient.extensions.collectWhenViewResumed
import gq.kirmanak.mealient.logging.Logger import gq.kirmanak.mealient.logging.Logger
@@ -122,14 +122,14 @@ class AddRecipeFragment : Fragment(R.layout.fragment_add_recipe) {
private fun saveValues() = with(binding) { private fun saveValues() = with(binding) {
logger.v { "saveValues() called" } logger.v { "saveValues() called" }
val instructions = parseInputRows(instructionsFlow).map { AddRecipeInstruction(text = it) } val instructions = parseInputRows(instructionsFlow).map { AddRecipeInstructionV0(text = it) }
val ingredients = parseInputRows(ingredientsFlow).map { AddRecipeIngredient(note = it) } val ingredients = parseInputRows(ingredientsFlow).map { AddRecipeIngredientV0(note = it) }
val settings = AddRecipeSettings( val settings = AddRecipeSettingsV0(
public = publicRecipe.isChecked, public = publicRecipe.isChecked,
disableComments = disableComments.isChecked, disableComments = disableComments.isChecked,
) )
viewModel.preserve( viewModel.preserve(
AddRecipeRequest( AddRecipeRequestV0(
name = recipeNameInput.text.toString(), name = recipeNameInput.text.toString(),
description = recipeDescriptionInput.text.toString(), description = recipeDescriptionInput.text.toString(),
recipeYield = recipeYieldInput.text.toString(), recipeYield = recipeYieldInput.text.toString(),
@@ -148,7 +148,7 @@ class AddRecipeFragment : Fragment(R.layout.fragment_add_recipe) {
.filterNot { it.isBlank() } .filterNot { it.isBlank() }
.toList() .toList()
private fun onSavedInputLoaded(request: AddRecipeRequest) = with(binding) { private fun onSavedInputLoaded(request: AddRecipeRequestV0) = with(binding) {
logger.v { "onSavedInputLoaded() called with: request = $request" } logger.v { "onSavedInputLoaded() called with: request = $request" }
recipeNameInput.setText(request.name) recipeNameInput.setText(request.name)
recipeDescriptionInput.setText(request.description) recipeDescriptionInput.setText(request.description)

View File

@@ -4,7 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import gq.kirmanak.mealient.data.add.AddRecipeRepo 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.extensions.runCatchingExceptCancel
import gq.kirmanak.mealient.logging.Logger import gq.kirmanak.mealient.logging.Logger
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
@@ -23,8 +23,8 @@ class AddRecipeViewModel @Inject constructor(
private val _addRecipeResultChannel = Channel<Boolean>(Channel.UNLIMITED) private val _addRecipeResultChannel = Channel<Boolean>(Channel.UNLIMITED)
val addRecipeResult: Flow<Boolean> get() = _addRecipeResultChannel.receiveAsFlow() val addRecipeResult: Flow<Boolean> get() = _addRecipeResultChannel.receiveAsFlow()
private val _preservedAddRecipeRequestChannel = Channel<AddRecipeRequest>(Channel.UNLIMITED) private val _preservedAddRecipeRequestChannel = Channel<AddRecipeRequestV0>(Channel.UNLIMITED)
val preservedAddRecipeRequest: Flow<AddRecipeRequest> val preservedAddRecipeRequest: Flow<AddRecipeRequestV0>
get() = _preservedAddRecipeRequestChannel.receiveAsFlow() get() = _preservedAddRecipeRequestChannel.receiveAsFlow()
fun loadPreservedRequest() { 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" } logger.v { "preserve() called with: request = $request" }
viewModelScope.launch { addRecipeRepo.preserve(request) } viewModelScope.launch { addRecipeRepo.preserve(request) }
} }

View File

@@ -10,7 +10,7 @@ import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import gq.kirmanak.mealient.R import gq.kirmanak.mealient.R
import gq.kirmanak.mealient.databinding.FragmentAuthenticationBinding 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.extensions.checkIfInputIsEmpty
import gq.kirmanak.mealient.logging.Logger import gq.kirmanak.mealient.logging.Logger
import gq.kirmanak.mealient.ui.OperationUiState import gq.kirmanak.mealient.ui.OperationUiState

View File

@@ -10,7 +10,7 @@ import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import gq.kirmanak.mealient.R import gq.kirmanak.mealient.R
import gq.kirmanak.mealient.databinding.FragmentBaseUrlBinding 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.extensions.checkIfInputIsEmpty
import gq.kirmanak.mealient.logging.Logger import gq.kirmanak.mealient.logging.Logger
import gq.kirmanak.mealient.ui.OperationUiState import gq.kirmanak.mealient.ui.OperationUiState

View File

@@ -2,8 +2,8 @@ package gq.kirmanak.mealient.data.network
import gq.kirmanak.mealient.data.auth.AuthRepo import gq.kirmanak.mealient.data.auth.AuthRepo
import gq.kirmanak.mealient.data.baseurl.BaseURLStorage import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
import gq.kirmanak.mealient.datasource.MealieDataSource import gq.kirmanak.mealient.datasource.NetworkError
import gq.kirmanak.mealient.datasource.models.NetworkError import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_AUTH_HEADER import gq.kirmanak.mealient.test.AuthImplTestData.TEST_AUTH_HEADER
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_BASE_URL import gq.kirmanak.mealient.test.AuthImplTestData.TEST_BASE_URL
import gq.kirmanak.mealient.test.RecipeImplTestData.GET_CAKE_RESPONSE import gq.kirmanak.mealient.test.RecipeImplTestData.GET_CAKE_RESPONSE
@@ -18,7 +18,7 @@ import org.junit.Test
import java.io.IOException import java.io.IOException
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
class MealieDataSourceWrapperTest { class MealieDataSourceV0WrapperTest {
@MockK @MockK
lateinit var baseURLStorage: BaseURLStorage lateinit var baseURLStorage: BaseURLStorage
@@ -27,14 +27,14 @@ class MealieDataSourceWrapperTest {
lateinit var authRepo: AuthRepo lateinit var authRepo: AuthRepo
@MockK @MockK
lateinit var mealieDataSource: MealieDataSource lateinit var mealieDataSourceV0: MealieDataSourceV0
lateinit var subject: MealieDataSourceWrapper lateinit var subject: MealieDataSourceWrapper
@Before @Before
fun setUp() { fun setUp() {
MockKAnnotations.init(this) MockKAnnotations.init(this)
subject = MealieDataSourceWrapper(baseURLStorage, authRepo, mealieDataSource) subject = MealieDataSourceWrapper(baseURLStorage, authRepo, mealieDataSourceV0)
} }
@Test @Test
@@ -42,10 +42,10 @@ class MealieDataSourceWrapperTest {
coEvery { baseURLStorage.requireBaseURL() } returns TEST_BASE_URL coEvery { baseURLStorage.requireBaseURL() } returns TEST_BASE_URL
coEvery { authRepo.getAuthHeader() } returns null andThen TEST_AUTH_HEADER coEvery { authRepo.getAuthHeader() } returns null andThen TEST_AUTH_HEADER
coEvery { coEvery {
mealieDataSource.requestRecipeInfo(eq(TEST_BASE_URL), isNull(), eq("cake")) mealieDataSourceV0.requestRecipeInfo(eq(TEST_BASE_URL), isNull(), eq("cake"))
} throws NetworkError.Unauthorized(IOException()) } throws NetworkError.Unauthorized(IOException())
coEvery { 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 } returns GET_CAKE_RESPONSE
subject.requestRecipeInfo("cake") subject.requestRecipeInfo("cake")
coVerifyAll { coVerifyAll {

View File

@@ -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.db.RecipeStorage
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity 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.logging.Logger
import gq.kirmanak.mealient.test.RecipeImplTestData.TEST_RECIPE_SUMMARIES import gq.kirmanak.mealient.test.RecipeImplTestData.TEST_RECIPE_SUMMARIES
import io.mockk.MockKAnnotations import io.mockk.MockKAnnotations

View File

@@ -1,10 +1,10 @@
package gq.kirmanak.mealient.extensions package gq.kirmanak.mealient.extensions
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import gq.kirmanak.mealient.datasource.models.AddRecipeIngredient import gq.kirmanak.mealient.datasource.v0.models.AddRecipeIngredientV0
import gq.kirmanak.mealient.datasource.models.AddRecipeInstruction import gq.kirmanak.mealient.datasource.v0.models.AddRecipeInstructionV0
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
import gq.kirmanak.mealient.datasource.models.AddRecipeSettings import gq.kirmanak.mealient.datasource.v0.models.AddRecipeSettingsV0
import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft
import org.junit.Test import org.junit.Test
@@ -22,19 +22,19 @@ class RemoteToLocalMappingsTest {
areCommentsDisabled = true, areCommentsDisabled = true,
) )
val expected = AddRecipeRequest( val expected = AddRecipeRequestV0(
name = "Recipe name", name = "Recipe name",
description = "Recipe description", description = "Recipe description",
recipeYield = "Recipe yield", recipeYield = "Recipe yield",
recipeIngredient = listOf( recipeIngredient = listOf(
AddRecipeIngredient(note = "Recipe ingredient 1"), AddRecipeIngredientV0(note = "Recipe ingredient 1"),
AddRecipeIngredient(note = "Recipe ingredient 2") AddRecipeIngredientV0(note = "Recipe ingredient 2")
), ),
recipeInstructions = listOf( recipeInstructions = listOf(
AddRecipeInstruction(text = "Recipe instruction 1"), AddRecipeInstructionV0(text = "Recipe instruction 1"),
AddRecipeInstruction(text = "Recipe instruction 2") AddRecipeInstructionV0(text = "Recipe instruction 2")
), ),
settings = AddRecipeSettings( settings = AddRecipeSettingsV0(
public = false, public = false,
disableComments = true, disableComments = true,
) )
@@ -45,19 +45,19 @@ class RemoteToLocalMappingsTest {
@Test @Test
fun `when toDraft then fills fields correctly`() { fun `when toDraft then fills fields correctly`() {
val request = AddRecipeRequest( val request = AddRecipeRequestV0(
name = "Recipe name", name = "Recipe name",
description = "Recipe description", description = "Recipe description",
recipeYield = "Recipe yield", recipeYield = "Recipe yield",
recipeIngredient = listOf( recipeIngredient = listOf(
AddRecipeIngredient(note = "Recipe ingredient 1"), AddRecipeIngredientV0(note = "Recipe ingredient 1"),
AddRecipeIngredient(note = "Recipe ingredient 2") AddRecipeIngredientV0(note = "Recipe ingredient 2")
), ),
recipeInstructions = listOf( recipeInstructions = listOf(
AddRecipeInstruction(text = "Recipe instruction 1"), AddRecipeInstructionV0(text = "Recipe instruction 1"),
AddRecipeInstruction(text = "Recipe instruction 2") AddRecipeInstructionV0(text = "Recipe instruction 2")
), ),
settings = AddRecipeSettings( settings = AddRecipeSettingsV0(
public = false, public = false,
disableComments = true, disableComments = true,
) )

View File

@@ -1,15 +1,15 @@
package gq.kirmanak.mealient.test package gq.kirmanak.mealient.test
import gq.kirmanak.mealient.database.recipe.entity.* import gq.kirmanak.mealient.database.recipe.entity.*
import gq.kirmanak.mealient.datasource.models.GetRecipeIngredientResponse import gq.kirmanak.mealient.datasource.v0.models.GetRecipeIngredientResponseV0
import gq.kirmanak.mealient.datasource.models.GetRecipeInstructionResponse import gq.kirmanak.mealient.datasource.v0.models.GetRecipeInstructionResponseV0
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse import gq.kirmanak.mealient.datasource.v0.models.GetRecipeResponseV0
import gq.kirmanak.mealient.datasource.models.GetRecipeSummaryResponse import gq.kirmanak.mealient.datasource.v0.models.GetRecipeSummaryResponseV0
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime import kotlinx.datetime.LocalDateTime
object RecipeImplTestData { object RecipeImplTestData {
val RECIPE_SUMMARY_CAKE = GetRecipeSummaryResponse( val RECIPE_SUMMARY_CAKE = GetRecipeSummaryResponseV0(
remoteId = 1, remoteId = 1,
name = "Cake", name = "Cake",
slug = "cake", slug = "cake",
@@ -22,7 +22,7 @@ object RecipeImplTestData {
dateUpdated = LocalDateTime.parse("2021-11-13T15:30:13"), dateUpdated = LocalDateTime.parse("2021-11-13T15:30:13"),
) )
val RECIPE_SUMMARY_PORRIDGE = GetRecipeSummaryResponse( val RECIPE_SUMMARY_PORRIDGE = GetRecipeSummaryResponseV0(
remoteId = 2, remoteId = 2,
name = "Porridge", name = "Porridge",
slug = "porridge", slug = "porridge",
@@ -59,7 +59,7 @@ object RecipeImplTestData {
dateUpdated = LocalDateTime.parse("2021-10-13T17:35:23"), dateUpdated = LocalDateTime.parse("2021-10-13T17:35:23"),
) )
private val SUGAR_INGREDIENT = GetRecipeIngredientResponse( private val SUGAR_INGREDIENT = GetRecipeIngredientResponseV0(
title = "Sugar", title = "Sugar",
note = "2 oz of white sugar", note = "2 oz of white sugar",
unit = "", unit = "",
@@ -68,7 +68,7 @@ object RecipeImplTestData {
quantity = 1 quantity = 1
) )
val BREAD_INGREDIENT = GetRecipeIngredientResponse( val BREAD_INGREDIENT = GetRecipeIngredientResponseV0(
title = "Bread", title = "Bread",
note = "2 oz of white bread", note = "2 oz of white bread",
unit = "", unit = "",
@@ -77,7 +77,7 @@ object RecipeImplTestData {
quantity = 2 quantity = 2
) )
private val MILK_INGREDIENT = GetRecipeIngredientResponse( private val MILK_INGREDIENT = GetRecipeIngredientResponseV0(
title = "Milk", title = "Milk",
note = "2 oz of white milk", note = "2 oz of white milk",
unit = "", unit = "",
@@ -86,22 +86,22 @@ object RecipeImplTestData {
quantity = 3 quantity = 3
) )
val MIX_INSTRUCTION = GetRecipeInstructionResponse( val MIX_INSTRUCTION = GetRecipeInstructionResponseV0(
title = "Mix", title = "Mix",
text = "Mix the ingredients" text = "Mix the ingredients"
) )
private val BAKE_INSTRUCTION = GetRecipeInstructionResponse( private val BAKE_INSTRUCTION = GetRecipeInstructionResponseV0(
title = "Bake", title = "Bake",
text = "Bake the ingredients" text = "Bake the ingredients"
) )
private val BOIL_INSTRUCTION = GetRecipeInstructionResponse( private val BOIL_INSTRUCTION = GetRecipeInstructionResponseV0(
title = "Boil", title = "Boil",
text = "Boil the ingredients" text = "Boil the ingredients"
) )
val GET_CAKE_RESPONSE = GetRecipeResponse( val GET_CAKE_RESPONSE = GetRecipeResponseV0(
remoteId = 1, remoteId = 1,
name = "Cake", name = "Cake",
slug = "cake", slug = "cake",
@@ -117,7 +117,7 @@ object RecipeImplTestData {
recipeInstructions = listOf(MIX_INSTRUCTION, BAKE_INSTRUCTION) recipeInstructions = listOf(MIX_INSTRUCTION, BAKE_INSTRUCTION)
) )
val GET_PORRIDGE_RESPONSE = GetRecipeResponse( val GET_PORRIDGE_RESPONSE = GetRecipeResponseV0(
remoteId = 2, remoteId = 2,
name = "Porridge", name = "Porridge",
slug = "porridge", slug = "porridge",

View File

@@ -2,7 +2,7 @@ package gq.kirmanak.mealient.ui.add
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import gq.kirmanak.mealient.data.add.AddRecipeRepo 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 gq.kirmanak.mealient.logging.Logger
import io.mockk.MockKAnnotations import io.mockk.MockKAnnotations
import io.mockk.coEvery import io.mockk.coEvery
@@ -61,21 +61,21 @@ class AddRecipeViewModelTest {
@Test @Test
fun `when preserve then doesn't update UI`() { fun `when preserve then doesn't update UI`() {
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeRequest()) coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeRequestV0())
subject.preserve(AddRecipeRequest()) subject.preserve(AddRecipeRequestV0())
coVerify(inverse = true) { addRecipeRepo.addRecipeRequestFlow } coVerify(inverse = true) { addRecipeRepo.addRecipeRequestFlow }
} }
@Test @Test
fun `when preservedAddRecipeRequest without loadPreservedRequest then empty`() = runTest { 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() } val actual = withTimeoutOrNull(10) { subject.preservedAddRecipeRequest.firstOrNull() }
assertThat(actual).isNull() assertThat(actual).isNull()
} }
@Test @Test
fun `when loadPreservedRequest then updates preservedAddRecipeRequest`() = runTest { fun `when loadPreservedRequest then updates preservedAddRecipeRequest`() = runTest {
val expected = AddRecipeRequest() val expected = AddRecipeRequestV0()
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected) coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected)
subject.loadPreservedRequest() subject.loadPreservedRequest()
assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected) assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected)
@@ -83,7 +83,7 @@ class AddRecipeViewModelTest {
@Test @Test
fun `when clear then updates preservedAddRecipeRequest`() = runTest { fun `when clear then updates preservedAddRecipeRequest`() = runTest {
val expected = AddRecipeRequest() val expected = AddRecipeRequestV0()
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected) coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected)
subject.clear() subject.clear()
assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected) assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected)

View File

@@ -6,6 +6,9 @@ import dagger.Module
import dagger.Provides import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent 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.MealieDataSourceV1
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1Impl import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1Impl
import gq.kirmanak.mealient.datasource.v1.MealieServiceV1 import gq.kirmanak.mealient.datasource.v1.MealieServiceV1
@@ -52,7 +55,7 @@ interface DataSourceModule {
@Provides @Provides
@Singleton @Singleton
fun provideMealieService(retrofit: Retrofit): MealieService = fun provideMealieService(retrofit: Retrofit): MealieServiceV0 =
retrofit.create() retrofit.create()
@Provides @Provides
@@ -71,7 +74,7 @@ interface DataSourceModule {
@Binds @Binds
@Singleton @Singleton
fun bindMealieDataSource(mealientDataSourceImpl: MealieDataSourceImpl): MealieDataSource fun bindMealieDataSource(mealientDataSourceImpl: MealieDataSourceV0Impl): MealieDataSourceV0
@Binds @Binds
@Singleton @Singleton

View File

@@ -1,4 +1,4 @@
package gq.kirmanak.mealient.datasource.models package gq.kirmanak.mealient.datasource
sealed class NetworkError(cause: Throwable) : RuntimeException(cause) { sealed class NetworkError(cause: Throwable) : RuntimeException(cause) {
class Unauthorized(cause: Throwable) : NetworkError(cause) class Unauthorized(cause: Throwable) : NetworkError(cause)

View File

@@ -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,
)

View File

@@ -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)

View File

@@ -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)

View File

@@ -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,
)

View File

@@ -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.v0.models.AddRecipeRequestV0
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse import gq.kirmanak.mealient.datasource.v0.models.GetRecipeResponseV0
import gq.kirmanak.mealient.datasource.models.GetRecipeSummaryResponse import gq.kirmanak.mealient.datasource.v0.models.GetRecipeSummaryResponseV0
import gq.kirmanak.mealient.datasource.models.VersionResponse import gq.kirmanak.mealient.datasource.v0.models.VersionResponseV0
interface MealieDataSource { interface MealieDataSourceV0 {
suspend fun addRecipe( suspend fun addRecipe(
baseUrl: String, baseUrl: String,
token: String?, token: String?,
recipe: AddRecipeRequest, recipe: AddRecipeRequestV0,
): String ): String
/** /**
@@ -24,18 +24,18 @@ interface MealieDataSource {
suspend fun getVersionInfo( suspend fun getVersionInfo(
baseUrl: String, baseUrl: String,
): VersionResponse ): VersionResponseV0
suspend fun requestRecipes( suspend fun requestRecipes(
baseUrl: String, baseUrl: String,
token: String?, token: String?,
start: Int, start: Int,
limit: Int, limit: Int,
): List<GetRecipeSummaryResponse> ): List<GetRecipeSummaryResponseV0>
suspend fun requestRecipeInfo( suspend fun requestRecipeInfo(
baseUrl: String, baseUrl: String,
token: String?, token: String?,
slug: String, slug: String,
): GetRecipeResponse ): GetRecipeResponseV0
} }

View File

@@ -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 gq.kirmanak.mealient.logging.Logger
import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationException import kotlinx.serialization.SerializationException
@@ -14,14 +15,14 @@ import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class MealieDataSourceImpl @Inject constructor( class MealieDataSourceV0Impl @Inject constructor(
private val logger: Logger, private val logger: Logger,
private val mealieService: MealieService, private val mealieServiceV0: MealieServiceV0,
private val json: Json, private val json: Json,
) : MealieDataSource { ) : MealieDataSourceV0 {
override suspend fun addRecipe( override suspend fun addRecipe(
baseUrl: String, token: String?, recipe: AddRecipeRequest baseUrl: String, token: String?, recipe: AddRecipeRequestV0
): String = makeCall( ): String = makeCall(
block = { addRecipe("$baseUrl/api/recipes/create", token, recipe) }, block = { addRecipe("$baseUrl/api/recipes/create", token, recipe) },
logMethod = { "addRecipe" }, logMethod = { "addRecipe" },
@@ -36,11 +37,11 @@ class MealieDataSourceImpl @Inject constructor(
logParameters = { "baseUrl = $baseUrl, username = $username, password = $password" } logParameters = { "baseUrl = $baseUrl, username = $username, password = $password" }
).map { it.accessToken }.getOrElse { ).map { it.accessToken }.getOrElse {
val errorBody = (it as? HttpException)?.response()?.errorBody() ?: throw it val errorBody = (it as? HttpException)?.response()?.errorBody() ?: throw it
val errorDetail = errorBody.decode<ErrorDetail>() val errorDetailV0 = errorBody.decode<ErrorDetailV0>()
throw if (errorDetail.detail == "Unauthorized") NetworkError.Unauthorized(it) else it 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") }, block = { getVersion("$baseUrl/api/debug/version") },
logMethod = { "getVersionInfo" }, logMethod = { "getVersionInfo" },
logParameters = { "baseUrl = $baseUrl" }, logParameters = { "baseUrl = $baseUrl" },
@@ -56,7 +57,7 @@ class MealieDataSourceImpl @Inject constructor(
override suspend fun requestRecipes( override suspend fun requestRecipes(
baseUrl: String, token: String?, start: Int, limit: Int baseUrl: String, token: String?, start: Int, limit: Int
): List<GetRecipeSummaryResponse> = makeCall( ): List<GetRecipeSummaryResponseV0> = makeCall(
block = { getRecipeSummary("$baseUrl/api/recipes/summary", token, start, limit) }, block = { getRecipeSummary("$baseUrl/api/recipes/summary", token, start, limit) },
logMethod = { "requestRecipes" }, logMethod = { "requestRecipes" },
logParameters = { "baseUrl = $baseUrl, token = $token, start = $start, limit = $limit" } logParameters = { "baseUrl = $baseUrl, token = $token, start = $start, limit = $limit" }
@@ -67,19 +68,19 @@ class MealieDataSourceImpl @Inject constructor(
override suspend fun requestRecipeInfo( override suspend fun requestRecipeInfo(
baseUrl: String, token: String?, slug: String baseUrl: String, token: String?, slug: String
): GetRecipeResponse = makeCall( ): GetRecipeResponseV0 = makeCall(
block = { getRecipe("$baseUrl/api/recipes/$slug", token) }, block = { getRecipe("$baseUrl/api/recipes/$slug", token) },
logMethod = { "requestRecipeInfo" }, logMethod = { "requestRecipeInfo" },
logParameters = { "baseUrl = $baseUrl, token = $token, slug = $slug" } logParameters = { "baseUrl = $baseUrl, token = $token, slug = $slug" }
).getOrThrowUnauthorized() ).getOrThrowUnauthorized()
private suspend inline fun <T> makeCall( private suspend inline fun <T> makeCall(
crossinline block: suspend MealieService.() -> T, crossinline block: suspend MealieServiceV0.() -> T,
crossinline logMethod: () -> String, crossinline logMethod: () -> String,
crossinline logParameters: () -> String, crossinline logParameters: () -> String,
): Result<T> { ): Result<T> {
logger.v { "${logMethod()} called with: ${logParameters()}" } logger.v { "${logMethod()} called with: ${logParameters()}" }
return mealieService.runCatching { block() } return mealieServiceV0.runCatching { block() }
.onFailure { logger.e(it) { "${logMethod()} request failed with: ${logParameters()}" } } .onFailure { logger.e(it) { "${logMethod()} request failed with: ${logParameters()}" } }
.onSuccess { logger.d { "${logMethod()} request succeeded with ${logParameters()}" } } .onSuccess { logger.d { "${logMethod()} request succeeded with ${logParameters()}" } }
} }

View File

@@ -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.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
import gq.kirmanak.mealient.datasource.models.* import gq.kirmanak.mealient.datasource.v0.models.*
import retrofit2.http.* import retrofit2.http.*
interface MealieService { interface MealieServiceV0 {
@FormUrlEncoded @FormUrlEncoded
@POST @POST
@@ -12,19 +12,19 @@ interface MealieService {
@Url url: String, @Url url: String,
@Field("username") username: String, @Field("username") username: String,
@Field("password") password: String, @Field("password") password: String,
): GetTokenResponse ): GetTokenResponseV0
@POST @POST
suspend fun addRecipe( suspend fun addRecipe(
@Url url: String, @Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?, @Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Body addRecipeRequest: AddRecipeRequest, @Body addRecipeRequestV0: AddRecipeRequestV0,
): String ): String
@GET @GET
suspend fun getVersion( suspend fun getVersion(
@Url url: String, @Url url: String,
): VersionResponse ): VersionResponseV0
@GET @GET
suspend fun getRecipeSummary( suspend fun getRecipeSummary(
@@ -32,11 +32,11 @@ interface MealieService {
@Header(AUTHORIZATION_HEADER_NAME) token: String?, @Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Query("start") start: Int, @Query("start") start: Int,
@Query("limit") limit: Int, @Query("limit") limit: Int,
): List<GetRecipeSummaryResponse> ): List<GetRecipeSummaryResponseV0>
@GET @GET
suspend fun getRecipe( suspend fun getRecipe(
@Url url: String, @Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?, @Header(AUTHORIZATION_HEADER_NAME) token: String?,
): GetRecipeResponse ): GetRecipeResponseV0
} }

View File

@@ -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,
)

View File

@@ -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 = "",
)

View File

@@ -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 = "",
)

View File

@@ -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(),
)

View File

@@ -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,
)

View File

@@ -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)

View File

@@ -1,10 +1,10 @@
package gq.kirmanak.mealient.datasource.models package gq.kirmanak.mealient.datasource.v0.models
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
data class GetRecipeIngredientResponse( data class GetRecipeIngredientResponseV0(
@SerialName("title") val title: String = "", @SerialName("title") val title: String = "",
@SerialName("note") val note: String = "", @SerialName("note") val note: String = "",
@SerialName("unit") val unit: String = "", @SerialName("unit") val unit: String = "",

View File

@@ -1,10 +1,10 @@
package gq.kirmanak.mealient.datasource.models package gq.kirmanak.mealient.datasource.v0.models
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
data class GetRecipeInstructionResponse( data class GetRecipeInstructionResponseV0(
@SerialName("title") val title: String = "", @SerialName("title") val title: String = "",
@SerialName("text") val text: String, @SerialName("text") val text: String,
) )

View File

@@ -1,4 +1,4 @@
package gq.kirmanak.mealient.datasource.models package gq.kirmanak.mealient.datasource.v0.models
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime import kotlinx.datetime.LocalDateTime
@@ -6,7 +6,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
data class GetRecipeResponse( data class GetRecipeResponseV0(
@SerialName("id") val remoteId: Int, @SerialName("id") val remoteId: Int,
@SerialName("name") val name: String, @SerialName("name") val name: String,
@SerialName("slug") val slug: String, @SerialName("slug") val slug: String,
@@ -18,6 +18,6 @@ data class GetRecipeResponse(
@SerialName("dateAdded") val dateAdded: LocalDate, @SerialName("dateAdded") val dateAdded: LocalDate,
@SerialName("dateUpdated") val dateUpdated: LocalDateTime, @SerialName("dateUpdated") val dateUpdated: LocalDateTime,
@SerialName("recipeYield") val recipeYield: String = "", @SerialName("recipeYield") val recipeYield: String = "",
@SerialName("recipeIngredient") val recipeIngredients: List<GetRecipeIngredientResponse>, @SerialName("recipeIngredient") val recipeIngredients: List<GetRecipeIngredientResponseV0>,
@SerialName("recipeInstructions") val recipeInstructions: List<GetRecipeInstructionResponse>, @SerialName("recipeInstructions") val recipeInstructions: List<GetRecipeInstructionResponseV0>,
) )

View File

@@ -1,4 +1,4 @@
package gq.kirmanak.mealient.datasource.models package gq.kirmanak.mealient.datasource.v0.models
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime import kotlinx.datetime.LocalDateTime
@@ -6,7 +6,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
data class GetRecipeSummaryResponse( data class GetRecipeSummaryResponseV0(
@SerialName("id") val remoteId: Int, @SerialName("id") val remoteId: Int,
@SerialName("name") val name: String, @SerialName("name") val name: String,
@SerialName("slug") val slug: String, @SerialName("slug") val slug: String,

View File

@@ -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)

View File

@@ -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,
)

View File

@@ -1,6 +1,6 @@
package gq.kirmanak.mealient.datasource.v1 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.GetRecipeResponseV1
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1 import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1 import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
@@ -10,7 +10,7 @@ interface MealieDataSourceV1 {
suspend fun addRecipe( suspend fun addRecipe(
baseUrl: String, baseUrl: String,
token: String?, token: String?,
recipe: AddRecipeRequest, recipe: AddRecipeRequestV0,
): String ): String
/** /**

View File

@@ -1,7 +1,7 @@
package gq.kirmanak.mealient.datasource.v1 package gq.kirmanak.mealient.datasource.v1
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest import gq.kirmanak.mealient.datasource.NetworkError
import gq.kirmanak.mealient.datasource.models.NetworkError import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1 import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1 import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1 import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
@@ -24,7 +24,7 @@ class MealieDataSourceV1Impl @Inject constructor(
override suspend fun addRecipe( override suspend fun addRecipe(
baseUrl: String, baseUrl: String,
token: String?, token: String?,
recipe: AddRecipeRequest recipe: AddRecipeRequestV0
): String { ): String {
TODO("Not yet implemented") TODO("Not yet implemented")
} }

View File

@@ -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.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
import gq.kirmanak.mealient.datasource.models.* 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.GetRecipeResponseV1
import gq.kirmanak.mealient.datasource.v1.models.GetRecipesResponseV1 import gq.kirmanak.mealient.datasource.v1.models.GetRecipesResponseV1
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1 import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
@@ -15,13 +17,13 @@ interface MealieServiceV1 {
@Url url: String, @Url url: String,
@Field("username") username: String, @Field("username") username: String,
@Field("password") password: String, @Field("password") password: String,
): GetTokenResponse ): GetTokenResponseV0
@POST @POST
suspend fun addRecipe( suspend fun addRecipe(
@Url url: String, @Url url: String,
@Header(AUTHORIZATION_HEADER_NAME) token: String?, @Header(AUTHORIZATION_HEADER_NAME) token: String?,
@Body addRecipeRequest: AddRecipeRequest, @Body addRecipeRequestV0: AddRecipeRequestV0,
): String ): String
@GET @GET

View File

@@ -1,9 +1,10 @@
package gq.kirmanak.mealient.datasource package gq.kirmanak.mealient.datasource
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import gq.kirmanak.mealient.datasource.models.GetTokenResponse import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0Impl
import gq.kirmanak.mealient.datasource.models.NetworkError import gq.kirmanak.mealient.datasource.v0.MealieServiceV0
import gq.kirmanak.mealient.datasource.models.VersionResponse 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.logging.Logger
import gq.kirmanak.mealient.test.toJsonResponseBody import gq.kirmanak.mealient.test.toJsonResponseBody
import io.mockk.MockKAnnotations import io.mockk.MockKAnnotations
@@ -21,25 +22,25 @@ import java.io.IOException
import java.net.ConnectException import java.net.ConnectException
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
class MealieDataSourceImplTest { class MealieDataSourceV0ImplTest {
@MockK @MockK
lateinit var service: MealieService lateinit var service: MealieServiceV0
@MockK(relaxUnitFun = true) @MockK(relaxUnitFun = true)
lateinit var logger: Logger lateinit var logger: Logger
lateinit var subject: MealieDataSourceImpl lateinit var subject: MealieDataSourceV0Impl
@Before @Before
fun setUp() { fun setUp() {
MockKAnnotations.init(this) MockKAnnotations.init(this)
subject = MealieDataSourceImpl(logger, service, Json.Default) subject = MealieDataSourceV0Impl(logger, service, Json)
} }
@Test(expected = NetworkError.NotMealie::class) @Test(expected = NetworkError.NotMealie::class)
fun `when getVersionInfo and getVersion throws HttpException then NotMealie`() = runTest { 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 coEvery { service.getVersion(any()) } throws error
subject.getVersionInfo(TEST_BASE_URL) subject.getVersionInfo(TEST_BASE_URL)
} }
@@ -60,14 +61,14 @@ class MealieDataSourceImplTest {
@Test @Test
fun `when getVersionInfo and getVersion returns result then result`() = runTest { 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 coEvery { service.getVersion(any()) } returns versionResponse
assertThat(subject.getVersionInfo(TEST_BASE_URL)).isSameInstanceAs(versionResponse) assertThat(subject.getVersionInfo(TEST_BASE_URL)).isSameInstanceAs(versionResponse)
} }
@Test @Test
fun `when authentication is successful then token is correct`() = runTest { 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) assertThat(callAuthenticate()).isEqualTo(TEST_TOKEN)
} }
@@ -76,7 +77,7 @@ class MealieDataSourceImplTest {
val body = "{\"detail\":\"Unauthorized\"}".toJsonResponseBody() val body = "{\"detail\":\"Unauthorized\"}".toJsonResponseBody()
coEvery { coEvery {
service.getToken(any(), any(), any()) service.getToken(any(), any(), any())
} throws HttpException(Response.error<GetTokenResponse>(401, body)) } throws HttpException(Response.error<GetTokenResponseV0>(401, body))
callAuthenticate() callAuthenticate()
} }
@@ -85,7 +86,7 @@ class MealieDataSourceImplTest {
val body = "{\"detail\":\"Something\"}".toJsonResponseBody() val body = "{\"detail\":\"Something\"}".toJsonResponseBody()
coEvery { coEvery {
service.getToken(any(), any(), any()) service.getToken(any(), any(), any())
} throws HttpException(Response.error<GetTokenResponse>(401, body)) } throws HttpException(Response.error<GetTokenResponseV0>(401, body))
callAuthenticate() callAuthenticate()
} }
@@ -94,7 +95,7 @@ class MealieDataSourceImplTest {
val body = "".toJsonResponseBody() val body = "".toJsonResponseBody()
coEvery { coEvery {
service.getToken(any(), any(), any()) service.getToken(any(), any(), any())
} throws HttpException(Response.error<GetTokenResponse>(401, body)) } throws HttpException(Response.error<GetTokenResponseV0>(401, body))
callAuthenticate() callAuthenticate()
} }