feat: Доделаны запросы на сервер + фикс некоторых ошибок

This commit is contained in:
yastruckov 2025-02-19 18:48:41 +03:00
parent b839938529
commit 2e52454f1c
15 changed files with 62 additions and 26 deletions

View File

@ -1,5 +1,5 @@
package ru.myitschool.work.core
// БЕРИТЕ И ИЗМЕНЯЙТЕ ХОСТ ТОЛЬКО ЗДЕСЬ И НЕ БЕРИТЕ ИЗ ДРУГИХ МЕСТ. ФАЙЛ ПЕРЕМЕЩАТЬ НЕЛЬЗЯ
object Constants {
const val SERVER_ADDRESS = "http://192.168.1.145:8080"
const val SERVER_ADDRESS = "http://10.6.66.78:8080"
}

View File

@ -2,8 +2,10 @@ package ru.myitschool.work.data.deleteEmployee
import ru.myitschool.work.domain.deleteEmployee.DeleteEmployeeRepo
class DeleteEmployeeRepoImpl : DeleteEmployeeRepo {
class DeleteEmployeeRepoImpl(
private val networkDataSource: DeleteEmployeeNetworkDataSource
) : DeleteEmployeeRepo {
override suspend fun deleteEmployee(login: String): Result<Unit> {
TODO("Not yet implemented")
return networkDataSource.deleteEmployee(login)
}
}

View File

@ -21,11 +21,11 @@ class EmployeeListNetworkDataSource(
private val client = NetworkModule.httpClient
private val userDataStoreManager = UserDataStoreManager.getInstance(context)
suspend fun getInfo():Result<EmployeePagingDTO> = withContext(Dispatchers.IO){
suspend fun getList(pageNum: Int, pageSize: Int):Result<EmployeePagingDTO> = withContext(Dispatchers.IO){
runCatching {
val username = userDataStoreManager.usernameFlow.first()
val password = userDataStoreManager.passwordFlow.first()
val result = client.get("${Constants.SERVER_ADDRESS}/api/employee/all"){
val result = client.get("${Constants.SERVER_ADDRESS}/api/employee/all?page=$pageNum&size=$pageSize"){
headers{
basicAuth(username, password)
}

View File

@ -3,8 +3,25 @@ package ru.myitschool.work.data.employeeList
import ru.myitschool.work.domain.employeeList.EmployeeListRepo
import ru.myitschool.work.entities.EmployeeEntity
class EmployeeListRepoImpl : EmployeeListRepo {
override suspend fun getList(): Result<List<EmployeeEntity>> {
TODO("Not yet implemented")
class EmployeeListRepoImpl(
private val networkDataSource: EmployeeListNetworkDataSource
) : EmployeeListRepo {
override suspend fun getList(
pageNum: Int,
pageSize: Int
): Result<List<EmployeeEntity>> {
return networkDataSource.getList(pageNum, pageSize ).map { pagingDTO->
pagingDTO.content?.mapNotNull { dto ->
EmployeeEntity(
id = dto.id ?: return@mapNotNull null,
login = dto.login ?: return@mapNotNull null ,
name = dto.name ?: return@mapNotNull null,
authority = dto.authority ?: return@mapNotNull null,
photoUrl = dto.photoUrl,
position = dto.position ?: return@mapNotNull null,
qrEnabled = dto.qrEnabled ?: false
)
} ?: return Result.failure(IllegalStateException("List parse error"))
}
}
}
}

View File

@ -16,7 +16,8 @@ class AllEntranceListRepoImpl(
id = dto.id ?: return@mapNotNull null,
scanTime = dto.scanTime ?: return@mapNotNull null,
readerName = dto.readerName ?: return@mapNotNull null,
type = dto.type ?: return@mapNotNull null
type = dto.type ?: return@mapNotNull null,
entryType = dto.entryType ?: return@mapNotNull null,
)
}?: return Result.failure(IllegalStateException("List parse error"))
}

View File

@ -14,6 +14,7 @@ class EmployeeEntranceListRepoImpl(
scanTime = dto.scanTime ?: return@mapNotNull null,
readerName = dto.readerName ?: return@mapNotNull null,
type = dto.type ?: return@mapNotNull null,
entryType = dto.entryType ?: return@mapNotNull null
)
}?: return Result.failure(IllegalStateException("List parse error"))
}

View File

@ -3,8 +3,7 @@ package ru.myitschool.work.data.entrance.lastEntrance
import android.content.Context
import io.ktor.client.call.body
import io.ktor.client.request.basicAuth
import io.ktor.client.request.post
import io.ktor.client.statement.bodyAsText
import io.ktor.client.request.get
import io.ktor.http.HttpStatusCode
import io.ktor.http.headers
import kotlinx.coroutines.Dispatchers
@ -24,7 +23,7 @@ class LastEntranceNetworkDataSource(
runCatching {
val username = userDataStoreManager.usernameFlow.first()
val password = userDataStoreManager.passwordFlow.first()
val result = client.post("${Constants.SERVER_ADDRESS}/api/entrance/last"){
val result = client.get("${Constants.SERVER_ADDRESS}/api/entrance/last"){
headers{
basicAuth(username, password)
}
@ -32,7 +31,6 @@ class LastEntranceNetworkDataSource(
if (result.status != HttpStatusCode.OK) {
error("Status ${result.status}")
}
println(result.bodyAsText())
result.body()
}
}

View File

@ -12,7 +12,8 @@ class LastEntranceRepoImpl(
id = dto.id ?: 0,
scanTime = dto.scanTime,
readerName = dto.readerName ?: "",
type = dto.type ?: ""
type = dto.type ?: "",
entryType = dto.entryType
)
}
}

View File

@ -3,5 +3,7 @@ package ru.myitschool.work.domain.employeeList
import ru.myitschool.work.entities.EmployeeEntity
interface EmployeeListRepo {
suspend fun getList() : Result<List<EmployeeEntity>>
suspend fun getList(
pageNum: Int,
pageSize: Int ) : Result<List<EmployeeEntity>>
}

View File

@ -3,5 +3,5 @@ package ru.myitschool.work.domain.employeeList
class GetEmployeeListUseCase(
private val repo: EmployeeListRepo
) {
suspend operator fun invoke() = repo.getList()
suspend operator fun invoke(pageNum: Int, pageSize: Int) = repo.getList(pageNum, pageSize)
}

View File

@ -10,5 +10,6 @@ data class EmployeeEntranceDTO(
@SerialName("id") val id : Int?,
@SerialName("entryTime") @Serializable(with = DateSerializer::class) val scanTime : Date?,
@SerialName("readerName") val readerName: String?,
@SerialName("type") val type: String?
@SerialName("type") val type: String?,
@SerialName("entryType") val entryType: String?
)

View File

@ -2,8 +2,9 @@ package ru.myitschool.work.entities
import java.util.Date
data class EmployeeEntranceEntity(
val id : Int,
val id : Int?,
val scanTime : Date?,
val readerName: String,
val type: String
val readerName: String?,
val type: String?,
val entryType: String?
)

View File

@ -4,9 +4,11 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import ru.myitschool.work.R
import ru.myitschool.work.databinding.ItemVisitBinding
import ru.myitschool.work.entities.EmployeeEntranceEntity
import ru.myitschool.work.utils.dateConverter
import ru.myitschool.work.utils.monthConverter
import ru.myitschool.work.utils.timeConverter
class EmployeeEntranceListAdapter : PagingDataAdapter<EmployeeEntranceEntity, EmployeeEntranceListAdapter.ViewHolder>(DiffUtil) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : ViewHolder{
@ -27,14 +29,23 @@ class EmployeeEntranceListAdapter : PagingDataAdapter<EmployeeEntranceEntity, Em
private val binding: ItemVisitBinding,
) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: EmployeeEntranceEntity) {
binding.readerName.text = item.readerName
binding.timeVisit.text = dateConverter(item.scanTime)
binding.visitReaderId.text = item.readerName
binding.visitDate.text = monthConverter(item.scanTime)
binding.visitTime.text = timeConverter(item.scanTime)
binding.visitDirection.text = item.entryType
if(item.type == "smartphone"){
binding.visitType.setImageResource(R.drawable.logo_visit_scan)
}
else{
binding.visitType.setImageResource(R.drawable.logo_visit_card)
}
}
}
object DiffUtil : androidx.recyclerview.widget.DiffUtil.ItemCallback<EmployeeEntranceEntity>() {
override fun areItemsTheSame(oldItem: EmployeeEntranceEntity, newItem: EmployeeEntranceEntity): Boolean {
return oldItem.scanTime == newItem.scanTime
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: EmployeeEntranceEntity, newItem: EmployeeEntranceEntity): Boolean {
return oldItem == newItem

View File

@ -16,4 +16,5 @@
<string name="result_null_text">Operation was cancelled</string>
<string name="password_hint">Enter the password</string>
<string name="history_title">Visit history</string>
<string name="login_unauthorized">Incorrect login or password</string>
</resources>

View File

@ -18,7 +18,7 @@
<string name="close_btn">Закрыть</string>
<string name="password_hint">Введите пароль</string>
<string name="history_title">История посещений</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="login_unauthorized">Неправильное имя пользователя или пароль</string>
</resources>