Skip to content

Commit

Permalink
Merge pull request #26 from markschaake/fix-example
Browse files Browse the repository at this point in the history
Fix example
  • Loading branch information
markschaake committed Jan 22, 2015
2 parents 4f509f8 + 4035d7b commit f1d4ad7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions examples/sprest-reactivemongo-example/build.sbt
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
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
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))

}

@@ -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 _)
}

@@ -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 _)

}

0 comments on commit f1d4ad7

Please sign in to comment.