本文整理汇总了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
}
}
}
示例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] = ???
}
示例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)
}
}
示例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]] = ???
}
示例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)
}
}
}
示例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] = ???
}
示例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
}
}
示例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)
}
}
示例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]
}