Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.4 kB
5
Indexable
Never
class GetProductUseCase(
    private inventoryRespo: inventoryRespo,
    private stocksRepo: stocksRepo,
    private cacheRequest: CacheRepo,
    private basektRepository: BasektRepository,
) {

    fun invoke(): Produkt()
    {

        val infoOProdukcie = inventoryRespo.get()

        val maxQuantity = stocksRepo(infoOProdukcie.ean)

        val productToAdd = cacheRepo.get(infoOProdukcie.ean)

        val porductInBasket = basektRepository.get(infoOProdukcie.ean)

        val currentQuantity = productToAdd.quantity ?: porductInBasket.quanotquantityy

        if (productToAdd.czyPierwszaSync) {
            ProduktState.PROCESSING
        } else if (productToAdd.error) {
            ProduktState.ERROR
        } else if (currentQuantity == null) {
            ProduktState.NONO
        } else if (currentQuantity < maxQuantity) {
            ProduktState.QUANITITY_ADD
        } else if (currentQuantity >= maxQuantity) {
            ProduktState.QUANITITY_NO_ADD
        }

    }

}


enum class ProduktState {
    NONO, PROCESSING, QUANITITY_ADD, QUANITITY_NO_ADD, ERROR
}


class CacheRepo(
    private stocksRepo: stocksRepo,
    private basektRepository: BasektRepository,
    private api: Api
) {

    val listToSync = listOf<String>()
    
    init {

        listToSync
            .debounce {
                if (czyPierwszaSync) {
                    0
                } else {
                    500
                }
            }.collect { prod ->
                val result = api.addToBasket(prod)
                
                if (result != ładny) {
                    val newProd = listToSync.get(prod)
                    
                    listToSync.rmove(prod)
                    listToSync.add(prod.copy(error = result.error))
                } else {

                    basektRepository.update(result.basket)
                    stocksRepo.update(result.stock)
                    
                    listToSync.remove(prod)   
                }
            }

    }

    fun addToBasket(ena: String) {
        val newProdukt = if (
        val prod = basektRepository.get(ean) == null) {
            new PorductInBasket (
                    quntityToAdd: 1,
            czyPierwszaSync: true
            )
        } else {
            prod.copy(quntity = quntity + 1, czyPierwszaSync = false)
        }

        listToSync.add(newProdukt)
    }
}