我刚开始使用Play框架和Scala。
要习惯它我正在开发一个歌词网络应用程序,但我在从数据库中提取记录时遇到一些麻烦...
以下是我的 MusicController.scala 。我在def show(id: Long)
收到错误。我调整了我找到的代码here。
我想我需要实施findById(id)
,对吗?但是哪里?怎么样?
我最近一直在使用 Laravel ,我发现很难找到Play with Scala的资源和代码示例。在这种情况下,文档具有我需要的内容,但我对于findById(id)
的实现位置和方式一无所知。我错过了什么吗?
Music.scala
package models
import play.api.libs.json.Json
case class Music(id: Long, title: String, lyrics: String, year: Int)
object Music {
implicit val musicFormat = Json.format[Music]
}
MusicController.scala
package controllers
import javax.inject.Inject
import dal.MusicRepository
import models.Music
import play.api.data.Form
import play.api.data.Forms._
import play.api.data.validation.Constraints._
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json.Json
import play.api.mvc._
import scala.concurrent.{Future, ExecutionContext}
class MusicController @Inject()(repo: MusicRepository, val messagesApi: MessagesApi)
(implicit ec: ExecutionContext) extends Controller with I18nSupport {
def index = Action {
Ok(views.html.musics.index(musicForm))
}
def show(id: Long) = Action {
Music.findById(id).map { music =>
Ok(views.html.musics.show(music))
}.getOrElse(NotFound)
}
val musicForm: Form[CreateMusicForm] = Form {
mapping(
"title" -> nonEmptyText,
"lyrics" -> nonEmptyText,
"year" -> number.verifying(min(0))
)(CreateMusicForm.apply)(CreateMusicForm.unapply)
}
def addMusic = Action.async { implicit request =>
musicForm.bindFromRequest.fold(
errorForm => {
Future.successful(Ok(views.html.musics.index(errorForm)))
},
music => {
repo.create(music.title, music.lyrics, music.year).map { _ =>
Redirect(routes.MusicController.index)
}
}
)
}
def getMusics = Action.async {
repo.list().map { musics =>
Ok(Json.toJson(musics))
}
}
}
case class CreateMusicForm(title: String, lyrics: String, year: Int)
MusicRepository.scala
package dal
import javax.inject.Inject
import models.Music
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile
import scala.concurrent.{Future, ExecutionContext}
class MusicRepository @Inject()(dbConfigProvider: DatabaseConfigProvider)
(implicit ec: ExecutionContext) {
private val dbConfig = dbConfigProvider.get[JdbcProfile]
import dbConfig._
import driver.api._
private class MusicsTable(tag: Tag) extends Table[Music](tag, "musics") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def title = column[String]("title")
def lyrics = column[String]("lyrics")
def year = column[Int]("year")
def * = (id, title, lyrics, year) <>((Music.apply _).tupled, Music.unapply)
}
private val musics = TableQuery[MusicsTable]
def create(title: String, lyrics: String, year: Int): Future[Music] = db.run {
(musics.map(m => (m.title, m.lyrics, m.year))
returning musics.map(_.id)
into ((stuff, id) => Music(id, stuff._1, stuff._2, stuff._3))
) +=(title, lyrics, year)
}
def list(): Future[Seq[Music]] = db.run {
musics.result
}
}
提前谢谢!
答案 0 :(得分:1)
我也设法解决了这个问题:
<强> MusicController.scala 强>
def show(id: Long) = Action.async { implicit request =>
repo.findById(id).map { music =>
Ok(views.html.musics.show(music))
}
}
<强> MusicRepository.scala 强>
def findById(id: Long): Future[Music] = db.run {
musics.filter(_.id === id).result.head
}