2017-10-05 15 views
0

下面的代码编译但它不存储任何通过Web界面创建的博客数据。除了blogService之外,我有userService和authTokenService,它们都可以正常工作,所以我认为postgres方面没有任何问题。Slick3:博客功能实现,编译,但不会将数据存储在数据库中

我是新来的斯卡拉和光滑的,所以我想知道我可能会忽略非常微不足道或严重的错误。

你看到代码中有什么问题吗?或者你会告诉我,我能做些什么来隔离问题? (我试图尽量减少和简化代码隔离问题,但仍然无法想出解决办法。)

BlogForm.scala:

object BlogForm { 

    val form = Form(
    mapping(
     "title" -> nonEmptyText, 
     "content" -> nonEmptyText 
    )(Data.apply)(Data.unapply) 
) 

    case class Data(
    title: String, 
    content: String 
) 
} 

BlogController.scala:

class BlogController @Inject() (
    val messagesApi: MessagesApi, 
    silhouette: Silhouette[DefaultEnv], 
    userService: UserService, 
    blogService: BlogService, 
    implicit val webJarAssets: WebJarAssets) 
    extends Controller with I18nSupport { 

    def submit: Action[AnyContent] = silhouette.SecuredAction.async { implicit request => 
    BlogForm.form.bindFromRequest.fold(
     form => Future.successful(BadRequest(views.html.blog.blog(request.identity, form))), 
     data => { 
     blogService.create(data.title, data.content, request.identity) 
     Future.successful(Ok(views.html.index(Some(request.identity)))) 
     } 
    ) 
    } 
} 

BlogServiceImpl.scala:

class BlogServiceImpl @Inject() (blogDAO: BlogDAO, clock: Clock) extends BlogService { 

    def create(title: String, content: String, user: User): Future[Int] = { 
    blogDAO.save(Blog(
     // id = None, 
     title = title, 
     content = content, 
     userID = user.userID, 
     createdAt = clock.now 
    )) 
    } 
} 

BlogDAOImpl.scala:

class BlogDAOImpl @Inject() (protected val dbConfigProvider: DatabaseConfigProvider) extends BlogDAO { 

    val dbConfig: DatabaseConfig[JdbcProfile] = dbConfigProvider.get[JdbcProfile] 
    val db: JdbcBackend#DatabaseDef = dbConfig.db 

    import dbConfig.driver.api._ 

    def save(blog: Blog): Future[Int] = { 
    db.run(
     blogs += DbBlog(blog.title, blog.content, blog.userID.toString, blog.createdAt.toString)) 
    } 
} 
object BlogDAOImpl { 

    private val blogs = TableQuery[BlogTable] 

} 

BlogTable.scala:

case class DbBlog(
    title: String, 
    content: String, 
    userID: String, 
    createdAt: String 
) 


class BlogTable(tag: Tag) extends Table[DbBlog](tag, "blogs") { 

    def title: Rep[String] = column[String]("title") 

    def content: Rep[String] = column[String]("content") 

    def userID: Rep[String] = column[String]("uesr_id") 

    def createdAt: Rep[String] = column[String]("created_at") 

    override def * : ProvenShape[DbBlog] = (title, content, userID, createdAt) <> (DbBlog.tupled, DbBlog.unapply) 

} 

我会在这里省略了绑定文件和博客模式。

再次编译代码。什么可能导致这种错误?

任何建议,将不胜感激。

+1

你需要在'blogService.create'之后'映射'。这个未来可能没有完成,你**已经/立即**用'Future.successful'返回一个答案。所以,试试这个:'blogService.create(...)。map {res => Ok(views.html.index(Some(request.identity)))}'。 –

+1

你的解释总是有道理的,它立即解决了问题。请将它作为答案发布,然后我可以接受它。谢谢! – hirofujitaaki

+0

很高兴帮助,@hirofujitaaki。我发布它作为答案。 –

回答

0

您需要mapblogService.create。该未来可能无法完成,并且您已经/立即用Future.successful返回答案。所以,试试这个:

blogService.create(...).map{ res => 
    Ok(views.html.index(Some(request.identity))) 
} 
+1

再次谢谢你! – hirofujitaaki

0

BlogDAOImpl.scala:

试试这个

@Singleton 
class BlogDAOImpl @Inject() (protected val dbConfigProvider: DatabaseConfigProvider) extends BlogDAO { 
    val dbConfig: DatabaseConfig[JdbcProfile] = dbConfigProvider.get[JdbcProfile] 
    val db: JdbcBackend#DatabaseDef = dbConfig.db 

    import dbConfig.driver.api._ 

    private val blogs = TableQuery[BlogTable] // this line is here as you can't create object of a @Singleton annotated class 

    def save(blog: Blog): Future[Int] = { 
    db.run(
     blogs += DbBlog(blog.title, blog.content, blog.userID.toString, blog.createdAt.toString)) 
    } 
} 

你需要一个使用这个类的对象注入类BlogDAOImpl

+0

谢谢你的回复。我没有尝试它,但由于我在伴侣对象中创建了'val blog',所以BlogDAOImpl类应该可以访问它。 – hirofujitaaki

+0

我在类里面写了'val blogs',因为如果你在一个类中使用'@ Singleton'注解,你不能创建一个伴随对象,即这个类不能被多次实例化。 – hirakJS

相关问题