From 5c1a5699063b0a3bc984a72bc469c7a763725d67 Mon Sep 17 00:00:00 2001 From: LilleBRG Date: Fri, 9 May 2025 10:22:02 +0200 Subject: [PATCH] made new getallrecipedto and return on getallrecipe --- .../tech/mercantec/easyeat/helpers/auth.kt | 1 + .../easyeat/ui/dishes/DishesFragment.kt | 43 +++++++++++++++---- backend/API/BusinessLogic/RecipeLogic.cs | 9 +++- .../Models/RecipeModels/GetAllRecipesDTO.cs | 12 ++++++ 4 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 backend/API/Models/RecipeModels/GetAllRecipesDTO.cs 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 3f3ce64..b82a640 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 @@ -110,6 +110,7 @@ fun changePassword(ctx: Context, oldPassword: String, newPassword: String) { return requestJson(ctx, "PUT", "/api/User/change-password", request) } + @Serializable data class CreateRecipeRequest(val recipe: Recipe) diff --git a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/DishesFragment.kt b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/DishesFragment.kt index e1f35d9..7ee7de3 100644 --- a/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/DishesFragment.kt +++ b/app/app/src/main/java/tech/mercantec/easyeat/ui/dishes/DishesFragment.kt @@ -2,6 +2,7 @@ package tech.mercantec.easyeat.ui.dishes import android.content.Intent import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -12,8 +13,13 @@ import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.launch import tech.mercantec.easyeat.R import tech.mercantec.easyeat.databinding.FragmentDishesBinding +import tech.mercantec.easyeat.helpers.ApiRequestException +import tech.mercantec.easyeat.helpers.RecipeResponse import tech.mercantec.easyeat.helpers.getRecipies +import tech.mercantec.easyeat.helpers.login import tech.mercantec.easyeat.models.DishListItem +import tech.mercantec.easyeat.ui.MainActivity +import kotlin.concurrent.thread class DishesFragment : Fragment() { @@ -31,20 +37,41 @@ class DishesFragment : Fragment() { _binding = FragmentDishesBinding.inflate(inflater, container, false) val root: View = binding.root - lifecycleScope.launch { + + thread { + val recipes: List try { - val recipes = getRecipies(requireContext()) // this is now suspend - val dishes = recipes.map { - DishListItem(it.name, it.description) // assuming description is the main ingredient and no price + recipes = getRecipies(requireContext()) + } catch (e: ApiRequestException) { + activity?.runOnUiThread { + Toast.makeText(requireContext(), e.message, Toast.LENGTH_LONG).show() } - val adapter = DishAdapter(requireContext(), dishes) - binding.dishesList.adapter = adapter - } catch (e: Exception) { - Toast.makeText(requireContext(), "Failed to load recipes: ${e.message}", Toast.LENGTH_LONG).show() + return@thread + } + val dishes = recipes.map { + DishListItem( + it.name, + it.description + ) // assuming description is the main ingredient and no price } } +// lifecycleScope.launch { +// try { +// val recipes = getRecipies(requireContext()) // this is now suspend +// val dishes = recipes.map { +// DishListItem(it.name, it.description) // assuming description is the main ingredient and no price +// } +// +// val adapter = DishAdapter(requireContext(), dishes) +// binding.dishesList.adapter = adapter +// } catch (e: Exception) { +// Log.i("nice", e.message.toString()) +// Toast.makeText(requireContext(), "Failed to load recipes: ${e.message}", Toast.LENGTH_LONG).show() +// } +// } + binding.addDish.setOnClickListener { val dialogView = LayoutInflater.from(requireContext()).inflate(R.layout.create_dish_modal_dialog, null) diff --git a/backend/API/BusinessLogic/RecipeLogic.cs b/backend/API/BusinessLogic/RecipeLogic.cs index 48aaa3f..28e515e 100644 --- a/backend/API/BusinessLogic/RecipeLogic.cs +++ b/backend/API/BusinessLogic/RecipeLogic.cs @@ -23,7 +23,14 @@ namespace API.BusinessLogic var recipes = await _dbAccess.ReadRecipes(userId); if (recipes == null || recipes.Count == 0) { return new ConflictObjectResult(new { message = "Could not find any recipes" }); } - return new OkObjectResult(recipes); + var recipeDtos = recipes.Select(r => new GetAllRecipesDTO + { + Id = r.Id, + Name = r.Name, + Description = r.Description + }).ToList(); + + return new OkObjectResult(recipeDtos); } // Gets a specifik with recipe with the ingredient and directions diff --git a/backend/API/Models/RecipeModels/GetAllRecipesDTO.cs b/backend/API/Models/RecipeModels/GetAllRecipesDTO.cs new file mode 100644 index 0000000..47bf29e --- /dev/null +++ b/backend/API/Models/RecipeModels/GetAllRecipesDTO.cs @@ -0,0 +1,12 @@ +namespace API.Models.RecipeModels +{ + public class GetAllRecipesDTO + { + public int Id { get; set; } + + public string Name { get; set; } + + public string Description { get; set; } + + } +}