Skip to content

Instantly share code, notes, and snippets.

@jcerdeira
Created August 21, 2015 16:05
Show Gist options
  • Save jcerdeira/94ffd9ca27406ce1c7da to your computer and use it in GitHub Desktop.
Save jcerdeira/94ffd9ca27406ce1c7da to your computer and use it in GitHub Desktop.
package controllers
import reactivemongo.api.collections._
import reactivemongo.api.MongoDriver
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson._
import reactivemongo.bson.{Macros => MongoMacros}
import reactivemongo.api.commands.UpdateWriteResult
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
/**
* Created by joaocerdeira on 21/08/15.
*/
package object MongoTest {
case class RapidView(id:Int,name:String,canEdit:Boolean,sprintSupportEnabled:Boolean,showDaysInColumn:Boolean)
case class SprintView(id: Int,sequence: Int,name: String, state:String)
case class SimpleTeam(name:String,key:String,label:String)
case class Team(name:String,key:String,label:String,
velocity: List[SprintVelocity],carryOvers: List[SprintCarryOvers]) //, workItemsNumbers: List[WorkItemsNumbers])
sealed trait StringValue{
val rapidView: RapidView
val sprintView: SprintView
def getNameAndValue : (String,Int)
}
case class SprintVelocity(rapidView: RapidView,sprintView: SprintView,velocity: Int) extends StringValue {
def getNameAndValue : (String,Int) = (sprintView.name,velocity)
}
case class SprintCarryOvers(rapidView: RapidView,sprintView: SprintView,carryOvers: Int) extends StringValue {
def getNameAndValue : (String,Int) = (sprintView.name,carryOvers)
}
case class SprintWorkItemsNumbers(rapidView: RapidView,sprintView: SprintView, byType: Map[String,Int],carryOvers: Int)
object MongoFunctions {
import MongoModelImplicits._
val driver = new MongoDriver
val connection = driver.connection(List("localhost"))
val db = connection.db("reporter")
val collection = db.collection[BSONCollection]("teams")
def getTeams: Future[List[Team]] = {
collection.find(BSONDocument()).cursor[Team].collect[List]()
}
def getSimpleTeams: Future[List[SimpleTeam]] = {
collection.find(BSONDocument()).cursor[SimpleTeam].collect[List]()
}
def getTeam(key:String): Future[Option[Team]] = {
collection.find(BSONDocument("key"->key)).one[Team]
}
def addSprintVelocity(key: String, sprintVelocity: SprintVelocity) : Future[UpdateWriteResult]= {
collection.update(BSONDocument("key"->key),BSONDocument("$addToSet"->BSONDocument("velocity"->sprintVelocity)))
}
def addSprintCarryOvers(key: String, sprintCarryOvers: SprintCarryOvers) : Future[UpdateWriteResult]= {
collection.update(BSONDocument("key"->key),BSONDocument("$addToSet"->BSONDocument("carryOvers"->sprintCarryOvers)))
}
def addSprintWorkItems(key: String, workItemsNumbers: SprintWorkItemsNumbers) : Future[UpdateWriteResult]= {
collection.update(BSONDocument("key"->key),BSONDocument("$addToSet"->BSONDocument("workItemsNumber"->"")))
}
}
object MongoModelImplicits {
import reactivemongo.bson.{Macros => MongoMacros}
implicit val simpleTeamFormat = MongoMacros.handler[SimpleTeam]
implicit val sprintViewformat = MongoMacros.handler[SprintView]
implicit val rapidViewformat = MongoMacros.handler[RapidView]
implicit val sprintVelocityformat = MongoMacros.handler[SprintVelocity]
implicit val sprintCarryOversformat = MongoMacros.handler[SprintCarryOvers]
//implicit val sprintWorkItemsNumberFormat = Macros.handler[WorkItemsNumbers]
implicit val teamFormat:BSONHandler[BSONDocument, Team] = MongoMacros.handler[Team]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment