add EnterUseCase

This commit is contained in:
Niktia 2025-02-19 18:56:07 +03:00
parent a926afbd3d
commit 90727a314b
5 changed files with 38 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import android.util.Log
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.patch
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
@ -46,4 +47,17 @@ class UserNetworkDataSource {
}
}
suspend fun enter(value : Long, login : String): Result<Boolean> = withContext(Dispatchers.IO) {
runCatching {
val result = client.patch("$SERVER_ADDRESS/api/open") {
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.math.log
class UserRepoImpl (
@ -28,12 +29,20 @@ class UserRepoImpl (
userList.map { it.toEntity() }
}
}
override suspend fun enter(value: Long, login : String): Result<Boolean> {
return userNetworkDataSource.enter(value, login);
}
fun EntranceDto.toEntity(): EntranceEntity {
return EntranceEntity(
login = login,
name = this.name,
enteredAt = this.enteredAt,
enterType = this.enterType
)
}
}

View File

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

View File

@ -1,6 +1,5 @@
package ru.myitschool.work.domain.user
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable

View File

@ -4,4 +4,5 @@ package ru.myitschool.work.domain.user
interface UserRepo {
suspend fun getUser(login: String) : Result<UserEntity>
suspend fun getEntrancesList(login : String) : Result<List<EntranceEntity>>
suspend fun enter(value : Long, login : String) : Result<Boolean>
}