Change info mais ne les sauvegarde pas

ConnectAPI
Leni BEAULATON 3 weeks ago
parent db175c17f9
commit 8362377241

@ -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<User>
@Body user: User // Envoie un objet `User` avec les nouvelles données
): Response<Unit>
}

@ -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

@ -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")
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
}
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 {

@ -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)){

@ -72,16 +72,26 @@ class CurrentUserViewModel : ViewModel(){
suspend fun editUsername(username: String, index: Int): Boolean {
_currentUserState.update {it.copy(username = username)}
return services.EditUsername(username, index)
if (!services.EditUsername(username, index, this)) {
_currentUserState.update {
it.copy(username = username)
}
return true
}
return false
}
suspend fun editEmail(email : String, index : Int) : Boolean{
if(!services.EditEmail(email, index, this)){
_currentUserState.update {
it.copy(email = email)
}
return services.EditEmail(email, index)
return true
}
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)
// }
}
}
Loading…
Cancel
Save