|
|
@ -1,14 +1,12 @@
|
|
|
|
package com.example.what_the_fantasy.data.services
|
|
|
|
package com.example.what_the_fantasy.data.services
|
|
|
|
|
|
|
|
|
|
|
|
import android.util.Log
|
|
|
|
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.Comment
|
|
|
|
import com.example.what_the_fantasy.data.model.Favorite
|
|
|
|
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.Quote
|
|
|
|
import com.example.what_the_fantasy.data.model.SrcLanguage
|
|
|
|
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.model.User
|
|
|
|
import com.example.what_the_fantasy.data.retrofit.RetrofitInstance
|
|
|
|
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.ui.viewModels.CurrentUserViewModel
|
|
|
|
|
|
|
|
|
|
|
|
//import com.example.what_the_fantasy.data.model.Comment
|
|
|
|
//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 {
|
|
|
|
try {
|
|
|
|
val responseOldUser = RetrofitInstance.api.getUserById(index)
|
|
|
|
val userExistsResponse = RetrofitInstance.api.checkIfUsernameExists(username)
|
|
|
|
val request = UserUpdateRequest(username,
|
|
|
|
|
|
|
|
responseOldUser.email,
|
|
|
|
|
|
|
|
responseOldUser.password,
|
|
|
|
|
|
|
|
responseOldUser.langage,
|
|
|
|
|
|
|
|
responseOldUser.imgUrl)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.d("EditUsername", "Username check: $username exists = $userExistsResponse")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (userExistsResponse) {
|
|
|
|
|
|
|
|
Log.d("EditUsername", "Username $username already exists, cannot update.")
|
|
|
|
return false
|
|
|
|
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) {
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
|
|
|
Log.d("EditUsername", "Exception occurred: ${e.message}")
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override suspend fun EditEmail(email: String, index: Int): Boolean {
|
|
|
|
|
|
|
|
|
|
|
|
override suspend fun EditEmail(email: String, index: Int,currentUserViewModel: CurrentUserViewModel): Boolean {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
val responseOldUser = RetrofitInstance.api.getUserById(index)
|
|
|
|
val userExistsResponse = RetrofitInstance.api.checkIfEmailExists(email)
|
|
|
|
val request = UserUpdateRequest(responseOldUser.username,
|
|
|
|
|
|
|
|
email,
|
|
|
|
|
|
|
|
responseOldUser.password,
|
|
|
|
|
|
|
|
responseOldUser.langage,
|
|
|
|
|
|
|
|
responseOldUser.imgUrl)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
|
|
|
Log.d("EditEmail", "Exception occurred: ${e.message}")
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override suspend fun EditPasswd(passwd: String, index: Int) {
|
|
|
|
override suspend fun EditPasswd(passwd: String, index: Int) {
|
|
|
|
try {
|
|
|
|
TODO()
|
|
|
|
val responseOldUser = RetrofitInstance.api.getUserById(index)
|
|
|
|
|
|
|
|
val request = UserUpdateRequest(responseOldUser.username,
|
|
|
|
|
|
|
|
responseOldUser.email,
|
|
|
|
|
|
|
|
passwd,
|
|
|
|
|
|
|
|
responseOldUser.langage,
|
|
|
|
|
|
|
|
responseOldUser.imgUrl)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}catch (_: Exception) { }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun EditImage(index: Int): String {
|
|
|
|
override fun EditImage(index: Int): String {
|
|
|
|