Bläddra i källkod

Добавление ингредиентов в бд

Anastasia 4 dagar sedan
förälder
incheckning
b9b20681ef

+ 8 - 0
EatWell/.idea/deploymentTargetSelector.xml

@@ -4,6 +4,14 @@
     <selectionStates>
       <SelectionState runConfigName="app">
         <option name="selectionMode" value="DROPDOWN" />
+        <DropdownSelection timestamp="2024-11-23T07:10:25.430159400Z">
+          <Target type="DEFAULT_BOOT">
+            <handle>
+              <DeviceId pluginId="LocalEmulator" identifier="path=C:\Users\user\.android\avd\Pixel_8_API_30.avd" />
+            </handle>
+          </Target>
+        </DropdownSelection>
+        <DialogSelection />
       </SelectionState>
       <SelectionState runConfigName="auth">
         <option name="selectionMode" value="DROPDOWN" />

+ 6 - 0
EatWell/.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
+  </component>
+</project>

+ 3 - 0
EatWell/app/src/main/java/com/example/eatwell/Resources.kt

@@ -3,12 +3,14 @@ package com.example.eatwell
 import androidx.compose.runtime.mutableStateOf
 import com.example.eatwell.model.Category
 import com.example.eatwell.model.Diets
+import com.example.eatwell.model.Fridge
 import com.example.eatwell.model.Ingredients
 import com.example.eatwell.model.Recipes
 import com.example.eatwell.view.MainViewModel
 
 class Resources {
     companion object {
+        var userId: Int = -1;
         var viewModel = MainViewModel()
         var diets: MutableList<Diets> = mutableListOf()
         var categories: MutableList<Category> = mutableListOf()
@@ -16,6 +18,7 @@ class Resources {
         var chosenRecipes: MutableList<Recipes> = mutableListOf()
         var ingredients: MutableList<Ingredients> = mutableListOf()
         var chosenIngredients: MutableList<Ingredients> = mutableListOf()
+        var fridge: MutableList<Fridge> = mutableListOf()
         // Состояние для выбранных рецептов
     }
 }

+ 1 - 1
EatWell/app/src/main/java/com/example/eatwell/model/Fridge.kt

@@ -4,7 +4,7 @@ import kotlinx.serialization.Serializable
 
 @Serializable
 data class Fridge(
-    val id: Int,
+    val id: Int? = null,
     val ingredients: Int,
     val user_id: String
 )

+ 67 - 10
EatWell/app/src/main/java/com/example/eatwell/view/MainViewModel.kt

@@ -7,6 +7,7 @@ import com.example.eatwell.Resources
 import com.example.eatwell.domain.Constant
 import com.example.eatwell.model.Category
 import com.example.eatwell.model.Diets
+import com.example.eatwell.model.Fridge
 import com.example.eatwell.model.Ingredients
 import com.example.eatwell.model.Recipes
 
@@ -14,23 +15,27 @@ import io.github.jan.supabase.gotrue.auth
 import io.github.jan.supabase.gotrue.providers.builtin.Email
 import io.github.jan.supabase.postgrest.from
 import kotlinx.coroutines.launch
+import java.util.UUID
 
 class MainViewModel():ViewModel() {
 
     fun onSignInEmailPassword(emailUser: String, passwordUser: String) {
         viewModelScope.launch {
             try {
+                //   Constant.supabase.auth.signOut()
                 val user = Constant.supabase.auth.signInWith(Email) {
                     email = emailUser
                     password = passwordUser
                 }
-                    //  println(user.toString())
+
+
+                //  println(user.toString())
                 Log.d("Логин: ", user.toString())
-               // println(Constant.supabase.auth.currentUserOrNull()!!.id)
+                // println(Constant.supabase.auth.currentUserOrNull()!!.id)
                 println("Success")
                 Log.d("Логин",Constant.supabase.auth.currentUserOrNull()!!.id )
             } catch (e: Exception) {
-                println("Error)))))))")
+
                 Log.d("Ошибка: ", e.message.toString())
                 println(e.message.toString())
             }
@@ -56,6 +61,28 @@ class MainViewModel():ViewModel() {
         }
     }
 
+    fun addIngredientToFridge(ingredientId: Int) {
+        viewModelScope.launch {
+            val userId = UUID.fromString(Constant.supabase.auth.currentUserOrNull()!!.id)
+            val fridgeItem = Fridge(
+                ingredients = ingredientId,
+                user_id = userId.toString()
+            )
+
+            Log.d("Идентификатор: ", Constant.supabase.auth.currentUserOrNull()!!.id );
+            try {
+                val response = Constant.supabase
+                    .from("Fridge")
+                    .insert(Fridge(ingredients = ingredientId, user_id = Constant.supabase.auth.currentUserOrNull()!!.id ))
+                // .execute()
+                Log.d("Ответ вставки: ", response.toString())
+            }
+            catch (ex: Exception){
+                Log.d("Ошибка вставки: ", ex.toString())
+            }
+        }
+    }
+
     fun getRecipes(){
 
         viewModelScope.launch {
@@ -69,17 +96,49 @@ class MainViewModel():ViewModel() {
         }
     }
 
+    fun getFridge(){
+        viewModelScope.launch {
+
+            try {
+                var list =  Constant.supabase.from("Fridge").select() {
+                    filter {
+                        Fridge::user_id eq Constant.supabase.auth.currentUserOrNull()!!.id
+                    }
+                }.decodeList<Fridge>()
+
+                Log.d("id ", Constant.supabase.auth.currentUserOrNull()!!.id )
+                Log.d("fridge: ", list.toString())
+
+                Resources.chosenIngredients.clear();
+                for (i in 0..list.size){
+                    for (j in 0..Resources.ingredients.size){
+                        if(Resources.ingredients.get(j).id == list.get(i).id){
+                            Resources.chosenIngredients.add(Resources.ingredients.get(j))
+                            break
+                        }
+
+                    }
+                }
+
+
+            } catch (e: Exception) {
+                Log.e("Ошибка: ", e.message.toString())
+            }
+        }
+    }
+
+
     fun getRecipes(categoryId: Int, dietId: Int): List<Recipes>{
         var recipesList: List<Recipes>
         recipesList = ArrayList()
         viewModelScope.launch {
             try{
-             recipesList = Constant.supabase.from("Recipes").select(){ filter {
+                recipesList = Constant.supabase.from("Recipes").select(){ filter {
 
-                 Recipes::category eq categoryId
-                 Recipes::diets eq dietId
-             } }.decodeList<Recipes>()
-           Log.d("Список по категории: ", recipesList.toString())
+                    Recipes::category eq categoryId
+                    Recipes::diets eq dietId
+                } }.decodeList<Recipes>()
+                Log.d("Список по категории: ", recipesList.toString())
             }
             catch (e: Exception){
                 Log.e("Ошибка: ", e.message.toString())
@@ -127,6 +186,4 @@ class MainViewModel():ViewModel() {
         }
 
     }
-
-
 }

+ 1 - 0
EatWell/app/src/main/java/com/example/eatwell/view/mainActivity/Fridge.kt

@@ -65,6 +65,7 @@ import io.github.jan.supabase.gotrue.auth
 //@Preview
 @Composable
 fun fridge(navController: NavController) {
+    Resources.viewModel.getFridge()
     Box(
         modifier = Modifier
             .fillMaxSize()

+ 1 - 0
EatWell/app/src/main/java/com/example/eatwell/view/mainActivity/Home.kt

@@ -82,6 +82,7 @@ fun home(navController: NavHostController) {
     var expanded by remember { mutableStateOf(false) }
     var expandedCat by remember { mutableStateOf(false) }
     var selectedOption: String
+
     for (i in 0..Resources.recipes.size-1){
         Resources.chosenRecipes.add(Resources.recipes.get(i))
     }

+ 4 - 2
EatWell/app/src/main/java/com/example/eatwell/view/mainActivity/Ingredients.kt

@@ -57,6 +57,7 @@ import com.example.eatwell.R
 import com.example.eatwell.Resources
 import com.example.eatwell.domain.Constant
 import com.example.eatwell.model.Recipes
+import com.example.eatwell.view.MainViewModel
 import com.example.eatwell.view.ui.theme.black
 import com.example.eatwell.view.ui.theme.fon2
 import com.example.eatwell.view.ui.theme.knopka
@@ -141,10 +142,11 @@ fun ingredients(navController: NavController) {
                                     fontFamily = FontFamily.Monospace
                                 )
                                 Button(onClick = {
+                                    var viewModel: MainViewModel = MainViewModel()
+                                    viewModel.addIngredientToFridge(item.id)
                                     Resources.chosenIngredients.add(
                                         item
-                                    )
-                                }) {
+                                    )}) {
 
                                 }
                             }