I have a list of dataClass OrderInProgress and I need to sum the prices of similar products and count them to return a list of CartDetail, similar to the query from room database. How can I do it?
@Entity(tableName = "order_in_progress")
data class OrderInProgress(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
var id: Int = 0,
@ColumnInfo(name = "product")
var product: String,
@ColumnInfo(name = "price")
var price: Double
)
data class CartDetail(
@ColumnInfo(name = "product")
val product: String,
@ColumnInfo(name = "sum_price")
val sumPrice: Double,
@ColumnInfo(name = "product_count")
val productCount: Int
)
this is the room query
@Query(
"SELECT product, SUM(price) as sum_price, COUNT(product) as product_count " +
"FROM order_in_progress GROUP BY product"
)
fun getCart(): Flow<List<CartDetail>?>