Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix example #26

Merged
merged 1 commit into from
Jan 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/sprest-reactivemongo-example/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/release

resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/service/local/repositories/snapshots/content"

resolvers += "Sprest Snapshots" at "http://sprest.io/snapshots"
resolvers += "Sprest Releases" at "http://sprest.io/releases"

scalaVersion := "2.10.4"

Expand All @@ -17,7 +17,7 @@ libraryDependencies ++= Seq(
"ch.qos.logback" % "logback-classic" % "1.1.2",
"joda-time" % "joda-time" % "2.3",
"org.joda" % "joda-convert" % "1.6",
"sprest" %% "sprest-reactivemongo" % "0.3.4-SNAPSHOT",
"sprest" %% "sprest-reactivemongo" % "0.3.8",
"org.specs2" %% "specs2" % "2.3.10" % "test"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ window.ToDos = ($scope, $resource, $http) ->

$scope.todos = ToDo.query()
$scope.addTodo = ->
toAdd = new ToDo({text: $scope.todoText, done: false, priority: $scope.todoPriority})
toAdd = new ToDo({id: 'new', text: $scope.todoText, done: false, priority: $scope.todoPriority})
toAdd.$save()
$scope.todos.push toAdd
$scope.todoText = null
Expand All @@ -27,7 +27,7 @@ window.Reminders = ($scope, $resource) ->

$scope.reminders = Reminder.query()
$scope.addReminder = ->
toAdd = new Reminder({title: $scope.reminderText, remindAt: new Date().getTime()})
toAdd = new Reminder({id: 'new', title: $scope.reminderText, remindAt: new Date().getTime()})
toAdd.$save()
$scope.reminders.push toAdd
$scope.reminderText = null
Expand Down
17 changes: 10 additions & 7 deletions examples/sprest-reactivemongo-example/src/main/scala/DB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import spray.json.RootJsonFormat
object DB extends ReactiveMongoPersistence {

import reactivemongo.api._
import sprest.models.UUIDStringId
import sprest.models.UniqueSelector
import models._
import scala.concurrent.ExecutionContext
Expand All @@ -17,23 +16,27 @@ object DB extends ReactiveMongoPersistence {
lazy val connection = driver.connection(List("localhost"))
lazy val db = connection("reactive-example")(Main.system.dispatcher)

// Json mapping to / from BSON - in this case we want "_id" from BSON to be
// Json mapping to / from BSON - in this case we want "_id" from BSON to be
// mapped to "id" in JSON in all cases
implicit object JsonTypeMapper extends SprayJsonTypeMapper with NormalizedIdTransformer

abstract class UnsecuredDAO[M <: sprest.models.Model[String]](collName: String)(implicit jsformat: RootJsonFormat[M]) extends CollectionDAO[M, String](db(collName)) {
abstract class UnsecuredDAO[M <: sprest.models.Model[String]](
collName: String)(withNewId: M => M)(
implicit
jsformat: RootJsonFormat[M]) extends CollectionDAO[M, String](db(collName)) {

case class Selector(id: String) extends UniqueSelector[M, String]

override def generateSelector(id: String) = Selector(id)
override protected def addImpl(m: M)(implicit ec: ExecutionContext) = doAdd(m)
override protected def addImpl(m: M)(implicit ec: ExecutionContext) = doAdd(withNewId(m))
override protected def updateImpl(m: M)(implicit ec: ExecutionContext) = doUpdate(m)
override def remove(selector: Selector)(implicit ec: ExecutionContext) = uncheckedRemoveById(selector.id)
}

def newGuid = java.util.UUID.randomUUID.toString

// MongoDB collections:
object ToDos extends UnsecuredDAO[ToDo]("todos") with UUIDStringId
object Reminders extends UnsecuredDAO[Reminder]("reminders") with UUIDStringId
object ToDos extends UnsecuredDAO[ToDo]("todos")(_.copy(id = newGuid))
object Reminders extends UnsecuredDAO[Reminder]("reminders")(_.copy(id = newGuid))

}

Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package sprest.examples.reactivemongo.models

import spray.json._
import spray.json.DefaultJsonProtocol._
import sprest.models._
import org.joda.time.DateTime
import sprest.reactivemongo.typemappers._

case class Reminder(
id: String,
remindAt: DateTime,
title: String,
body: Option[String] = None,
var id: Option[String] = None) extends Model[String]
body: Option[String] = None) extends Model[String]

object Reminder extends ModelCompanion[Reminder, String] {
object Reminder {
import sprest.Formats._
implicit val reminderJsonFormat = jsonFormat4(Reminder.apply _)
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sprest.examples.reactivemongo.models

import spray.json._
import spray.json.DefaultJsonProtocol._
import sprest.models._

import sprest.util.enum._
Expand All @@ -24,11 +25,12 @@ object Priority extends EnumCompanion[Priority] {
BackBurner)
}

case class ToDo(text: String, done: Boolean, var id: Option[String] = None, priority: Priority = Priority.Normal) extends Model[String]

object ToDo extends ModelCompanion[ToDo, String] {
case class ToDo(
id: String,
text: String,
done: Boolean,
priority: Priority = Priority.Normal) extends Model[String]

object ToDo {
implicit val toDoFormat = jsonFormat4(ToDo.apply _)

}