From 6e24977f386c61dbf0d5c9023471e7ff766e2106 Mon Sep 17 00:00:00 2001 From: "kentin.brongniart" Date: Sun, 6 Apr 2025 23:32:47 +0200 Subject: [PATCH] =?UTF-8?q?test=20cr=C3=A9ate=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../what_the_fantasy/data/model/User.kt | 2 ++ .../data/retrofit/ApiService.kt | 5 +++- .../data/services/IServices.kt | 2 +- .../data/services/ServicesAPI.kt | 11 +++++--- .../data/services/ServicesStub.kt | 2 +- .../ui/screens/AccueilPage.kt | 1 - .../what_the_fantasy/ui/screens/SignUpPage.kt | 26 ++++++++++++------- .../ui/viewModels/SignInUserViewModel.kt | 9 ++++--- 8 files changed, 38 insertions(+), 20 deletions(-) diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt index c7355c1..956e3c9 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/model/User.kt @@ -3,6 +3,7 @@ package com.example.what_the_fantasy.data.model import com.google.gson.annotations.SerializedName class User( + @SerializedName("id") val id:Int, @SerializedName("pseudo") var username:String, @@ -13,6 +14,7 @@ class User( @SerializedName("imageProfil") val imgUrl: String, + @SerializedName("password") var password: String, @SerializedName("lang") diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt index 3b53466..ed3b4f3 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/retrofit/ApiService.kt @@ -50,7 +50,10 @@ interface ApiService { @Body user: User // Envoie un objet `User` avec les nouvelles données ): Response - + @POST("users") + suspend fun addUser( + @Body newUser: User + ): Response @GET("quote/dailyquote") diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt index 527e576..f9a09fe 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/IServices.kt @@ -29,7 +29,7 @@ interface IServices { fun AddFav(userId: Int, QuoteId : Int) fun SupFav(userId: Int, QuoteId : Int) fun AddComment(content : String) - fun CreateUser(username : String, email : String, passwd : String) : Boolean + suspend fun CreateUser(username : String, email : String, passwd : String) : Boolean fun getFavorite(user: User): List fun getAllUsers(): List fun getComment(quoteId : Int) : List diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt index 49f1b5c..3b9b5e7 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesAPI.kt @@ -12,6 +12,7 @@ import com.example.what_the_fantasy.data.model.User import com.example.what_the_fantasy.data.retrofit.RetrofitInstance import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel import java.time.LocalDate +import java.util.Calendar import java.util.Date //import com.example.what_the_fantasy.data.model.Comment @@ -234,8 +235,13 @@ class ServicesAPI : IServices { TODO("Not yet implemented") } - override fun CreateUser(username: String, email: String, passwd: String): Boolean { - TODO("Not yet implemented") + + override suspend fun CreateUser(username: String, email: String, passwd: String): Boolean { + val today = Calendar.getInstance().time + val rep =RetrofitInstance.api.addUser( User(15,username,email,today.toString(),"img",passwd,SrcLanguage.vo)) + Log.d("test",rep.toString()) + if(rep.code() in 200..299) return true + return false } override fun getFavorite(user: User): List { @@ -272,7 +278,6 @@ class ServicesAPI : IServices { override suspend fun getSomeQuotes(nb: Int, page: Int): MutableList { val result = RetrofitInstance.api.getAllQuote(page,nb) - Log.d("test", result.toString()) return result.items } diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt index 114244e..aa7c987 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/data/services/ServicesStub.kt @@ -104,7 +104,7 @@ class ServicesStub : IServices { } - override fun CreateUser(username: String, email: String, passwd: String) : Boolean { + override suspend fun CreateUser(username: String, email: String, passwd: String) : Boolean { val date =dateDuJour() val passwordhash = hashPassword(passwd) val services = ServicesStub() diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt index 51a1cc0..e4f0806 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/AccueilPage.kt @@ -71,7 +71,6 @@ fun AccueilPage( isLoading.value = true delay(500) val newQuotes = services.getSomeQuotes(15, page.intValue) - Log.d("Accueil",newQuotes.toString()) val uniqueQuotes = newQuotes.filterNot { new -> quotes.any { it.id == new.id } } if (uniqueQuotes.isNotEmpty()) { diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt index d812798..8de7106 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/screens/SignUpPage.kt @@ -26,6 +26,7 @@ import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -47,6 +48,7 @@ import com.example.what_the_fantasy.ui.components.ErrorMessageProfileComponent import com.example.what_the_fantasy.ui.components.VisibleIconPasswordComponent import com.example.what_the_fantasy.ui.states.SignInUserState import com.example.what_the_fantasy.ui.viewModels.SignInUserViewModel +import kotlinx.coroutines.launch @Composable fun SignUpPage(navControllerLogin: () -> Unit, signInUserVM :SignInUserViewModel,signInState : SignInUserState) { @@ -91,7 +93,7 @@ fun SignUpPage(navControllerLogin: () -> Unit, signInUserVM :SignInUserViewModel ConnexionButtonSign(R.string.ButtonSignUp,18, Color.White, Color.Black, signInState.username, signInState.email, signInState.password, signInState.confirmPassword, signInUserVM, navControllerLogin) SpaceHeightComponent(16) - ReturnLogin(R.string.ButtonLogin,12, Color.White, navController = navControllerLogin) + ReturnLogin(R.string.ButtonLogin,12, MaterialTheme.colorScheme.onBackground, navController = navControllerLogin) } } @@ -219,17 +221,21 @@ fun ConnexionButtonSign( val invalidRegex = """^[a-zA-Z0-9]*$""".toRegex() + + val coroutineScope = rememberCoroutineScope() Button( onClick = { - emailError = !isValidEmail(email) - passwordError = !arePasswordsMatching(password, confirmPassword) - usernameError = username.isBlank() && !username.matches(invalidRegex) - passwordErrorEmpty = password.isBlank() || confirmPassword.isBlank() - - if (!emailError && !passwordError && !usernameError && !passwordErrorEmpty) { - usernameErrorExist = !viewModel.createUser(username, email, password) - if(!usernameErrorExist){ - navController() // retour à la page login + coroutineScope.launch { + emailError = !isValidEmail(email) + passwordError = !arePasswordsMatching(password, confirmPassword) + usernameError = username.isBlank() && !username.matches(invalidRegex) + passwordErrorEmpty = password.isBlank() || confirmPassword.isBlank() + + if (!emailError && !passwordError && !usernameError && !passwordErrorEmpty) { + usernameErrorExist = !viewModel.createUser(username, email, password) + if (!usernameErrorExist) { + navController() // retour à la page login + } } } }, diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/SignInUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/SignInUserViewModel.kt index 42033f8..04277da 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/SignInUserViewModel.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/SignInUserViewModel.kt @@ -1,20 +1,23 @@ package com.example.what_the_fantasy.ui.viewModels import androidx.lifecycle.ViewModel +import com.example.what_the_fantasy.data.services.ServicesAPI import com.example.what_the_fantasy.data.services.ServicesStub import com.example.what_the_fantasy.ui.states.SignInUserState +import kotlinx.coroutines.awaitAll import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update class SignInUserViewModel: ViewModel() { - private val services = ServicesStub() // faire repository qui gère les services Stub et API + private val services = ServicesAPI() // faire repository qui gère les services Stub et API private val _userState = MutableStateFlow(SignInUserState()) val userState : StateFlow = _userState.asStateFlow() - fun createUser(username: String, email: String, passwd: String) : Boolean{ - return services.CreateUser(username, email, passwd) + suspend fun createUser(username: String, email: String, passwd: String) : Boolean{ + return services.CreateUser(username, email, passwd) + } fun setUsername(username : String){