Приложение
This commit is contained in:
27
client-mobile/profiles/data/remote/api/ProfileApi.kt
Normal file
27
client-mobile/profiles/data/remote/api/ProfileApi.kt
Normal file
@@ -0,0 +1,27 @@
|
||||
package profiles.data.remote.api
|
||||
|
||||
import okhttp3.MultipartBody
|
||||
import profiles.data.remote.dto.ProfileDto
|
||||
import profiles.data.remote.dto.UpdateProfileRequest
|
||||
import retrofit2.http.*
|
||||
|
||||
interface ProfileApi {
|
||||
@GET("profiles/{id}")
|
||||
suspend fun getProfile(@Path("id") id: String): ProfileDto
|
||||
|
||||
@GET("profiles/me")
|
||||
suspend fun getMyProfile(): ProfileDto
|
||||
|
||||
@PUT("profiles/profile")
|
||||
suspend fun updateProfile(@Body request: UpdateProfileRequest): ProfileDto
|
||||
|
||||
@Multipart
|
||||
@POST("profiles/avatar")
|
||||
suspend fun uploadAvatar(@Part avatar: MultipartBody.Part): ProfileDto
|
||||
|
||||
@DELETE("profiles/avatar")
|
||||
suspend fun removeAvatar(): ProfileDto
|
||||
|
||||
@GET("profiles/search")
|
||||
suspend fun searchProfiles(@Query("q") query: String): List<ProfileDto>
|
||||
}
|
||||
16
client-mobile/profiles/data/remote/dto/ProfileDto.kt
Normal file
16
client-mobile/profiles/data/remote/dto/ProfileDto.kt
Normal file
@@ -0,0 +1,16 @@
|
||||
package profiles.data.remote.dto
|
||||
|
||||
data class ProfileDto(
|
||||
val id: String,
|
||||
val username: String,
|
||||
val displayName: String?,
|
||||
val avatarUrl: String?,
|
||||
val bio: String?,
|
||||
val isOnline: Boolean = false,
|
||||
val lastSeen: String? = null
|
||||
)
|
||||
|
||||
data class UpdateProfileRequest(
|
||||
val displayName: String?,
|
||||
val bio: String?
|
||||
)
|
||||
@@ -0,0 +1,55 @@
|
||||
package profiles.data.repository
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import profiles.data.remote.api.ProfileApi
|
||||
import profiles.data.remote.dto.ProfileDto
|
||||
import profiles.data.remote.dto.UpdateProfileRequest
|
||||
import profiles.domain.repository.ProfileRepository
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import javax.inject.Inject
|
||||
|
||||
class ProfileRepositoryImpl @Inject constructor(
|
||||
private val api: ProfileApi,
|
||||
private val context: Context
|
||||
) : ProfileRepository {
|
||||
|
||||
override suspend fun getProfile(id: String): Result<ProfileDto> = runCatching {
|
||||
api.getProfile(id)
|
||||
}
|
||||
|
||||
override suspend fun getMyProfile(): Result<ProfileDto> = runCatching {
|
||||
api.getMyProfile()
|
||||
}
|
||||
|
||||
override suspend fun updateProfile(displayName: String?, bio: String?): Result<ProfileDto> = runCatching {
|
||||
api.updateProfile(UpdateProfileRequest(displayName, bio))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
override suspend fun removeAvatar(): Result<ProfileDto> = runCatching {
|
||||
api.removeAvatar()
|
||||
}
|
||||
|
||||
private fun uriToFile(uri: Uri): File {
|
||||
val inputStream = context.contentResolver.openInputStream(uri)
|
||||
val file = File(context.cacheDir, "temp_avatar_${System.currentTimeMillis()}.jpg")
|
||||
val outputStream = FileOutputStream(file)
|
||||
inputStream?.use { input ->
|
||||
outputStream.use { output ->
|
||||
input.copyTo(output)
|
||||
}
|
||||
}
|
||||
return file
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user