我想构建一个简单的RESTful服务,对数据库执行CRUD操作并返回JSON。我有一个服务秉承的API这样油滑 - 如果数据库不包含结果怎么办
GET mydomain.com/predictions/some%20string
我用它包含了我创建对象的关联预测如下方法的DAO:
def getPrediction(rawText: String): Prediction = {
val predictionAction = predictions.filter{_.rawText === rawText}.result
val header = predictionAction.head
val f = db.run(header)
f.onComplete{case pred => pred}
throw new Exception("Oops")
}
但是,这不能成为对,所以我开始阅读有关Option
。我相应地更改了我的代码:
def getPrediction(rawText: String): Option[Prediction] = {
val predictionAction = predictions.filter{_.rawText === rawText}.result
val header = predictionAction.headOption
val f = db.run(header)
f.onSuccess{case pred => pred}
None
}
这仍然不太合适。调用这些过滤器,返回结果并处理任何不确定性的最佳方法是什么?
哪个版本的浮油? – marcospereira
@marcospereira'3.1.1'。我想我通过添加'return Some(red)'和'return None'来计算出来。我读了一些关于'Option'的文章,并使用'.isEmpty'来决定做什么。尽管文档看起来很模糊,但我仍然对使用'Slick'的最佳方式感到好奇。例如,还有'DBIO'序列。不确定哪个最适合使用。 –