0
votes

I work on method that returns me for example 'stores' by id and some dynamic query parameters. I've made method that returns me all stores but only by id.

This is my method with id and two optional things:

override fun retriveStoresByDatabaseId(databaseId: String, pageSize: Int, pageNumber: Int): List<StoreDto> {
    val sq = SearchQuery()
    sq.setSearchParam("databaseId", databaseId)
    sq.setPageNumber(pageNumber)
    sq.setPageSize(pageSize)
    val stores: MutableList<StoreDto> = mutableListOf()
    storeRepository.findAll(sq).forEach {
        stores.add(DtoHelper.toDto(it, ""))
    }
    return stores
}

In store model class I have list that have all Attributes(that is the type), in my request I want to send body that is map, and values are my dynamic params. Then I want to get value only these arguments that are equals to values in map. Can You give me some advice how to iter that list with every dynamic param?

1

1 Answers

0
votes

Here is a search that uses a name as input and brings back the id and name

    fun getOneName(name: String): Contact? {
    val db = this.writableDatabase
    val selectQuery = "SELECT  * FROM $TABLE_NAME WHERE $colName = ?"
    db.rawQuery(selectQuery, arrayOf(name)).use { // .use requires API 16
        if (it.moveToFirst()) {
            val result = Contact()
            result.id = it.getInt(it.getColumnIndex(colId))
            result.name = it.getString(it.getColumnIndex(colName))
            return result
        }
    }
    return null
}

Here is one that uses ID as input and brings back data from childList

    fun queryALL(fkI: Int): List<ModelChild> {
    val db = this.writableDatabase
    val childList = ArrayList<ModelChild>()
    val selectQuery = "SELECT  * FROM $CHILD_TABLE WHERE $colCFK = ?"
    //val selectQuery = "SELECT  * FROM ${CHILD_TABLE}"
    val cursor = db.rawQuery(selectQuery, arrayOf(fkI.toString()))
    //val cursor = db.rawQuery(selectQuery, null)
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                val contact = ModelChild()
                contact.idI = Integer.parseInt(cursor.getString(cursor.getColumnIndex(colidI)))
                contact.item = cursor.getString(cursor.getColumnIndex(colItem))
                contact.fkI = Integer.parseInt(cursor.getString(cursor.getColumnIndex(colCFK)))
                childList.add(contact)
            } while (cursor.moveToNext())
        }
    }
    cursor.close()
    return childList
}

hope this helps