Files
rider-pro-android-app/app/src/main/java/com/aiosman/riderpro/data/AccountService.kt

111 lines
3.6 KiB
Kotlin
Raw Normal View History

2024-07-29 00:01:09 +08:00
package com.aiosman.riderpro.data
2024-08-11 17:15:17 +08:00
import com.aiosman.riderpro.data.api.ApiClient
import com.aiosman.riderpro.data.api.LoginUserRequestBody
import com.aiosman.riderpro.data.api.RegisterRequestBody
2024-07-29 16:50:07 +08:00
import com.aiosman.riderpro.test.TestDatabase
2024-08-11 17:15:17 +08:00
data class AccountProfileEntity(
2024-07-29 00:01:09 +08:00
val id: Int,
val followerCount: Int,
val followingCount: Int,
val nickName: String,
val avatar: String,
val bio: String,
val country: String,
2024-08-11 17:15:17 +08:00
val isFollowing: Boolean
2024-07-29 00:01:09 +08:00
)
2024-08-11 17:15:17 +08:00
//{
// "id": 1,
// "username": "root",
// "nickname": "rider_4351",
// "avatar": "/api/v1/public/default_avatar.jpeg",
// "followingCount": 1,
// "followerCount": 0
//}
data class AccountProfile (
val id: Int,
val username: String,
val nickname: String,
val avatar: String,
val followingCount: Int,
val followerCount: Int,
val isFollowing: Boolean
) {
fun toAccountProfileEntity(): AccountProfileEntity {
return AccountProfileEntity(
id = id,
followerCount = followerCount,
followingCount = followingCount,
nickName = nickname,
avatar = ApiClient.BASE_SERVER + avatar,
bio = "",
country = "Worldwide",
isFollowing = isFollowing
)
}
}
2024-07-29 00:01:09 +08:00
interface AccountService {
2024-08-11 17:15:17 +08:00
suspend fun getMyAccountProfile(): AccountProfileEntity
suspend fun getAccountProfileById(id: Int): AccountProfileEntity
2024-07-31 14:50:55 +08:00
suspend fun getMyAccount(): UserAuth
suspend fun loginUserWithPassword(loginName: String, password: String): UserAuth
suspend fun logout()
suspend fun updateAvatar(uri: String)
suspend fun updateProfile(nickName: String, bio: String)
2024-08-11 17:15:17 +08:00
suspend fun registerUserWithPassword(loginName: String, password: String)
2024-07-29 00:01:09 +08:00
}
class TestAccountServiceImpl : AccountService {
2024-08-11 17:15:17 +08:00
override suspend fun getMyAccountProfile(): AccountProfileEntity {
val resp = ApiClient.api.getMyAccount()
val body = resp.body() ?: throw ServiceException("Failed to get account")
return body.data.toAccountProfileEntity()
2024-07-29 16:50:07 +08:00
}
2024-08-11 17:15:17 +08:00
override suspend fun getAccountProfileById(id: Int): AccountProfileEntity {
val resp = ApiClient.api.getAccountProfileById(id)
val body = resp.body() ?: throw ServiceException("Failed to get account")
return body.data.toAccountProfileEntity()
2024-07-29 00:01:09 +08:00
}
2024-07-31 14:50:55 +08:00
override suspend fun getMyAccount(): UserAuth {
2024-08-11 17:15:17 +08:00
val resp = ApiClient.api.checkToken()
val body = resp.body() ?: throw ServiceException("Failed to get account")
return UserAuth(body.id)
2024-07-31 14:50:55 +08:00
}
override suspend fun loginUserWithPassword(loginName: String, password: String): UserAuth {
2024-08-11 17:15:17 +08:00
val resp = ApiClient.api.login(LoginUserRequestBody(loginName, password))
val body = resp.body() ?: throw ServiceException("Failed to login")
return UserAuth(0, body.token)
2024-07-31 14:50:55 +08:00
}
override suspend fun logout() {
// do nothing
}
override suspend fun updateAvatar(uri: String) {
TestDatabase.accountData = TestDatabase.accountData.map {
if (it.id == 1) {
it.copy(avatar = uri)
} else {
it
}
}
}
2024-08-11 17:15:17 +08:00
2024-07-31 14:50:55 +08:00
override suspend fun updateProfile(nickName: String, bio: String) {
TestDatabase.accountData = TestDatabase.accountData.map {
if (it.id == 1) {
it.copy(nickName = nickName, bio = bio)
} else {
it
}
}
}
2024-08-11 17:15:17 +08:00
override suspend fun registerUserWithPassword(loginName: String, password: String) {
ApiClient.api.register(RegisterRequestBody(loginName, password))
}
2024-07-29 00:01:09 +08:00
}