android – Best practice to change item based on RoomDB value before inserting it using Kotlin

In my App i use Room database and i have three tables where two of them are connected.

The app just save the user input and insert it in the database. The user actually just insert as input a code and quantity but before inserting that value in my table i have to check if there is in another table (items get from an API) there is yet an item with that code and if it exist i have to set the description to the item the user is inserting from the item in another table get from the API.

Which practice would be the best to do so?

The tables of items get from the API are two:

one with the main item and the code and another that contains all secondary codes which belong to that item, so i have to check if the insert code is in the secondary code table and get it’s primary, while if it’s not i have to check if it’s a primary code or if it’s not i just add it without description.

My code looks like this (i have added all comments which specify what i’m doing)

ArticoliDAO.kt

@Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(articolo: Articolo)

    @Update
    suspend fun update(articolo: Articolo)

    @Query("SELECT * FROM articoli_letti_table WHERE barcode = :key")
    suspend fun get(key: String): Articolo?

    @Query("UPDATE articoli_letti_table SET qta = qta + :qta WHERE barcode = :barcode")
    suspend fun updateQuantity(qta: Float, barcode: String)

    // Inserting the item if not exist in the table, else i'm updagint the quantity
    suspend fun insertOrUpdate(articolo: Articolo) {
        // here i check if the item exist in the table with items synched from my API
        val prodotto = selectProdotto(articolo.barcode)
        // if the item exist i update the description of the item the user is adding
        // and i'm also multiply the quantity by default item quantity
        if (prodotto != null) {
            articolo.desc = prodotto.desc
            articolo.qta = articolo.qta * prodotto.qta
        }
        // here i check if the item has been yet added
        val itemFromDB = get(articolo.barcode)
        if (itemFromDB == null) {
            // adding the item if it's new
            insert(articolo)
        }else {
            // else i sum the quantity of the old item with the new one
            updateQuantity(articolo.qta, articolo.barcode)
        }
    }

    @Query("SELECT codart FROM barcode_prodotti_table WHERE barcode = :barcode")
    suspend fun getCodart(barcode: String): String?

    @Query("SELECT * FROM prodotti_table WHERE codart = :barcode")
    suspend fun getProdotto(barcode: String): Prodotto?

    suspend fun selectProdotto(barcode: String): Prodotto? {
        // here i check if the code insert by user exist in my barcode_table
        // barcode_table contains all secondary codes of an item
        val codart = getCodart(barcode)
        return if (codart.isNullOrEmpty()) {
            // if the secondary code doesn't exist i check if it's the primary code of the item and get it's object
            getProdotto(barcode)
        }else {
            // else i get the item object by setting the primary code in the SELECT get previously or return null
            val prodotto = getProdotto(codart)
            prodotto
        }
    }

My code is working but i would know if it’s okay to do so for the performance and if i could improve the code somewhere