All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ReadKtorRolesRepo.kt Maven / Gradle / Ivy

The newest version!
package dev.inmo.kroles.repos.repos.ktor.client

import dev.inmo.kroles.repos.BaseRoleSubject
import dev.inmo.kroles.repos.ReadRolesRepo
import dev.inmo.kroles.repos.repos.ktor.RolesKtorConstants
import dev.inmo.kroles.roles.BaseRole
import dev.inmo.micro_utils.ktor.common.buildStandardUrl
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.asUrlQueryArrayParts
import dev.inmo.micro_utils.pagination.asUrlQueryParts
import dev.inmo.micro_utils.repos.ktor.common.getAllRoute
import dev.inmo.micro_utils.repos.ktor.common.reversedParameterName
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.http.*

class ReadKtorRolesRepo(
    private val client: HttpClient,
    private val rootPath: String = RolesKtorConstants.DefaultRolesRootPathPart
) : ReadRolesRepo {
    private val BaseRoleSubject.queryParameter
        get() = when (this) {
            is BaseRoleSubject.OtherRole -> RolesKtorConstants.SubjectRoleQueryParameterName to role.plain.encodeURLParameter()
            is BaseRoleSubject.Direct -> RolesKtorConstants.SubjectIdentifierQueryParameterName to identifier.encodeURLParameter()
        }
    override suspend fun getDirectSubjects(
        role: BaseRole
    ): List = client.get(
        buildStandardUrl(
            rootPath,
            RolesKtorConstants.GetDirectSubjectsPathPart,
            RolesKtorConstants.RoleQueryParameterName to role.plain.encodeURLParameter()
        )
    ).body()

    override suspend fun getDirectRoles(
        subject: BaseRoleSubject
    ): List = client.get(
        buildStandardUrl(
            rootPath,
            RolesKtorConstants.GetDirectRolesPathPart,
            subject.queryParameter
        )
    ).body()

    override suspend fun getAll(): Map> = client.get(
        buildStandardUrl(
            rootPath,
            RolesKtorConstants.GetAllPathPart
        )
    ).body>>>().toMap()

    override suspend fun getAllRoles(
        subject: BaseRoleSubject
    ): List = client.get(
        buildStandardUrl(
            rootPath,
            RolesKtorConstants.GetAllRolesPathPart,
            subject.queryParameter
        )
    ).body()

    override suspend fun getAllRolesByPagination(
        pagination: Pagination,
        reversed: Boolean
    ): PaginationResult {
        return client.get(
            buildStandardUrl(
                rootPath,
                RolesKtorConstants.GetAllRolesByPaginationPathPart,
                *pagination.asUrlQueryArrayParts,
                reversedParameterName to reversed.toString()
            )
        ).body()
    }

    override suspend fun getAllSubjectsByPagination(pagination: Pagination, reversed: Boolean): PaginationResult {
        return client.get(
            buildStandardUrl(
                rootPath,
                RolesKtorConstants.GetAllSubjectsRolesByPaginationPathPart,
                pagination.asUrlQueryParts + (
                    reversedParameterName to reversed.toString()
                )
            )
        ).body()
    }

    override suspend fun contains(
        subject: BaseRoleSubject,
        role: BaseRole
    ): Boolean = client.get(
        buildStandardUrl(
            rootPath,
            RolesKtorConstants.ContainsPathPart,
            subject.queryParameter,
            RolesKtorConstants.RoleQueryParameterName to role.plain.encodeURLParameter()
        )
    ).body()

    override suspend fun containsAny(
        subject: BaseRoleSubject,
        roles: List
    ): Boolean = client.get(
        buildStandardUrl(
            rootPath,
            RolesKtorConstants.ContainsAnyPathPart,
            subject.queryParameter,
            *roles.map {
                RolesKtorConstants.RoleQueryParameterName to it.plain
            }.toTypedArray()
        )
    ).body()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy