create dish done but not checked with api

This commit is contained in:
LilleBRG 2025-05-07 13:51:54 +02:00
parent bdd7a0dfdb
commit 536b693085
6 changed files with 104 additions and 18 deletions

View File

@ -1,6 +1,7 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JetCodeStyleSettings>
<option name="ALLOW_TRAILING_COMMA" value="true" />
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="XML">

View File

@ -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<ChangePasswordRequest, Unit>(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<CreateRecipeRequest, Unit>(ctx, "POST", "/api/recipe/create", request)
}

View File

@ -1,7 +0,0 @@
package tech.mercantec.easyeat.models
data class Ingredient(
val Amount: Int,
val Unit: String,
val Element: String
)

View File

@ -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<Direction>,
val ingredients: List<Ingredient>
)
@Serializable
data class Direction(
val instructions: String
)
@Serializable
data class Ingredient(
val amount: Double?,
val unit: String?,
val name: String
)

View File

@ -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<EditText>(R.id.dishName).text.toString().trim()
val description = findViewById<EditText>(R.id.dishDescription).text.toString().trim()
val instructions = findViewById<EditText>(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<Direction> = instructions
.split("\n")
.map { line -> line.trim() }
.filter { it.isNotEmpty() }
.map { line -> Direction(instructions = line) }
val recipe = Recipe(
name = findViewById<EditText>(R.id.dishName).text.toString().trim(),
description = findViewById<EditText>(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))
}
}

View File

@ -38,10 +38,13 @@ namespace API.Controllers
}
[Authorize]
[HttpPost("create/{RecipesId}")]
public async Task<IActionResult> CreateRecipe([FromBody] RecipeDTO recipe, int RecipesId)
[HttpPost("create")]
public async Task<IActionResult> 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]