O'Reilly logo

Building Applications with Scala by Diego Pacheco

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with services

Services are where we put the business logic. We will look at reactive persistence in Chapter 6, Persistence with Slick. Right now, we don't have a database to persist information, so, for now, we will do an in-memory persistence.

First we will define the contract of our services. This is the Base API that we will use in the controllers. Let's take a look at the following trait in BaseService.scala:

 package services import java.util.concurrent.atomic.AtomicLong import scala.collection.mutable.HashMap trait BaseService[A] { var inMemoryDB = new HashMap[Long,A] var idCounter = new AtomicLong(0) def insert(a:A):Long def update(id:Long,a:A):Boolean def remove(id:Long):Boolean def findById(id:Long):Option[A] def findAll():Option[List[A]] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required