-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFactory.scala
43 lines (37 loc) · 1.45 KB
/
Factory.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package internbookmark.helper
import org.joda.time.LocalDateTime
import internbookmark.model._
import internbookmark.repository
import internbookmark.repository.{Context, TitleExtractor}
import scala.util.Random
object Factory {
def createUser(
name: String = Random.nextInt().toString
)(implicit ctx: Context): User =
repository.Users.createByName(name)
def createEntry(
url: String = "http://example.com?" + Random.nextInt().toString,
title: String = Random.nextInt().toString
)(implicit ctx: Context): Entry =
new repository.Entries {
val titleExtractor = new TitleExtractor {
override protected def fetch(url: String) = None
override def extract(url: String) = Some(title)
}
}.createByUrl(url)
def createBookmark(
optionalUser: Option[User] = None,
optionalEntry: Option[Entry] = None,
comment: String = "",
updatedAt: LocalDateTime = new LocalDateTime()
)(implicit ctx: Context): Bookmark = {
import slick.driver.MySQLDriver.api._
import com.github.tototoshi.slick.MySQLJodaSupport._
val user = optionalUser.getOrElse(createUser())
val entry = optionalEntry.getOrElse(createEntry())
repository.Bookmarks.createOrUpdate(user, entry, comment)
val bookmark = repository.Bookmarks.findByEntry(user, entry).get
Context.runDBIO(sqlu"""UPDATE bookmark SET updated_at = ${updatedAt} where id = ${bookmark.id}""")
repository.Bookmarks.findByEntry(user, entry).get
}
}