block un block usecase

This commit is contained in:
Niktia 2025-02-20 11:20:28 +03:00
parent 8c92180a56
commit d5e4411767
5 changed files with 65 additions and 0 deletions

View File

@ -60,4 +60,30 @@ class UserNetworkDataSource {
result.body<Boolean>()
}
}
suspend fun block(login : String) : Result<Boolean> = withContext(Dispatchers.IO) {
runCatching {
val result = client.patch("$SERVER_ADDRESS/api/{login}/block") {
header(HttpHeaders.Authorization, token)
}
Log.d("result", "${result.status}")
if (result.status != HttpStatusCode.OK) {
error("Status ${result.status}")
}
result.body<Boolean>()
}
}
suspend fun unBlock(login : String) : Result<Boolean> = withContext(Dispatchers.IO) {
runCatching {
val result = client.patch("$SERVER_ADDRESS/api/{login}/unblock") {
header(HttpHeaders.Authorization, token)
}
Log.d("result", "${result.status}")
if (result.status != HttpStatusCode.OK) {
error("Status ${result.status}")
}
result.body<Boolean>()
}
}
}

View File

@ -2,6 +2,7 @@ package ru.myitschool.work.data.user
import ru.myitschool.work.domain.user.EntranceEntity
import ru.myitschool.work.domain.user.UserEntity
import ru.myitschool.work.domain.user.UserRepo
import kotlin.contracts.Returns
import kotlin.math.log
@ -44,5 +45,11 @@ class UserRepoImpl (
)
}
override suspend fun block(login: String): Result<Boolean> {
return userNetworkDataSource.block(login);
}
override suspend fun unblock(login: String): Result<Boolean> {
return userNetworkDataSource.block(login);
}
}

View File

@ -0,0 +1,15 @@
package ru.myitschool.work.domain.user
import ru.myitschool.work.data.auth.AuthStorageDataSource
import ru.myitschool.work.data.user.UserDto
public class BlockUseCase (
private val repo: UserRepo,
private val authStorageDataSource : AuthStorageDataSource
){
private fun getUserFromStorage() : UserDto? {
return authStorageDataSource.userInfo
}
suspend operator fun invoke(value : String) = repo.block(getUserFromStorage()?.login!!)
}

View File

@ -0,0 +1,15 @@
package ru.myitschool.work.domain.user
import ru.myitschool.work.data.auth.AuthStorageDataSource
import ru.myitschool.work.data.user.UserDto
public class UnBlockUseCase (
private val repo: UserRepo,
private val authStorageDataSource : AuthStorageDataSource
){
private fun getUserFromStorage() : UserDto? {
return authStorageDataSource.userInfo
}
suspend operator fun invoke(value : String) = repo.unblock(getUserFromStorage()?.login!!)
}

View File

@ -5,4 +5,6 @@ interface UserRepo {
suspend fun getUser(login: String) : Result<UserEntity>
suspend fun getEntrancesList(login : String) : Result<List<EntranceEntity>>
suspend fun enter(value : String, login : String) : Result<Boolean>
suspend fun block(login: String): Result<Boolean>
suspend fun unblock(login: String): Result<Boolean>
}