Last active
August 29, 2015 14:01
-
-
Save DeaconDesperado/9123509cabb72ce51a85 to your computer and use it in GitHub Desktop.
Revisions
-
DeaconDesperado revised this gist
May 5, 2014 . 1 changed file with 23 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,18 +10,27 @@ import com.gn.gncore.GncoreConfig object BSONPlaceSerializer extends App{ implicit object MapReader extends BSONReader[BSONDocument,Map[String, String]]{ def read(bson: BSONDocument): Map[String, String] = { val elements = bson.elements.map { tuple => tuple._1 -> tuple._2.seeAsTry[String].get } elements.toMap[String, String] } } private val timePattern = DateTimeFormat.forPattern("HH:mm:ss") implicit object TimeBlockBSONReader extends BSONDocumentReader[TimeBlock] { def read(doc: BSONDocument):TimeBlock = { val opensString = doc.getAs[String]("opens").get val closesString = doc.getAs[String]("closes").get val label = doc.getAs[String]("label") val opens = LocalTime.parse(opensString, timePattern) val closes = LocalTime.parse(closesString, timePattern) TimeBlock(opens,closes,label) } } @@ -46,6 +55,7 @@ object BSONPlaceSerializer extends App{ def read(doc: BSONDocument) :Place = { val dtString = doc.getAs[String]("created").get val created = new DateTime(dtString) val linksBSON = doc.getAs[BSONDocument]("links").get Place( doc.getAs[String]("id").get, @@ -59,8 +69,8 @@ object BSONPlaceSerializer extends App{ doc.getAs[String]("description").get, doc.getAs[String]("business_type").get, created, doc.getAs[Hours]("hours"), linksBSON.as[Map[String, String]], doc.getAs[Boolean]("hidden").get ) } @@ -72,7 +82,7 @@ object BSONPlaceSerializer extends App{ "opens" -> block.opens.toString(timePattern), "closes" -> block.closes.toString(timePattern), "label" -> block.label ) } } @@ -93,11 +103,11 @@ object BSONPlaceSerializer extends App{ implicit object PlaceBSONWriter extends BSONDocumentWriter[Place] { def write(place:Place): BSONDocument = { val links = BSONDocument(place.links.map { tuple => tuple._1 -> BSONString(tuple._2) }) BSONDocument( "id" -> place.id, "name" -> place.name, @@ -111,13 +121,13 @@ object BSONPlaceSerializer extends App{ "business_type" -> place.business_type, "created" -> place.created.toString(), "hours" -> place.hours, "links" -> links, "hidden" -> place.hidden ) } } import scala.concurrent.ExecutionContext.Implicits.global val driver = new MongoDriver val connection = driver.connection(List("localhost")) val placeDoc = BSON.write(placeFixture) @@ -132,5 +142,4 @@ object BSONPlaceSerializer extends App{ println(place) } } } -
DeaconDesperado revised this gist
May 5, 2014 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,11 @@ package com.gn.gncore.models import java.util.UUID import org.joda.time.DateTime import org.joda.time.LocalTime /** Core place model throughout service */ case class Place( id:String, name:String, @@ -34,4 +42,4 @@ case class TimeBlock( opens:LocalTime, closes:LocalTime, label:Option[String] = None ) -
DeaconDesperado revised this gist
May 5, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ case class Place( hours:Option[Hours] = None, links:Map[String, String] = Map(), hidden:Boolean = false ) /** Hours property for places. Every day has an optional list of TimeBlock instances -
DeaconDesperado created this gist
May 5, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ case class Place( id:String, name:String, street:String, city:String, state:String, zip:String, phone:String, location:List[Double], description:String, business_type:String, created:DateTime, hours:Option[Hours] = None, links:Map[String, String] = Map(), hidden:Boolean = false ) extends GnModel /** Hours property for places. Every day has an optional list of TimeBlock instances */ case class Hours( monday:Option[List[TimeBlock]] = None, tuesday:Option[List[TimeBlock]] = None, wednesday:Option[List[TimeBlock]] = None, thursday:Option[List[TimeBlock]] = None, friday:Option[List[TimeBlock]] = None, saturday:Option[List[TimeBlock]] = None, sunday:Option[List[TimeBlock]] = None ) /** A TimeBlock represents a discrete opening and closing time */ case class TimeBlock( opens:LocalTime, closes:LocalTime, label:Option[String] = None ) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,136 @@ package com.gn.gncore.models.serializers.bson import com.gn.gncore.models.{Place, TimeBlock, Hours, placeFixture} import org.joda.time.DateTime import org.joda.time.LocalTime import org.joda.time.format.DateTimeFormat import reactivemongo.bson._ import reactivemongo.api.MongoDriver import com.gn.gncore.GncoreConfig object BSONPlaceSerializer extends App{ private val timePattern = DateTimeFormat.forPattern("HH:mm:ss") implicit object TimeBlockBSONReader extends BSONDocumentReader[TimeBlock] { def read(doc: BSONDocument):TimeBlock = { val opensString = doc.getAs[String]("opens").get val closesString = doc.getAs[String]("closes").get val label = doc.getAs[String]("label") val opens = LocalTime.parse(opensString, timePattern) val closes = LocalTime.parse(closesString, timePattern) TimeBlock(opens,closes,label) } } implicit object HoursBSONReader extends BSONDocumentReader[Hours] { def read(doc: BSONDocument):Hours = { Hours( doc.getAs[List[TimeBlock]]("monday"), doc.getAs[List[TimeBlock]]("tuesday"), doc.getAs[List[TimeBlock]]("wednesday"), doc.getAs[List[TimeBlock]]("thursday"), doc.getAs[List[TimeBlock]]("friday"), doc.getAs[List[TimeBlock]]("saturday"), doc.getAs[List[TimeBlock]]("sunday") ) } } implicit object PlaceBSONReader extends BSONDocumentReader[Place] { def read(doc: BSONDocument) :Place = { val dtString = doc.getAs[String]("created").get val created = new DateTime(dtString) Place( doc.getAs[String]("id").get, doc.getAs[String]("name").get, doc.getAs[String]("street").get, doc.getAs[String]("city").get, doc.getAs[String]("state").get, doc.getAs[String]("zip").get, doc.getAs[String]("phone").get, doc.getAs[List[Double]]("location").get, doc.getAs[String]("description").get, doc.getAs[String]("business_type").get, created, doc.getAs[Hours]("hours"), doc.getAs[Map[String, String]]("links").get, doc.getAs[Boolean]("hidden").get ) } } implicit object TimeBlockBSONWriter extends BSONDocumentWriter[TimeBlock] { def write(block:TimeBlock): BSONDocument = { BSONDocument( "opens" -> block.opens.toString(timePattern), "closes" -> block.closes.toString(timePattern), "label" -> block.label ) } } implicit object HoursBSONWriter extends BSONDocumentWriter[Hours] { def write(hours:Hours): BSONDocument = { BSONDocument( "monday" -> hours.monday, "tuesday" -> hours.tuesday, "wednesday" -> hours.wednesday, "thursday" -> hours.thursday, "friday" -> hours.friday, "saturday" -> hours.saturday, "sunday" -> hours.sunday ) } } implicit object PlaceBSONWriter extends BSONDocumentWriter[Place] { def write(place:Place): BSONDocument = { val links = BSONDocument(place.links.map { tuple => tuple._1 -> BSONString(tuple._2) }) BSONDocument( "id" -> place.id, "name" -> place.name, "street" -> place.street, "city" -> place.city, "state" -> place.state, "zip" -> place.zip, "phone" -> place.phone, "location" -> place.location, "description" -> place.description, "business_type" -> place.business_type, "created" -> place.created.toString(), "hours" -> place.hours, "links" -> links, "hidden" -> place.hidden ) } } import scala.concurrent.ExecutionContext.Implicits.global val driver = new MongoDriver val connection = driver.connection(List("localhost")) val placeDoc = BSON.write(placeFixture) val database = connection("mydatabase") val collection = database("acollection") println(collection) collection.insert(placeDoc) val places = collection.find(BSONDocument("name" -> "Marks Bar")). cursor[Place]. collect[List]().map { places => for(place <- places){ println(place) } } }