当前位置: 首页>>代码示例>>Scala>>正文


Scala DB类代码示例

本文整理汇总了Scala中reactivemongo.api.DB的典型用法代码示例。如果您正苦于以下问题:Scala DB类的具体用法?Scala DB怎么用?Scala DB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了DB类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: ctx

//设置package包名称以及导入依赖的类
package base

import play.modules.reactivemongo.json.JSONSerializationPack
import play.modules.reactivemongo.json.collection.JSONCollection
import reactivemongo.api.DB

import scala.concurrent.{Future, ExecutionContext}


  def ctx(db: DB, collectionName: Symbol): JSONCollection = ctx(db, collectionName.name)

  private def ctx(db: DB, collectionName: String): JSONCollection =
    db.collection[JSONCollection](collectionName)

  def LastOk = reactivemongo.core.commands.LastError(true, None, None, None, None, 0, false)
  def LastError(error: String) = reactivemongo.core.commands.LastError(false, None, None, Some(error), None, 0, false)

  def bulkInsert[T]
  (ctx: JSONCollection, lst: Seq[T])
  (implicit writer: JSONSerializationPack.Writer[T], ec: ExecutionContext): Future[Int] = {
    lst.foldLeft(Future.successful(0)) { (acc: Future[Int], x: T) =>
      for {
        n <- acc
        error <- ctx.insert(x)
        i = if (error.ok) 1 else 0
      } yield n + 1
    }
  }
} 
开发者ID:scozv,项目名称:bolero,代码行数:30,代码来源:mongo.scala

示例2: OrderBiz

//设置package包名称以及导入依赖的类
package biz

import base._
import biz.interop.CanConnectDB
import models.interop.HTTPResponseError
import models.{CoreOrder, Order, TuanOrder}
import reactivemongo.api.DB
import scala.concurrent.{ExecutionContext, Future}

object OrderBiz extends CanConnectDB {
  def ctx(db: DB) =
    mongo.ctx(db, base.mongo.collectionName.ORDERS)

  def validate
  (db: DB, order: CoreOrder)
  (implicit ec: ExecutionContext): Future[Either[CoreOrder, HTTPResponseError]] =  order match {
    case x : Order => biz.rules.OrderRules.pipeRules(x, db, biz.rules.OrderRules.allRules)
    case x : TuanOrder => biz.rules.TuanRules.pipeRules(x, db, biz.rules.TuanRules.allRules)
  }

  def getOrder(db: DB, userId: String, orderId: String)(implicit ec: ExecutionContext): Future[Option[CoreOrder]] = ???
  def getOrders
  (db: DB, userId: String, status: Option[Int] = None)
  (implicit ec: ExecutionContext): Future[Seq[CoreOrder]] = ???

  def createOrder(db: DB, payload: CoreOrder, userId: String)(implicit ec: ExecutionContext): Future[CoreOrder] = ???

  def nextOrderId
  (db: DB)(implicit ec: ExecutionContext): Future[String] = ???

  def closeOrder(db: DB, userId: String, orderId: String)(implicit ec: ExecutionContext): Future[CoreOrder] = ???
} 
开发者ID:scozv,项目名称:bolero,代码行数:33,代码来源:OrderBiz.scala

示例3: ctx

//设置package包名称以及导入依赖的类
package biz.interop

import play.api.libs.json.{JsBoolean, JsObject, JsString, Json}
import play.modules.reactivemongo.json.collection.JSONCollection
import reactivemongo.api.DB
import scala.concurrent.{ExecutionContext, Future}

trait CanConnectDB {
  def ctx(db: DB): JSONCollection

  val defaultIdentityField = "_id"
  def identityQuery(value: String, identityField: String = defaultIdentityField): JsObject =
    Json.obj(identityField -> JsString(value))
  def fieldsProjection(fields: String*): JsObject = {
    val project = fields.filter(! _.trim.isEmpty).map(_ -> JsBoolean(true))
    JsObject(project)
  }

  val allQuery = Json.obj()

  def count
  (db: DB, value: String, identityField: String = defaultIdentityField)
  (implicit ec: ExecutionContext): Future[Int] = {
    ctx(db).count(Some(identityQuery(value, identityField)))
  }

  def any
  (db: DB, value: String, identityField: String = defaultIdentityField)
  (implicit ec: ExecutionContext): Future[Boolean] = {
    count(db, value, identityField).map(_ > 0)
  }
} 
开发者ID:scozv,项目名称:bolero,代码行数:33,代码来源:CanConnectDB.scala

示例4: GoodsBiz

//设置package包名称以及导入依赖的类
package biz

import base.mongo
import biz.interop.CanConnectDB
import models._
import reactivemongo.api.DB
import scala.concurrent.{ExecutionContext, Future}

object GoodsBiz extends CanConnectDB {
  def ctx(db: DB) =
    mongo.ctx(db, base.mongo.collectionName.GOODS)

  def getAllGoods
  (db: DB, goodsId: Seq[String] = Seq(), goodsTypes: Seq[String] = Seq())
  (implicit ec: ExecutionContext): Future[Seq[CoreGoods]] = ???

  def getTuanGoodsList(db: DB)(implicit ec: ExecutionContext): Future[Seq[TuanGoods]] = ???

  def getTuanGoods(db: DB, goodsId: String)(implicit ec: ExecutionContext): Future[Seq[TuanGoods]] = ???

  def getGoods(db: DB, goodsId: String)(implicit ec: ExecutionContext) = ???

  def getPictureId(db: DB, goodsId: String)(implicit ec: ExecutionContext): Future[Seq[String]] = ???

  def getThumbsnailId(db: DB, goodsId: String)(implicit ec: ExecutionContext): Future[String] = ???

  def importProducts(db: DB, goodsLst: Seq[CoreGoods])(implicit ec: ExecutionContext) = {
    if (goodsLst.groupBy(x=>x._id).keys.toList.length == goodsLst.length) {
      // no _id duplicated
      base.mongo.bulkInsert(ctx(db), goodsLst)
    } else Future.successful(-1)
  }

  def getAllTypies(keywords: Seq[String]): Future[Map[String, String]] = ???
} 
开发者ID:scozv,项目名称:bolero,代码行数:36,代码来源:GoodsBiz.scala

示例5: OrderRules

//设置package包名称以及导入依赖的类
package biz.rules

import biz.{GoodsBiz}
import models._
import models.interop.HTTPResponseError
import reactivemongo.api.DB

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.ExecutionContext.Implicits.global

object OrderRules {
  private type OrderOrResponse = Either[CoreOrder, HTTPResponseError]
  private type ValidateOrder =
  (CoreOrder, => DB) => Future[OrderOrResponse]

  private[biz] val allRules: Seq[ValidateOrder] = Seq(
    cartItemNotBeEmpty,
    orderPriceGreaterThanZero,
    cartPriceLessThanCurrentGoods
  )

  private def pipeRules
  (order: OrderOrResponse, db: => DB, rules: Seq[ValidateOrder])
  (implicit ec: ExecutionContext): Future[OrderOrResponse] =
    rules.foldLeft(Future.successful(order)){ (acc, ruleItem) =>
      acc.flatMap { orderOrElse =>
        if (orderOrElse.isLeft) ruleItem(orderOrElse.left.get, db)
        else Future.successful(orderOrElse)
      }
    }

  private[biz] def pipeRules
  (order: CoreOrder, db: => DB, rules: Seq[ValidateOrder])
  (implicit ec: ExecutionContext): Future[OrderOrResponse] =
    pipeRules(Left(order), db, rules)

  
  private def cartPriceLessThanCurrentGoods
  (order: CoreOrder, db: => DB)(implicit ec: ExecutionContext): Future[OrderOrResponse] = {

    val allGoods = GoodsBiz.getAllGoods(db, order.orderItems.map(_.goods._id))

    allGoods.map { goods =>
      val currentPrice = goods.foldLeft(0.0){ (acc, x) =>
        val itemInCart = order.orderItems.find(_.goods._id == x._id)
        val itemCurrentPrice =
          if (itemInCart.isDefined) itemInCart.get.goodsAmount * x.price
          else 0.0

        acc + itemCurrentPrice
      }

      // the price that user ordered must <= db updated price
      // cause if not, order may cancle this order, and re-reder with lower db updated price
      if (order.orderAmount > currentPrice) Right(HTTPResponseError.ORDER_PRICE_NOT_MATCHED)
      else Left(order)
    }
  }
} 
开发者ID:scozv,项目名称:bolero,代码行数:60,代码来源:OrderRules.scala

示例6: TuanRules

//设置package包名称以及导入依赖的类
package biz.rules

import models._
import models.interop.HTTPResponseError
import reactivemongo.api.DB
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.ExecutionContext.Implicits.global

object TuanRules {
  private type OrderOrResponse = Either[TuanOrder, HTTPResponseError]
  private type ValidateOrder =
  (TuanOrder, => DB) => Future[OrderOrResponse]

  private[biz] val allRules: Seq[ValidateOrder] = Seq(
    cartItemNotBeEmpty,
    orderPriceMatched,
    orderPriceGreaterThanZero,
    limitedAmountForSpecialOrder
  )

  private def pipeRules
  (order: OrderOrResponse, db: => DB, rules: Seq[ValidateOrder])
  (implicit ec: ExecutionContext): Future[OrderOrResponse] =
    rules.foldLeft(Future.successful(order)){ (acc, ruleItem) =>
      acc.flatMap { orderOrElse =>
        if (orderOrElse.isLeft) ruleItem(orderOrElse.left.get, db)
        else Future.successful(orderOrElse)
      }
    }

