Search⌘ K
AI Features

Some More Routes

Explore how to create new product routes and return all products using Akka-HTTP and streaming in Scala. Understand the challenges of in-memory operations, learn to stream database results, group product data, and optimize endpoints for better performance and scalability.

Creating a new product

Let’s create a new product.

Create
Update
path("products") {
post {
entity(as[Product]) { p =>
complete {
repo.saveProduct(p)
}
}
}
}
Creating a product

As you can see, the function is basically the same as the one for updating a product, except we’re calling a different function from the repository.

Returning all products

Let’s now take a look at the “return all products” endpoint.

Scala
path("products") {
get {
complete {
val products = for {
rows <- repo.loadProducts()
ps <- Future {
rows.toList.groupBy(_._1).map {
case (_, cols) => Product.fromDatabase(cols)
}
}
} yield ps
products.map(_.toList.flatten)
}
}
}

This looks more complicated than the other endpoints. So, what exactly are we doing here? Well, first we load the raw product data from the repository (Line 55 ...

Ask