From 536b6930852a2456426f1678bcfc742aa7029881 Mon Sep 17 00:00:00 2001 From: LilleBRG Date: Wed, 7 May 2025 13:51:54 +0200 Subject: [PATCH] create dish done but not checked with api --- app/.idea/codeStyles/Project.xml | 1 + .../tech/mercantec/easyeat/helpers/auth.kt | 11 ++- .../mercantec/easyeat/models/Ingredient.kt | 7 -- .../tech/mercantec/easyeat/models/recipe.kt | 24 +++++++ .../easyeat/ui/dishes/CreateDishActivity.kt | 70 +++++++++++++++++-- backend/API/Controllers/RecipeController.cs | 9 ++- 6 files changed, 104 insertions(+), 18 deletions(-) delete mode 100644 app/app/src/main/java/tech/mercantec/easyeat/models/Ingredient.kt create mode 100644 app/app/src/main/java/tech/mercantec/easyeat/models/recipe.kt diff --git a/app/.idea/codeStyles/Project.xml b/app/.idea/codeStyles/Project.xml index 7643783..ab75be2 100644 --- a/app/.idea/codeStyles/Project.xml +++ b/app/.idea/codeStyles/Project.xml @@ -1,6 +1,7 @@ + diff --git a/app/app/src/main/java/tech/mercantec/easyeat/helpers/auth.kt b/app/app/src/main/java/tech/mercantec/easyeat/helpers/auth.kt index 7d483c0..22bf240 100644 --- a/app/app/src/main/java/tech/mercantec/easyeat/helpers/auth.kt +++ b/app/app/src/main/java/tech/mercantec/easyeat/helpers/auth.kt @@ -3,7 +3,7 @@ package tech.mercantec.easyeat.helpers import android.content.Context import android.util.Log import kotlinx.serialization.Serializable - +import tech.mercantec.easyeat.models.Recipe @Serializable data class LoginRequest(val emailUsr: String, val password: String) @@ -109,3 +109,12 @@ fun changePassword(ctx: Context, oldPassword: String, newPassword: String) { return requestJson(ctx, "PUT", "/api/User/change-password", request) } +@Serializable +data class CreateRecipeRequest(val recipe: Recipe) + +fun createRecipe(ctx: Context, recipe: Recipe) { + val request = CreateRecipeRequest(recipe) + + return requestJson(ctx, "POST", "/api/recipe/create", request) + +} diff --git a/app/app/src/main/java/tech/mercantec/easyeat/models/Ingredient.kt b/app/app/src/main/java/tech/mercantec/easyeat/models/Ingredient.kt deleted file mode 100644 index 7413734..0000000 --- a/app/app/src/main/java/tech/mercantec/easyeat/models/Ingredient.kt +++ /dev/null @@ -1,7 +0,0 @@ -package tech.mercantec.easyeat.models - -data class Ingredient( - val Amount: Int, - val Unit: String, - val Element: String -) \ No newline at end of file diff --git a/app/app/src/main/java/tech/mercantec/easyeat/models/recipe.kt b/app/app/src/main/java/tech/mercantec/easyeat/models/recipe.kt new file mode 100644 index 0000000..1215baa --- /dev/null +++ b/app/app/src/main/java/tech/mercantec/easyeat/models/recipe.kt @@ -0,0 +1,24 @@ +package tech.mercantec.easyeat.models + +import kotlinx.serialization.Serializable + +@Serializable +data class Recipe( + val name: String, + val description: String, + val directions: List, + val ingredients: List +) + +@Serializable +data class Direction( + val instructions: String +) + + +@Serializable +data class Ingredient( + val amount: Double?, + val unit: String?, + val name: String +) diff --git a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishActivity.kt b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishActivity.kt index 52666df..a6d0463 100644 --- a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishActivity.kt +++ b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/CreateDishActivity.kt @@ -1,5 +1,7 @@ package tech.mercantec.easyeat.ui.dishes +import android.app.ProgressDialog +import android.content.Context import android.os.Bundle import android.util.Log import android.view.LayoutInflater @@ -9,9 +11,18 @@ import android.widget.EditText import android.widget.ImageButton import android.widget.LinearLayout import android.widget.Spinner +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContentProviderCompat.requireContext import tech.mercantec.easyeat.R +import tech.mercantec.easyeat.helpers.ApiRequestException +import tech.mercantec.easyeat.helpers.changePassword +import tech.mercantec.easyeat.helpers.createRecipe +import tech.mercantec.easyeat.helpers.request +import tech.mercantec.easyeat.models.Direction import tech.mercantec.easyeat.models.Ingredient +import tech.mercantec.easyeat.models.Recipe +import kotlin.concurrent.thread class CreateDishActivity : AppCompatActivity() { @@ -35,16 +46,61 @@ class CreateDishActivity : AppCompatActivity() { val saveButton: Button = findViewById(R.id.saveDishButton) saveButton.setOnClickListener { val ingredientList = collectIngredients() - - val name = findViewById(R.id.dishName).text.toString().trim() - val description = findViewById(R.id.dishDescription).text.toString().trim() val instructions = findViewById(R.id.instructions).text.toString().trim() - // Debug/log example - for (ingredient in ingredientList) { - Log.d("INGREDIENT", "Name: ${ingredient.Element}, Amount: ${ingredient.Amount}, Unit: ${ingredient.Unit}") + val directions: List = instructions + .split("\n") + .map { line -> line.trim() } + .filter { it.isNotEmpty() } + .map { line -> Direction(instructions = line) } + + val recipe = Recipe( + name = findViewById(R.id.dishName).text.toString().trim(), + description = findViewById(R.id.dishDescription).text.toString().trim(), + directions = directions, + ingredients = ingredientList + ) + + Log.i("recipe name:", recipe.name) + Log.i("recipe name:", recipe.description) + for (x in recipe.directions) { + Log.i("recipe name:", x.instructions) + } + for(x in recipe.ingredients){ + Log.i("recipe name:", x.name) + Log.i("recipe name:", x.amount.toString()) + Log.i("recipe name:", x.unit.toString()) + } + val progressDialog = ProgressDialog(this) + progressDialog.setMessage("Loading...") + progressDialog.show() + thread { + try { + createRecipe(this, recipe) + } catch (e: ApiRequestException) { + runOnUiThread { + Toast.makeText(this, e.message, Toast.LENGTH_LONG).show() + } + + return@thread + } finally { + runOnUiThread { + progressDialog.hide() + } + } + + runOnUiThread { + Toast.makeText(this, "Password changed successfully", Toast.LENGTH_LONG).show() + } + + finish() } } + + + + + } private fun addIngredientRow() { @@ -84,7 +140,7 @@ class CreateDishActivity : AppCompatActivity() { // Optional: Only add non-empty rows if (element.isNotEmpty() && amount.isNotEmpty()) { - ingredients.add(Ingredient(Element = element, Amount = amount.toInt(), Unit = unit)) + ingredients.add(Ingredient(name = element, amount = amount.toDouble(), unit = unit)) } } diff --git a/backend/API/Controllers/RecipeController.cs b/backend/API/Controllers/RecipeController.cs index 5fa9bdf..7d4fe6a 100644 --- a/backend/API/Controllers/RecipeController.cs +++ b/backend/API/Controllers/RecipeController.cs @@ -38,10 +38,13 @@ namespace API.Controllers } [Authorize] - [HttpPost("create/{RecipesId}")] - public async Task CreateRecipe([FromBody] RecipeDTO recipe, int RecipesId) + [HttpPost("create")] + public async Task CreateRecipe([FromBody] RecipeDTO recipe) { - return await _recipeLogic.CreateRecipe(recipe, RecipesId); + var claims = HttpContext.User.Claims; + string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; + int userId = Convert.ToInt32(userIdString); + return await _recipeLogic.CreateRecipe(recipe, userId); } [Authorize]