title: Slick 3.0: Idiomatic way to GET results from the database inside of Option (Scala Play Framework)
body: <p>I have this code for an <code>API</code> that allows me to retrieve and object from the database and return a <code>JSON</code> object using <code>Slick 3.0</code>: </p>
  
  <pre class="lang-scala prettyprint-override"><code>// Model
  
  case class Thing(id: Option[Int], name: String)
  
  object Thing {
    implicit val teamWrites = Json.writes[Thing]
  }
  
  class Things(tag: Tag) extends Table[Thing](tag, "thing") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def name = column[String]("name")
    def * = (id.?, name) &lt;&gt; ((Thing.apply _).tupled, Thing.unapply)
  }
  
  object Things {
    private val db = Database.forConfig("h2mem1")
    private object things extends TableQuery[Things](tag ⇒ new Things(tag)) { 
      def all = things.result 
    }
    private def filterQuery(id: Int): Query[Things, Thing, Seq] = 
      things.filter(_.id === id)
  
    def findById(id: Int): Future[Thing] = db.run(filterQuery(id).result.head)
  }
  </code></pre>
  
  <pre class="lang-scala prettyprint-override"><code>// Controller
  
  class ThingController extends Controller {
    def get(id: Int) = Action.async {
      Things.findById(id).map(thing =&gt; Ok(Json.obj("result" -&gt; thing)))
    }
  }
  </code></pre>
  
  <p>The problem is that if I query an object that is not in the database, I get an exception. What I would like to do is to get an <code>Option</code> inside of the <code>Future</code> that is returned from the <code>Model</code> in order to be able to write something like this: </p>
  
  <pre class="lang-scala prettyprint-override"><code>// Controller
  
  class ThingController extends Controller {
    def get(id: Int) = Action.async {
      Things.findById(id).map {
        case None =&gt; NotFound(Json.obj("error" -&gt; "Not Found"))) 
        case Some(thing) =&gt; Ok(Json.obj("result" -&gt; thing)))
      }
    }
  }
  </code></pre>
  
  <p>Does it make sense?</p>