From 8362377241ab08b469739c8f63434b939363b959 Mon Sep 17 00:00:00 2001 From: "leni.beaulaton" Date: Sat, 5 Apr 2025 16:41:48 +0200 Subject: [PATCH] Change info mais ne les sauvegarde pas --- .../data/retrofit/ApiService.kt | 22 ++++- .../data/services/IServices.kt | 4 +- .../data/services/ServicesAPI.kt | 93 +++++++++++++------ .../data/services/ServicesStub.kt | 2 +- .../ui/viewModels/CurrentUserViewModel.kt | 32 ++++--- 5 files changed, 104 insertions(+), 49 deletions(-) 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 15c7c82..3b570eb 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 @@ -8,6 +8,7 @@ import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.POST +import retrofit2.http.PUT import retrofit2.http.Path import retrofit2.http.Query @@ -16,7 +17,8 @@ data class UserUpdateRequest( val email: String, val passwd: String, val langage: SrcLanguage, - val imgUrl: String + val imgUrl: String, + ) interface UserApiService { @@ -31,12 +33,22 @@ interface UserApiService { @Path("id") id: Int ): User + @GET("users/existusername") + suspend fun checkIfUsernameExists( + @Query("username") username: String + ): Boolean + + @GET("users/existemail") + suspend fun checkIfEmailExists( + @Query("email") email: String + ): Boolean + - @POST("users") - suspend fun updateUser( + @PUT("users") + suspend fun updateUsername( @Query("id") id: Int, - @Body userUpdate: UserUpdateRequest - ): Response + @Body user: User // Envoie un objet `User` avec les nouvelles données + ): Response } 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 a16d76d..78dbd7d 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 @@ -18,8 +18,8 @@ interface IServices { currentUserViewModel: CurrentUserViewModel ): Boolean - suspend fun EditUsername(username : String, index : Int) : Boolean - suspend fun EditEmail(email : String, index : Int) : Boolean + suspend fun EditUsername(username : String, index : Int, currentUserViewModel: CurrentUserViewModel) : Boolean + suspend fun EditEmail(email : String, index : Int,currentUserViewModel: CurrentUserViewModel) : Boolean suspend fun EditPasswd(passwd : String, index : Int) fun EditImage(index : Int) : String fun ChangeLangage(index : Int): SrcLanguage 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 b5d0a0b..3d99a15 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 @@ -1,14 +1,12 @@ package com.example.what_the_fantasy.data.services import android.util.Log -import com.example.what_the_fantasy.data.local.UserStub.users import com.example.what_the_fantasy.data.model.Comment import com.example.what_the_fantasy.data.model.Favorite import com.example.what_the_fantasy.data.model.Quote import com.example.what_the_fantasy.data.model.SrcLanguage import com.example.what_the_fantasy.data.model.User import com.example.what_the_fantasy.data.retrofit.RetrofitInstance -import com.example.what_the_fantasy.data.retrofit.UserUpdateRequest import com.example.what_the_fantasy.ui.viewModels.CurrentUserViewModel //import com.example.what_the_fantasy.data.model.Comment @@ -46,49 +44,84 @@ class ServicesAPI : IServices { } } - override suspend fun EditUsername(username: String, index: Int): Boolean { + override suspend fun EditUsername(username: String, index: Int, currentUserViewModel: CurrentUserViewModel): Boolean { try { - val responseOldUser = RetrofitInstance.api.getUserById(index) - val request = UserUpdateRequest(username, - responseOldUser.email, - responseOldUser.password, - responseOldUser.langage, - responseOldUser.imgUrl) + val userExistsResponse = RetrofitInstance.api.checkIfUsernameExists(username) + Log.d("EditUsername", "Username check: $username exists = $userExistsResponse") - return false - }catch (e: Exception) { + if (userExistsResponse) { + Log.d("EditUsername", "Username $username already exists, cannot update.") + return false + } + + val responseUser = RetrofitInstance.api.getUserById(index) + + val updatedUser = User( + id = index, + username = username, // Nouveau nom d'utilisateur + email = responseUser.email, + password = responseUser.password, + imgUrl = responseUser.imgUrl, + langage = responseUser.langage, + date = responseUser.date + ) + + val response = RetrofitInstance.api.updateUsername(index, updatedUser) + + if (response.isSuccessful) { + currentUserViewModel.setUser(updatedUser) + Log.d("EditUsername", "Username updated successfully.") + return true + } else { + Log.d("EditUsername", "Failed to update username, API response not successful.") + return false + } + } catch (e: Exception) { + e.printStackTrace() + Log.d("EditUsername", "Exception occurred: ${e.message}") return false } } - override suspend fun EditEmail(email: String, index: Int): Boolean { + + override suspend fun EditEmail(email: String, index: Int,currentUserViewModel: CurrentUserViewModel): Boolean { try { - val responseOldUser = RetrofitInstance.api.getUserById(index) - val request = UserUpdateRequest(responseOldUser.username, - email, - responseOldUser.password, - responseOldUser.langage, - responseOldUser.imgUrl) + val userExistsResponse = RetrofitInstance.api.checkIfEmailExists(email) + if (userExistsResponse) { + return false + } - return false - }catch (e: Exception) { + val responseUser = RetrofitInstance.api.getUserById(index) + + val updatedUser = User( + id = index, + username = responseUser.username, + email = email,// Nouvel email d'utilisateur + password = responseUser.password, + imgUrl = responseUser.imgUrl, + langage = responseUser.langage, + date = responseUser.date + ) + + val response = RetrofitInstance.api.updateUsername(index, updatedUser) + + if (response.isSuccessful) { + currentUserViewModel.setUser(updatedUser) + return true + } else { + return false + } + } catch (e: Exception) { + e.printStackTrace() + Log.d("EditEmail", "Exception occurred: ${e.message}") return false } } - override suspend fun EditPasswd(passwd: String, index: Int) { - try { - val responseOldUser = RetrofitInstance.api.getUserById(index) - val request = UserUpdateRequest(responseOldUser.username, - responseOldUser.email, - passwd, - responseOldUser.langage, - responseOldUser.imgUrl) - - }catch (_: Exception) { } + TODO() } override fun EditImage(index: Int): String { 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 c6363fb..1fab8bb 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 @@ -39,7 +39,7 @@ class ServicesStub : IServices { } - override suspend fun EditUsername(username: String, index : Int) : Boolean{ + override suspend fun EditUsername(username: String, index : Int,currentUserViewModel: CurrentUserViewModel) : Boolean{ val user = getUserById(index) if(!isUsernameExist(username)){ diff --git a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt index 1920bdc..7aa7bbb 100644 --- a/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt +++ b/What_The_Fantasy/app/src/main/java/com/example/what_the_fantasy/ui/viewModels/CurrentUserViewModel.kt @@ -71,18 +71,28 @@ class CurrentUserViewModel : ViewModel(){ - suspend fun editUsername(username : String, index : Int) : Boolean{ - _currentUserState.update {it.copy(username = username)} - return services.EditUsername(username, index) + suspend fun editUsername(username: String, index: Int): Boolean { + if (!services.EditUsername(username, index, this)) { + _currentUserState.update { + it.copy(username = username) + } + return true + } + return false } suspend fun editEmail(email : String, index : Int) : Boolean{ - _currentUserState.update { - it.copy(email = email) + + if(!services.EditEmail(email, index, this)){ + _currentUserState.update { + it.copy(email = email) + } + return true } - return services.EditEmail(email, index) + return false } + suspend fun editPassword(password : String, index : Int){ services.EditPasswd(password, index) @@ -97,10 +107,10 @@ class CurrentUserViewModel : ViewModel(){ } fun editImage(index : Int){ - val image = services.EditImage(index) - - _currentUserState.update { - it.copy(imagePath = image) - } + // val image = services.EditImage(index) +// +// _currentUserState.update { +// it.copy(imagePath = image) +// } } } \ No newline at end of file