Blog Infos
Author
Published
Topics
Author
Published

The most common problem that we face when build an e-commerce app is designing and persisting the order and its items data. This article will walk you through the steps to tackle this kind of problem in real production. As an Android developer, we have a very powerful assistant to help us do this, which is Room.

Problem statement
Approach
Actual code steps by steps
@Entity(tableName = "products")
data class Product(
@PrimaryKey
@NonNull
@ColumnInfo(name = "productId")
val id: String,
@ColumnInfo(name = "name")
var name: String?,
@ColumnInfo(name = "price")
var price: Double
)
view raw product.kt hosted with ❤ by GitHub

Define the line item entity

@Entity(tableName = "line_items")
data class LineItem(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "lineItemId")
val id: Long,
@ColumnInfo(name = "productId")
val productId: String,
@ColumnInfo(name = "orderId")
val orderId: String,
@ColumnInfo(name = "quantity")
var quantity: Int,
@ColumnInfo(name = "subtotal")
var subtotal: Double,
)
view raw LineItem.kt hosted with ❤ by GitHub
@Entity(tableName = "orders")
data class Order(
@PrimaryKey
@ColumnInfo(name = "orderId")
val id: String,
@ColumnInfo(name = "status")
var status: String,
@ColumnInfo(name = "address")
var address: String
)
view raw order.kt hosted with ❤ by GitHub
data class LineItemAndProduct(
@Embedded val lineItem: LineItem?,
@Relation(
parentColumn = "productId",
entityColumn = "productId",
entity = Product::class
)
val product: Product?
)

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

Define relationship between Order and Line Item

data class OrderWithLineItems(
@Embedded var order: Order,
@Relation(
parentColumn = "orderId",
entityColumn = "orderId",
entity = LineItem::class
)
val lineItemList: MutableList<LineItemAndProduct>
)
Here is our DAO with database query to get our data
@Dao
interface LineItemDao {
//Get all line item in along with its product
@Transaction
@Query("SELECT * FROM line_items ")
fun getAll(): Flow<List<LineItemAndProduct>>
//Get line item in an order by orderId
@Transaction
@Query("SELECT * FROM line_items WHERE orderId = :orderId")
fun getLineItemInOrder(orderId: String): Flow<List<LineItemAndProduct>>
}
view raw LineItemDao.kt hosted with ❤ by GitHub
@Dao
interface OrderDao {
//Get an order along with its line items by orderId
@Transaction
@Query("SELECT * FROM orders WHERE orderId = :id")
fun getById(id: String): Flow<OrderWithLineItems>
//Get an order by its status
@Transaction
@Query("SELECT * FROM orders WHERE status = :status LIMIT 1 ")
fun getCartWithLineItems(status: String): Flow<OrderWithLineItems>
}
view raw OrderDao.kt hosted with ❤ by GitHub
Conclusion
TL; DR
This article was originally published on proandroiddev.com on March 26, 2022

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
This is the second article in an article series that will discuss the dependency…
READ MORE
blog
Let’s suppose that for some reason we are interested in doing some tests with…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu