package com.aiosman.riderpro.data import com.aiosman.riderpro.AppStore import com.aiosman.riderpro.data.api.ApiClient import com.aiosman.riderpro.data.api.LoginUserRequestBody import com.aiosman.riderpro.data.api.RegisterRequestBody import com.aiosman.riderpro.test.TestDatabase import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import okhttp3.RequestBody import okhttp3.RequestBody.Companion.asRequestBody import okhttp3.RequestBody.Companion.toRequestBody import java.io.File data class AccountProfileEntity( val id: Int, val followerCount: Int, val followingCount: Int, val nickName: String, val avatar: String, val bio: String, val country: String, val isFollowing: Boolean ) //{ // "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 + "?token=${AppStore.token}", bio = "", country = "Worldwide", isFollowing = isFollowing ) } } interface AccountService { suspend fun getMyAccountProfile(): AccountProfileEntity suspend fun getAccountProfileById(id: Int): AccountProfileEntity suspend fun getMyAccount(): UserAuth suspend fun loginUserWithPassword(loginName: String, password: String): UserAuth suspend fun logout() suspend fun updateAvatar(uri: String) suspend fun updateProfile(avatar: UploadImage?, nickName: String?, bio: String?) suspend fun registerUserWithPassword(loginName: String, password: String) } class TestAccountServiceImpl : AccountService { override suspend fun getMyAccountProfile(): AccountProfileEntity { val resp = ApiClient.api.getMyAccount() val body = resp.body() ?: throw ServiceException("Failed to get account") return body.data.toAccountProfileEntity() } 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() } override suspend fun getMyAccount(): UserAuth { val resp = ApiClient.api.checkToken() val body = resp.body() ?: throw ServiceException("Failed to get account") return UserAuth(body.id) } override suspend fun loginUserWithPassword(loginName: String, password: String): UserAuth { val resp = ApiClient.api.login(LoginUserRequestBody(loginName, password)) val body = resp.body() ?: throw ServiceException("Failed to login") return UserAuth(0, body.token) } 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 } } } fun createMultipartBody(file: File, filename:String,name: String): MultipartBody.Part { val requestFile = file.asRequestBody("image/*".toMediaTypeOrNull()) return MultipartBody.Part.createFormData(name, filename, requestFile) } override suspend fun updateProfile(avatar: UploadImage?, nickName: String?, bio: String?) { val nicknameField: RequestBody? = nickName?.toRequestBody("text/plain".toMediaTypeOrNull()) val avatarField: MultipartBody.Part? = avatar?.let { createMultipartBody(it.file,it.filename, "avatar") } ApiClient.api.updateProfile(avatarField, nicknameField) } override suspend fun registerUserWithPassword(loginName: String, password: String) { ApiClient.api.register(RegisterRequestBody(loginName, password)) } }