Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.79 KB | None | 0 0
  1. trait QueryCommands {
  2.   val records = TableQuery[TODOTable]
  3.   val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
  4.  
  5.   def find(id: Long) = {
  6.     dbConfig.db.run(find_query(id).result.headOption)
  7.   }
  8.  
  9.   def all = {
  10.     dbConfig.db.run(records.result)
  11.   }
  12.  
  13.   def destroy(id: Long) = {
  14.     dbConfig.db.run(find_query(id).delete)
  15.   }
  16.  
  17.   protected
  18.  
  19.   def find_query(id: Long) = {
  20.     records.filter(_.id === id)
  21.   }
  22. }
  23.  
  24. object TODOs extends QueryCommands {
  25.   override val records = TableQuery[TODOTable]
  26.  
  27.   def update(id: Long, values: (String, Boolean)) = {
  28.     var record = find_query(id).map(result => (result.text, result.is_performed))
  29.  
  30.     dbConfig.db.run(record.update(values))
  31.   }
  32.  
  33.   def create(record: TODO) = {
  34.     dbConfig.db.run(records += record)
  35.   }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement