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.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.PATCH
|
||||
import retrofit2.http.POST
|
||||
|
||||
interface TecnologiaApi {
|
||||
|
|
@ -24,6 +25,11 @@ interface TecnologiaApi {
|
|||
@GET("api/me")
|
||||
suspend fun getUserProfile(): Response<UserProfile>
|
||||
|
||||
@PATCH("me")
|
||||
suspend fun updateProfile(
|
||||
@Body request: UserProfileUpdateRequest
|
||||
): Response<Unit>
|
||||
|
||||
// 發送好友請求
|
||||
@POST("api/friends/request")
|
||||
suspend fun sendFriendRequest(
|
||||
|
|
|
|||
|
|
@ -17,3 +17,12 @@ data class UserProfile(
|
|||
val last_login: 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.api_class.UserProfile
|
||||
import com.example.login_v3.data.api.api_class.UserProfileUpdateRequest
|
||||
import javax.inject.Inject
|
||||
|
||||
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()
|
||||
if (response.isSuccessful) {
|
||||
// body()!! 雖然簡單,但建議確保 API 回傳格式正確
|
||||
return response.body() ?: throw Exception("回傳資料為空")
|
||||
val body = response.body()
|
||||
if (body != null) Result.success(body)
|
||||
else Result.failure(Exception("回傳資料為空"))
|
||||
} else {
|
||||
// 這裡可以根據 response.code() 給出更詳細的錯誤訊息
|
||||
throw Exception("讀取失敗 (${response.code()})")
|
||||
Result.failure(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() {
|
||||
viewModelScope.launch {
|
||||
_uiState.value = ProfileUiState.Loading
|
||||
try {
|
||||
val profile = repository.getMyProfile()
|
||||
|
||||
val result = repository.getMyProfile()
|
||||
|
||||
result
|
||||
.onSuccess { profile ->
|
||||
_uiState.value = ProfileUiState.Success(profile)
|
||||
} catch (e: Exception) {
|
||||
// 如果是 401,Interceptor 會處理登出,
|
||||
// 這裡處理一般的網路錯誤或 API 錯誤
|
||||
_uiState.value = ProfileUiState.Error(e.localizedMessage ?: "未知錯誤")
|
||||
}
|
||||
.onFailure { e ->
|
||||
_uiState.value =
|
||||
ProfileUiState.Error(e.localizedMessage ?: "未知錯誤")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue