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


    Kotlin Backend Developer and Mobile Enthusiast (m/f/d)

    Axel Springer National Media & Tech
    Berlin
    • Full Time
    apply now

    Senior Android Engineer – Outfits Program

    Zalando SE
    Berlin
    • Full Time
    apply now

    (Senior) Android Developer – Machine Learning (w/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

The Evolution of Android Graphics in Android 12/13

Android 12 and 13 both added significant new capabilities to Android platform graphics, including RenderEffect, RuntimeShader, and more. At the same time, RenderScript has been deprecated and we’ve introduced the RenderScript Intrinsics Replacement Toolkit. This…
Watch Video

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocate and Fast Talking YouTuber
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Ad ...
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocat ...
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
Nowadays authentication has become common in almost all apps. And many of us know…
READ MORE
blog
Collections are a set of interfaces and classes that implement highly optimised data structures.…
READ MORE

Leave a Reply

Your email address will not be published.

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

Menu