  private[biz] def pipeRules
  (order: TuanOrder, db: => DB, rules: Seq[ValidateOrder])
  (implicit ec: ExecutionContext): Future[OrderOrResponse] =
    pipeRules(Left(order), db, rules)

  private def cartItemNotBeEmpty
  (order: TuanOrder, db: => DB)
  (implicit ec: ExecutionContext): Future[OrderOrResponse] = ???

  private def orderPriceGreaterThanZero
  (order: TuanOrder, db: => DB)
  (implicit ec: ExecutionContext): Future[OrderOrResponse] = ???

  private def orderPriceMatched
  (order: TuanOrder, db: => DB)
  (implicit ec: ExecutionContext): Future[OrderOrResponse] = ???

  private def limitedAmountForSpecialOrder
  (order: TuanOrder, db: => DB)
  (implicit ec: ExecutionContext): Future[OrderOrResponse] = ???
} 
开发者ID:scozv,项目名称:bolero,代码行数:52,代码来源:TuanRules.scala

示例7: UserProfileBiz

//设置package包名称以及导入依赖的类
package biz

import base.mongo
import base.mongo.userFields.IdentityType
import biz.interop.CanConnectDB
import models._
import reactivemongo.api.DB
import scala.concurrent.{ExecutionContext, Future}

object UserProfileBiz extends CanConnectDB {
  def ctx(db: DB) =
    mongo.ctx(db, base.mongo.collectionName.USERS)

  def addressCtx(db: DB) =
    base.mongo.ctx(db, base.mongo.collectionName.ADDRESS)

  def createUser
  (db: DB, user: User)
  (implicit write: play.api.libs.json.OWrites[models.User], ec: ExecutionContext): Future[User] = ???

  def updateUserWeChatInfo
  (db: DB, user: User)
  (implicit ec: ExecutionContext): Future[User] = ???

  
  def isFreshUser(db: DB, userId: String)(implicit ec: ExecutionContext): Future[Boolean] = {
    for {
      user <- getProfile(db, userId, mongo.userFields.IdentityType.UserId)
      orders <- OrderBiz.getOrders(db, userId)
    } yield user.isDefined && orders.isEmpty
  }
} 
开发者ID:scozv,项目名称:bolero,代码行数:33,代码来源:UserProfileBiz.scala

示例8: find

//设置package包名称以及导入依赖的类
package dao

import javax.inject.Inject

import play.api.libs.json.{JsObject, Json}
import reactivemongo.api.commands.WriteResult
import reactivemongo.api.{DB, ReadPreference}
import reactivemongo.bson.{BSONDocument, BSONObjectID}
import reactivemongo.play.json.collection.JSONCollection

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

trait CompaniesRepository {
  def find(term: String): Future[List[JsObject]]

  def save(document: BSONDocument): Future[WriteResult]
}

class MongoDbCompaniesRepository @Inject()(db: DB) extends CompaniesRepository {

  import play.modules.reactivemongo.json._

  protected val collection = db.collection[JSONCollection]("companies")

  override def find(term: String): Future[List[JsObject]] = {
    val query = if (term == "") Json.obj() else Json.obj("$text" -> Json.obj("$search" -> term))
    collection.find(query)
      .cursor[JsObject](ReadPreference.Primary)
      .collect[List]()
  }

  override def save(document: BSONDocument): Future[WriteResult] = {
    collection.update(BSONDocument("_id" -> document.get("_id").getOrElse(BSONObjectID.generate)), document, upsert = true)
  }
} 
开发者ID:team3,项目名称:Play2-mongodb-silhouette-activator-template,代码行数:37,代码来源:CompaniesRepository.scala

示例9: find

//设置package包名称以及导入依赖的类
package dao

import java.util.UUID
import javax.inject.Inject

import com.mohiva.play.silhouette.api.LoginInfo
import model.User
import play.api.libs.json.Json
import reactivemongo.play.json.collection.JSONCollection
import reactivemongo.api.DB

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import play.modules.reactivemongo.json._
import reactivemongo.api.commands.WriteResult

trait UserRepository {
  def find(loginInfo: LoginInfo): Future[Option[User]]

  def find(userId: UUID): Future[Option[User]]

  def save(user: User): Future[WriteResult]
}

class MongoDbUserRepository @Inject()(db: DB) extends UserRepository {
  val collection: JSONCollection = db.collection[JSONCollection]("users")

  override def find(loginInfo: LoginInfo): Future[Option[User]] = {
    collection.find(Json.obj("loginInfo" -> loginInfo)).one[User]
  }

  override def save(user: User): Future[WriteResult] = {
    collection.insert(user)
  }

  override def find(userId: UUID): Future[Option[User]] = collection.find(Json.obj("userId" -> userId)).one[User]
} 
开发者ID:team3,项目名称:Play2-mongodb-silhouette-activator-template,代码行数:38,代码来源:UserRepository.scala


注:本文中的reactivemongo.api.DB类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。