auth to server-V35
This commit is contained in:
parent
8af90dfb48
commit
a9475e8c78
4 changed files with 56 additions and 18 deletions
|
|
@ -5,6 +5,7 @@ import retrofit2.Response
|
||||||
import retrofit2.http.Body
|
import retrofit2.http.Body
|
||||||
import retrofit2.http.GET
|
import retrofit2.http.GET
|
||||||
import retrofit2.http.Header
|
import retrofit2.http.Header
|
||||||
|
import retrofit2.http.PATCH
|
||||||
import retrofit2.http.POST
|
import retrofit2.http.POST
|
||||||
|
|
||||||
interface TecnologiaApi {
|
interface TecnologiaApi {
|
||||||
|
|
@ -24,6 +25,11 @@ interface TecnologiaApi {
|
||||||
@GET("api/me")
|
@GET("api/me")
|
||||||
suspend fun getUserProfile(): Response<UserProfile>
|
suspend fun getUserProfile(): Response<UserProfile>
|
||||||
|
|
||||||
|
@PATCH("me")
|
||||||
|
suspend fun updateProfile(
|
||||||
|
@Body request: UserProfileUpdateRequest
|
||||||
|
): Response<Unit>
|
||||||
|
|
||||||
// 發送好友請求
|
// 發送好友請求
|
||||||
@POST("api/friends/request")
|
@POST("api/friends/request")
|
||||||
suspend fun sendFriendRequest(
|
suspend fun sendFriendRequest(
|
||||||
|
|
|
||||||
|
|
@ -17,3 +17,12 @@ data class UserProfile(
|
||||||
val last_login: String?,
|
val last_login: String?,
|
||||||
val created_at: String?
|
val created_at: String?
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class UserProfileUpdateRequest(
|
||||||
|
val avatar_url: String? = null,
|
||||||
|
val banner_url: String? = null,
|
||||||
|
val bio: String? = null,
|
||||||
|
val display_name: String? = null,
|
||||||
|
val status: String? = null
|
||||||
|
)
|
||||||
|
|
@ -2,19 +2,39 @@ package com.example.login_v3.data.repository.basic
|
||||||
|
|
||||||
import com.example.login_v3.data.api.TecnologiaApi
|
import com.example.login_v3.data.api.TecnologiaApi
|
||||||
import com.example.login_v3.data.api.api_class.UserProfile
|
import com.example.login_v3.data.api.api_class.UserProfile
|
||||||
|
import com.example.login_v3.data.api.api_class.UserProfileUpdateRequest
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class PersonalProfileRepository @Inject constructor(
|
class PersonalProfileRepository @Inject constructor(
|
||||||
private val api: TecnologiaApi
|
private val api: TecnologiaApi // 確保你的 API interface 裡有 getUserProfile 和 updateProfile
|
||||||
) {
|
) {
|
||||||
suspend fun getMyProfile(): UserProfile {
|
// 取得資料
|
||||||
|
suspend fun getMyProfile(): Result<UserProfile> {
|
||||||
|
return try {
|
||||||
val response = api.getUserProfile()
|
val response = api.getUserProfile()
|
||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) {
|
||||||
// body()!! 雖然簡單,但建議確保 API 回傳格式正確
|
val body = response.body()
|
||||||
return response.body() ?: throw Exception("回傳資料為空")
|
if (body != null) Result.success(body)
|
||||||
|
else Result.failure(Exception("回傳資料為空"))
|
||||||
} else {
|
} else {
|
||||||
// 這裡可以根據 response.code() 給出更詳細的錯誤訊息
|
Result.failure(Exception("讀取失敗 (${response.code()})"))
|
||||||
throw Exception("讀取失敗 (${response.code()})")
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Result.failure(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新資料 (PATCH)
|
||||||
|
suspend fun patchUserProfile(request: UserProfileUpdateRequest): Result<Unit> {
|
||||||
|
return try {
|
||||||
|
val response = api.updateProfile(request)
|
||||||
|
if (response.isSuccessful) {
|
||||||
|
Result.success(Unit)
|
||||||
|
} else {
|
||||||
|
Result.failure(Exception("更新失敗: ${response.code()}"))
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Result.failure(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -34,13 +34,16 @@ class PersonalProfileViewModel @Inject constructor(
|
||||||
fun fetchProfile() {
|
fun fetchProfile() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
_uiState.value = ProfileUiState.Loading
|
_uiState.value = ProfileUiState.Loading
|
||||||
try {
|
|
||||||
val profile = repository.getMyProfile()
|
val result = repository.getMyProfile()
|
||||||
|
|
||||||
|
result
|
||||||
|
.onSuccess { profile ->
|
||||||
_uiState.value = ProfileUiState.Success(profile)
|
_uiState.value = ProfileUiState.Success(profile)
|
||||||
} catch (e: Exception) {
|
}
|
||||||
// 如果是 401,Interceptor 會處理登出,
|
.onFailure { e ->
|
||||||
// 這裡處理一般的網路錯誤或 API 錯誤
|
_uiState.value =
|
||||||
_uiState.value = ProfileUiState.Error(e.localizedMessage ?: "未知錯誤")
|
ProfileUiState.Error(e.localizedMessage ?: "未知錯誤")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue