Профиль, редактирование без аватара
This commit is contained in:
@@ -9,7 +9,7 @@ interface ProfileApi {
|
||||
@GET("profiles/{id}")
|
||||
suspend fun getProfile(@Path("id") id: String): ProfileDto
|
||||
|
||||
@GET("profiles/me")
|
||||
@GET("auth/me")
|
||||
suspend fun getMyProfile(): ProfileDto
|
||||
|
||||
@PUT("profiles/profile")
|
||||
|
||||
@@ -1,16 +1,29 @@
|
||||
package profiles.data.remote.dto
|
||||
|
||||
data class ProfileDto(
|
||||
@com.google.gson.annotations.SerializedName("id", alternate = ["userId"])
|
||||
val id: String,
|
||||
@com.google.gson.annotations.SerializedName("username", alternate = ["userName"])
|
||||
val username: String,
|
||||
@com.google.gson.annotations.SerializedName("displayName")
|
||||
val displayName: String?,
|
||||
@com.google.gson.annotations.SerializedName("avatarUrl", alternate = ["avatar"])
|
||||
val avatarUrl: String?,
|
||||
@com.google.gson.annotations.SerializedName("bio", alternate = ["about"])
|
||||
val bio: String?,
|
||||
@com.google.gson.annotations.SerializedName("isOnline")
|
||||
val isOnline: Boolean = false,
|
||||
val lastSeen: String? = null
|
||||
@com.google.gson.annotations.SerializedName("lastSeen")
|
||||
val lastSeen: String? = null,
|
||||
@com.google.gson.annotations.SerializedName("birthday")
|
||||
val birthday: String? = null
|
||||
)
|
||||
|
||||
data class UpdateProfileRequest(
|
||||
@com.google.gson.annotations.SerializedName("displayName")
|
||||
val displayName: String?,
|
||||
val bio: String?
|
||||
@com.google.gson.annotations.SerializedName("bio")
|
||||
val bio: String?,
|
||||
@com.google.gson.annotations.SerializedName("birthday")
|
||||
val birthday: String? = null
|
||||
)
|
||||
|
||||
@@ -15,30 +15,40 @@ import javax.inject.Inject
|
||||
|
||||
class ProfileRepositoryImpl @Inject constructor(
|
||||
private val api: ProfileApi,
|
||||
private val context: Context
|
||||
private val context: Context,
|
||||
private val serverConfig: core.network.ServerConfig
|
||||
) : ProfileRepository {
|
||||
|
||||
private fun ProfileDto.fixAvatarUrl(): ProfileDto {
|
||||
if (avatarUrl == null || avatarUrl.startsWith("http")) return this
|
||||
val baseUrl = serverConfig.getBaseUrl().removeSuffix("/api/")
|
||||
return copy(avatarUrl = baseUrl + avatarUrl)
|
||||
}
|
||||
|
||||
override suspend fun getProfile(id: String): Result<ProfileDto> = runCatching {
|
||||
api.getProfile(id)
|
||||
api.getProfile(id).fixAvatarUrl()
|
||||
}
|
||||
|
||||
override suspend fun getMyProfile(): Result<ProfileDto> = runCatching {
|
||||
api.getMyProfile()
|
||||
// Сначала получаем ID пользователя из /auth/me
|
||||
val me = api.getMyProfile()
|
||||
// Затем загружаем полный профиль из /profiles/{id}
|
||||
api.getProfile(me.id).fixAvatarUrl()
|
||||
}
|
||||
|
||||
override suspend fun updateProfile(displayName: String?, bio: String?): Result<ProfileDto> = runCatching {
|
||||
api.updateProfile(UpdateProfileRequest(displayName, bio))
|
||||
override suspend fun updateProfile(displayName: String?, bio: String?, birthday: String?): Result<ProfileDto> = runCatching {
|
||||
api.updateProfile(UpdateProfileRequest(displayName, bio, birthday)).fixAvatarUrl()
|
||||
}
|
||||
|
||||
override suspend fun uploadAvatar(uri: Uri): Result<ProfileDto> = runCatching {
|
||||
val file = uriToFile(uri)
|
||||
val requestFile = file.asRequestBody("image/jpeg".toMediaTypeOrNull())
|
||||
val body = MultipartBody.Part.createFormData("avatar", file.name, requestFile)
|
||||
api.uploadAvatar(body)
|
||||
api.uploadAvatar(body).fixAvatarUrl()
|
||||
}
|
||||
|
||||
override suspend fun removeAvatar(): Result<ProfileDto> = runCatching {
|
||||
api.removeAvatar()
|
||||
api.removeAvatar().fixAvatarUrl()
|
||||
}
|
||||
|
||||
private fun uriToFile(uri: Uri): File {
|
||||
|
||||
Reference in New Issue
Block a user