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

80 lines
2.0 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
2024-08-24 23:11:20 +08:00
import com.aiosman.riderpro.entity.AccountProfileEntity
2024-07-29 00:01:09 +08:00
2024-07-30 16:57:25 +08:00
data class UserAuth(
val id: Int,
val token: String? = null
)
2024-08-24 23:11:20 +08:00
/**
* 用户相关 Service
*/
2024-07-29 00:01:09 +08:00
interface UserService {
2024-08-24 23:11:20 +08:00
/**
* 获取用户信息
* @param id 用户ID
* @return 用户信息
*/
2024-08-11 17:15:17 +08:00
suspend fun getUserProfile(id: String): AccountProfileEntity
2024-08-24 23:11:20 +08:00
/**
* 关注用户
* @param id 用户ID
*/
2024-08-11 17:15:17 +08:00
suspend fun followUser(id: String)
2024-08-24 23:11:20 +08:00
/**
* 取消关注用户
* @param id 用户ID
*/
2024-08-11 17:15:17 +08:00
suspend fun unFollowUser(id: String)
2024-08-24 23:11:20 +08:00
/**
* 获取用户列表
* @param pageSize 分页大小
* @param page 页码
* @param nickname 昵称搜索
* @return 用户列表
*/
2024-08-17 17:40:21 +08:00
suspend fun getUsers(
pageSize: Int = 20,
page: Int = 1,
nickname: String? = null
): ListContainer<AccountProfileEntity>
2024-07-31 14:50:55 +08:00
2024-07-29 00:01:09 +08:00
}
2024-08-24 23:11:20 +08:00
class UserServiceImpl : UserService {
2024-08-11 17:15:17 +08:00
override suspend fun getUserProfile(id: String): AccountProfileEntity {
val resp = ApiClient.api.getAccountProfileById(id.toInt())
val body = resp.body() ?: throw ServiceException("Failed to get account")
return body.data.toAccountProfileEntity()
}
override suspend fun followUser(id: String) {
val resp = ApiClient.api.followUser(id.toInt())
return
}
override suspend fun unFollowUser(id: String) {
val resp = ApiClient.api.unfollowUser(id.toInt())
return
2024-07-29 00:01:09 +08:00
}
2024-08-17 17:40:21 +08:00
override suspend fun getUsers(
pageSize: Int,
page: Int,
nickname: String?
): ListContainer<AccountProfileEntity> {
val resp = ApiClient.api.getUsers(page, pageSize, nickname)
val body = resp.body() ?: throw ServiceException("Failed to get account")
return ListContainer<AccountProfileEntity>(
list = body.list.map { it.toAccountProfileEntity() },
page = body.page,
total = body.total,
pageSize = body.pageSize
)
}
2024-07-29 00:01:09 +08:00
}