当前位置: 动力学知识库 > 问答 > 编程问答 >

playframework - How to fetch records from the database using Play with Scala and Slick

问题描述:

I am just starting with the Play framework and Scala.

To get used to it I am developing a lyrics web app, but I am having some trouble fetching records from the database though...

Below is my MusicController.scala. I am getting an error at def show(id: Long). I adapted the code I found here.

I guess I need to implement findById(id), right? But where? And how?

I have been using Laravel lately, and I am finding it much harder to find resources and code samples for Play with Scala. In this case, the docs have what I need, but I am left in the dark regarding where and how to implement the findById(id). Am I missing something?

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

}

}

Thank you in advance!

网友答案:

I have managed to solve it likewise:

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
  }
分享给朋友:
您可能感兴趣的文章:
随机阅读: