Skip to content

Commit

Permalink
✨ implement editing recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Hogu59 committed Oct 24, 2024
1 parent 7e94f74 commit 9be5079
Show file tree
Hide file tree
Showing 32 changed files with 1,912 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package net.pengcook.android.data.datasource.edit

object EditRecipeCacheDataSource {
private var savedRecipeDescription: SavedRecipeDescription? = null

var savedRecipeSteps: List<SavedRecipeSteps> = listOf()
private set

fun fetchSavedRecipeDescription(): SavedRecipeDescription {
val data = savedRecipeDescription
check(data != null) { "Saved recipe description is not available" }
return data
}

fun saveRecipeDescription(changedRecipeDescription: SavedRecipeDescription) {
this.savedRecipeDescription = changedRecipeDescription
}

fun clearSavedRecipeDescription() {
savedRecipeDescription = null
}

fun saveRecipeSteps(savedRecipeSteps: List<SavedRecipeSteps>) {
this.savedRecipeSteps = savedRecipeSteps
}

fun clearSavedRecipeSteps() {
savedRecipeSteps = emptyList()
}
}

data class SavedRecipeDescription(
val id: Long,
val title: String,
val imageUri: String,
val description: String,
val cookingTime: String,
val categories: List<String>,
val ingredients: List<SavedIngredient>,
val difficulty: Int,
val thumbnail: String,
)

data class SavedIngredient(
val name: String,
val requirement: String = "REQUIRED",
val substitutions: List<String> = emptyList(),
)

data class SavedRecipeSteps(
val sequence: Int,
val imageUri: String?,
val imageTitle: String?,
val cookingTime: String?,
val description: String?,
val imageUploaded: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.pengcook.android.data.datasource.feed

import net.pengcook.android.data.model.feed.item.FeedItemResponse2
import net.pengcook.android.data.model.feed.item.FeedItemResponseForList
import net.pengcook.android.data.model.feed.item.RecipeEditRequest
import net.pengcook.android.data.model.step.RecipeStepResponse
import net.pengcook.android.data.remote.api.FeedService
import retrofit2.Response
Expand Down Expand Up @@ -43,4 +44,10 @@ class DefaultFeedRemoteDataSource
accessToken: String,
recipeId: Long,
): Response<FeedItemResponse2> = feedService.fetchRecipe(accessToken, recipeId)

override suspend fun updateRecipe(
accessToken: String,
recipeId: Long,
recipeEditRequest: RecipeEditRequest,
): Response<Unit> = feedService.updateRecipe(accessToken, recipeId, recipeEditRequest)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.pengcook.android.data.datasource.feed

import net.pengcook.android.data.model.feed.item.FeedItemResponse2
import net.pengcook.android.data.model.feed.item.FeedItemResponseForList
import net.pengcook.android.data.model.feed.item.RecipeEditRequest
import net.pengcook.android.data.model.step.RecipeStepResponse
import retrofit2.Response

Expand All @@ -26,4 +27,10 @@ interface FeedRemoteDataSource {
accessToken: String,
recipeId: Long,
): Response<FeedItemResponse2>

suspend fun updateRecipe(
accessToken: String,
recipeId: Long,
recipeEditRequest: RecipeEditRequest,
): Response<Unit>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.pengcook.android.data.model.feed.item

import net.pengcook.android.data.model.makingrecipe.request.IngredientRequest
import net.pengcook.android.data.model.makingrecipe.request.RecipeStepRequest

data class RecipeEditRequest(
val title: String,
val cookingTime: String,
val thumbnail: String,
val difficulty: Int,
val description: String,
val categories: List<String>,
val ingredients: List<IngredientRequest>,
val recipeSteps: List<RecipeStepRequest>,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.pengcook.android.data.model.step

data class RecipeStepResponse(
val stepId: Long,
val id: Long,
val description: String,
val image: String,
val recipeId: Long,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package net.pengcook.android.data.remote.api
import net.pengcook.android.data.model.feed.item.FeedItemResponse
import net.pengcook.android.data.model.feed.item.FeedItemResponse2
import net.pengcook.android.data.model.feed.item.FeedItemResponseForList
import net.pengcook.android.data.model.feed.item.RecipeEditRequest
import net.pengcook.android.data.model.step.RecipeStepResponse
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.Query

Expand Down Expand Up @@ -49,4 +52,11 @@ interface FeedService {
@Header("Authorization") accessToken: String,
@Path("recipeId") recipeId: Long,
): Response<FeedItemResponse2>

@PUT("/recipes/{recipeId}")
suspend fun updateRecipe(
@Header("Authorization") accessToken: String,
@Path("recipeId") recipeId: Long,
@Body recipeEditRequest: RecipeEditRequest,
): Response<Unit>
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.pengcook.android.data.remote.api

import net.pengcook.android.data.model.makingrecipe.request.RecipeStepRequest
import net.pengcook.android.data.model.step.RecipeStepResponse
import net.pengcook.android.data.model.step.request.RecipeStepRequest
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package net.pengcook.android.data.repository.edit

import net.pengcook.android.data.datasource.edit.EditRecipeCacheDataSource
import net.pengcook.android.data.datasource.edit.SavedIngredient
import net.pengcook.android.data.datasource.edit.SavedRecipeDescription
import net.pengcook.android.data.datasource.edit.SavedRecipeSteps
import net.pengcook.android.domain.model.recipemaking.RecipeCreation
import net.pengcook.android.presentation.core.model.Ingredient
import net.pengcook.android.presentation.core.model.RecipeStepMaking

object EditRecipeRepository {
fun saveRecipe(recipeCreation: RecipeCreation): Result<Unit> =
runCatching {
EditRecipeCacheDataSource.saveRecipeDescription(recipeCreation.toSavedRecipeDescription())
EditRecipeCacheDataSource.saveRecipeSteps(recipeCreation.steps.map { it.toSavedRecipeStep() })
}

fun saveRecipeSteps(recipeSteps: List<RecipeStepMaking>): Result<Unit> =
runCatching {
EditRecipeCacheDataSource.saveRecipeSteps(recipeSteps.map { it.toSavedRecipeStep() })
}

fun saveRecipeDescription(recipeDescription: RecipeCreation): Result<Unit> =
runCatching {
EditRecipeCacheDataSource.saveRecipeDescription(recipeDescription.toSavedRecipeDescription())
}

fun fetchAllSavedRecipeData(): Result<RecipeCreation> =
runCatching {
val description = EditRecipeCacheDataSource.fetchSavedRecipeDescription()
val steps = EditRecipeCacheDataSource.savedRecipeSteps
println("description: $description")
println("steps: $steps")
RecipeCreation(
title = description.title,
introduction = description.description,
cookingTime = description.cookingTime,
difficulty = description.difficulty,
ingredients =
description.ingredients.map {
Ingredient(
ingredientId = 1L,
ingredientName = it.name,
requirement = it.requirement,
)
},
categories = description.categories,
thumbnail = description.thumbnail,
steps =
steps.mapIndexed { index, it ->
RecipeStepMaking(
stepId = index.toLong(),
recipeId = description.id,
description = it.description ?: "",
image = it.imageUri ?: "",
sequence = it.sequence,
imageUri = it.imageUri ?: "",
cookingTime = it.cookingTime ?: "00:00:00",
imageUploaded = it.imageUploaded,
)
},
)
}

// fun fetchSavedRecipeSteps(): Result<List<RecipeStep>> = runCatching {
// EditRecipeCacheDataSource.savedRecipeSteps.map {
// RecipeStep(
// stepId = it.sequence.toLong(),
// description = it.description ?: "",
// image = it.imageUri ?: "",
// sequence = it.sequence,
// image = it.imageUri ?: "",
// cookingTime = it.cookingTime ?: "00:00:00",
// imageUploaded = it.imageUploaded,
// )
// }
// }
//

fun clearSavedRecipeData() {
EditRecipeCacheDataSource.clearSavedRecipeDescription()
EditRecipeCacheDataSource.clearSavedRecipeSteps()
}

private fun RecipeCreation.toSavedRecipeDescription(): SavedRecipeDescription =
SavedRecipeDescription(
id = 1L,
title = title,
imageUri = "",
description = introduction,
cookingTime = cookingTime,
categories = categories,
ingredients =
ingredients.map {
SavedIngredient(
name = it.ingredientName,
)
},
difficulty = difficulty,
thumbnail = thumbnail,
)

private fun RecipeStepMaking.toSavedRecipeStep(): SavedRecipeSteps =
SavedRecipeSteps(
sequence = sequence,
imageUri = imageUri,
imageTitle = "",
cookingTime = cookingTime,
description = description,
imageUploaded = imageUploaded,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import net.pengcook.android.data.datasource.auth.SessionLocalDataSource
import net.pengcook.android.data.datasource.feed.FeedRemoteDataSource
import net.pengcook.android.data.model.feed.item.FeedItemResponseForList
import net.pengcook.android.data.model.step.RecipeStepResponse
import net.pengcook.android.data.util.mapper.toRecipeEditRequest
import net.pengcook.android.data.util.mapper.toRecipeForItem
import net.pengcook.android.data.util.mapper.toRecipeForList
import net.pengcook.android.data.util.mapper.toRecipeStep
import net.pengcook.android.data.util.network.NetworkResponseHandler
import net.pengcook.android.presentation.core.model.ChangedRecipe
import net.pengcook.android.presentation.core.model.RecipeForItem
import net.pengcook.android.presentation.core.model.RecipeForList
import net.pengcook.android.presentation.core.model.RecipeStep
Expand Down Expand Up @@ -67,6 +69,16 @@ class DefaultFeedRepository
body(response, RESPONSE_CODE_SUCCESS).toRecipeForItem()
}

override suspend fun updateRecipe(
recipeId: Long,
changedRecipe: ChangedRecipe,
): Result<Unit> =
kotlin.runCatching {
val accessToken = sessionLocalDataSource.sessionData.first().accessToken ?: throw RuntimeException()
val response = feedRemoteDataSource.updateRecipe(accessToken, recipeId, changedRecipe.toRecipeEditRequest())
body(response, RESPONSE_CODE_SUCCESS)
}

companion object {
private const val RESPONSE_CODE_SUCCESS = 200
private const val RESPONSE_CODE_DELETED = 204
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.pengcook.android.data.repository.feed

import net.pengcook.android.presentation.core.model.ChangedRecipe
import net.pengcook.android.presentation.core.model.RecipeForItem
import net.pengcook.android.presentation.core.model.RecipeForList
import net.pengcook.android.presentation.core.model.RecipeStep
Expand All @@ -18,4 +19,9 @@ interface FeedRepository {
suspend fun deleteRecipe(recipeId: Long): Result<Unit>

suspend fun fetchRecipe(recipeId: Long): Result<RecipeForItem>

suspend fun updateRecipe(
recipeId: Long,
changedRecipe: ChangedRecipe,
): Result<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import net.pengcook.android.data.model.feed.item.CategoryResponse
import net.pengcook.android.data.model.feed.item.FeedItemResponse2
import net.pengcook.android.data.model.feed.item.FeedItemResponseForList
import net.pengcook.android.data.model.feed.item.IngredientResponse
import net.pengcook.android.data.model.feed.item.RecipeEditRequest
import net.pengcook.android.data.model.makingrecipe.request.RecipeStepRequest
import net.pengcook.android.data.model.step.RecipeStepResponse
import net.pengcook.android.presentation.core.model.ChangedRecipe
import net.pengcook.android.presentation.core.model.Ingredient
import net.pengcook.android.presentation.core.model.RecipeForItem
import net.pengcook.android.presentation.core.model.RecipeForList
Expand Down Expand Up @@ -41,6 +44,18 @@ fun FeedItemResponse2.toRecipeForItem(): RecipeForItem =
isLiked = isLike,
)

fun ChangedRecipe.toRecipeEditRequest(): RecipeEditRequest =
RecipeEditRequest(
title = title,
cookingTime = cookingTime,
thumbnail = thumbnail,
difficulty = difficulty,
description = description,
categories = categories,
ingredients = ingredients.map { it.toIngredientRequest() },
recipeSteps = recipeSteps.map { it.toRecipeStepRequest() },
)

// fun FeedItemResponse.toRecipe(): Recipe =
// Recipe(
// title = title,
Expand All @@ -59,7 +74,7 @@ fun FeedItemResponse2.toRecipeForItem(): RecipeForItem =

fun RecipeStepResponse.toRecipeStep(): RecipeStep =
RecipeStep(
stepId = stepId,
stepId = id,
recipeId = recipeId,
description = description,
image = image,
Expand All @@ -82,3 +97,11 @@ private fun IngredientResponse.toIngredient(): Ingredient =
ingredientName = ingredientName,
requirement = requirement,
)

private fun RecipeStep.toRecipeStepRequest(): RecipeStepRequest =
RecipeStepRequest(
cookingTime = cookingTime,
description = description,
image = image,
sequence = sequence,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.pengcook.android.presentation.core.model

data class ChangedRecipe(
val title: String,
val cookingTime: String,
val thumbnail: String,
val difficulty: Int,
val description: String,
val categories: List<String>,
val ingredients: List<Ingredient>,
val recipeSteps: List<RecipeStep>,
)
Loading

0 comments on commit 9be5079

Please sign in to comment.