本文整理汇总了Scala中play.modules.reactivemongo.MongoController类的典型用法代码示例。如果您正苦于以下问题:Scala MongoController类的具体用法?Scala MongoController怎么用?Scala MongoController使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MongoController类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: BalanceInfoController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.{Inject, Singleton}
import models.BalanceInfo
import play.api.libs.json.Json
import play.api.mvc.{Action, Controller}
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json.collection.JSONCollection
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import reactivemongo.api.{Cursor, ReadPreference}
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class BalanceInfoController @Inject()(val reactiveMongoApi: ReactiveMongoApi)(implicit exec: ExecutionContext)
extends Controller with MongoController with ReactiveMongoComponents {
val balances: JSONCollection = db.collection[JSONCollection]("balance_info")
def get(name: String) = Action.async {
val balanceCursor: Cursor[BalanceInfo] =
balances.find(Json.obj("name" -> name)).cursor[BalanceInfo](ReadPreference.primary)
val balanceFuture: Future[List[BalanceInfo]] = balanceCursor.collect[List]()
balanceFuture.map { data => Ok(Json.toJson(data)) }
}
}
示例2: MemoController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.{Inject, Singleton}
import model.Memo
import org.joda.time.LocalDateTime
import play.api.libs.json.Json
import play.api.mvc.{BodyParsers, Action, Controller}
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import reactivemongo.bson.BSONObjectID
import scala.concurrent.{Future, ExecutionContext}
@Singleton
class MemoController @Inject()(val reactiveMongoApi: ReactiveMongoApi)
(implicit ec: ExecutionContext)
extends Controller with MongoController with ReactiveMongoComponents {
def memoRepo = new backend.MemoMongoRepo(reactiveMongoApi)
//import reactivemongo.play.json._
import util.ReactiveMongoFormats._
implicit val memoFormat = Json.format[Memo]
def list = Action.async {
val memos: Future[List[Memo]] = memoRepo.find()
memos.map(list => Ok(Json.toJson(list)))
}
def post = Action.async(BodyParsers.parse.json) { implicit request =>
val memo: Memo = Memo(Some(BSONObjectID.generate()),
(request.body \ "text").as[String],
new LocalDateTime((request.body \ "date").as[Long]))
memoRepo.save(memo).map(_ => Created(memo._id.get.stringify))
}
}
示例3: Application
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import models.PlaceData
import play.api.data.Form
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc.{Action, Controller}
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import scala.concurrent.ExecutionContext
class Application @Inject() (val messagesApi: MessagesApi, val reactiveMongoApi: ReactiveMongoApi)
(implicit ec: ExecutionContext)
extends Controller with MongoController with ReactiveMongoComponents with I18nSupport {
val placeDAO = new PlaceDAO(reactiveMongoApi)
def index() = Action.async{implicit request =>
placeDAO.all().map(placesList => Ok(views.html.main(placesList, PlaceDAO.createPlaceForm)))}
def uploadPlace() = Action.async(parse.multipartFormData) { implicit request =>
def failure(formWithErrors: Form[PlaceData]) = {
placeDAO.all().map(placesList => BadRequest(views.html.main(placesList, formWithErrors)))
}
def success(placeData: PlaceData) = {
placeDAO.create(placeData, request.body.file("picture").get).map {
case w if w.ok => Redirect(routes.Application.index()).flashing("success" -> "Added place")
case _ => Redirect(routes.Application.index()).flashing("error" -> "Could not add place to database")
}
}
PlaceDAO.createPlaceForm.bindFromRequest().fold(failure, success)
}
}
示例4: PlaceDAO
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import com.google.common.io.Files
import models.{Place, PlaceData}
import play.api.data.Form
import play.api.data.Forms._
import play.api.libs.Files.TemporaryFile
import play.api.libs.json.Json
import play.api.mvc.Controller
import play.api.mvc.MultipartFormData.FilePart
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import reactivemongo.api.commands.WriteResult
import reactivemongo.api.{Cursor, ReadPreference}
import reactivemongo.play.json.collection.JSONCollection
import scala.concurrent.{ExecutionContext, Future}
class PlaceDAO @Inject() (val reactiveMongoApi: ReactiveMongoApi)
(implicit ec: ExecutionContext)
extends Controller with MongoController with ReactiveMongoComponents{
def create(placeData: PlaceData, picture: FilePart[TemporaryFile]): Future[WriteResult] = {
placesCollection.flatMap(_.insert(Place(placeData.name, placeData.country, placeData.description,
Files.toByteArray(picture.ref.file))))
}
def all(): Future[List[Place]] = placesCollection.flatMap(_.find(Json.obj())
.cursor[Place](ReadPreference.primaryPreferred).collect[List](Int.MaxValue, Cursor.FailOnError[List[Place]]()))
def placesCollection: Future[JSONCollection] = database.map(_.collection[JSONCollection]("places"))
}
object PlaceDAO {
val createPlaceForm = Form(
mapping(
"name" -> nonEmptyText,
"country" -> nonEmptyText,
"description" -> nonEmptyText
)(PlaceData.apply)(PlaceData.unapply)
)
}
示例5: WebSocketController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Singleton
import actors.{ActorManager, WebSocketActor}
import akka.actor.{ActorSystem, Props}
import akka.stream.Materializer
import com.google.inject.Inject
import play.api.Configuration
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc.{Action, Controller, WebSocket}
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import scala.concurrent.ExecutionContext
@Singleton
class WebSocketController @Inject() (val reactiveMongoApi: ReactiveMongoApi)
(implicit executionContext: ExecutionContext,
configuration: Configuration,
system: ActorSystem,
materializer: Materializer)
extends Controller with MongoController with ReactiveMongoComponents {
lazy final val actorManager = system.actorOf(Props[ActorManager],"actor-manager")
def socket = WebSocket.accept[JsValue,JsValue] { request =>
//request.session.get(Id).map { id =>
ActorFlow.actorRef(out => WebSocketActor.props(actorManager,out,"10"))
//}.getOrElse(throw new Exception("connection error"))
}
def index = Action { implicit request =>
Ok(views.html.websocket("test"))
}
}
示例6: TrackingDao
//设置package包名称以及导入依赖的类
package com.fourstars.prodbox.repositories
import com.fourstars.prodbox.model.TrackingItem
import com.google.inject.{Inject, Singleton}
import com.typesafe.scalalogging.LazyLogging
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Action
import play.modules.reactivemongo.json.ImplicitBSONHandlers._
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.{MongoController, ReactiveMongoApi, ReactiveMongoComponents}
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson.{BSONDocument, BSONObjectID}
import scala.concurrent.Future
@Singleton
class TrackingDao @Inject()(val reactiveMongoApi: ReactiveMongoApi) extends MongoController with ReactiveMongoComponents with LazyLogging with BsonFormatters {
def collection: BSONCollection = db.collection[BSONCollection]("persons")
def index = Action {
Ok("works")
}
def create(url: String): Future[TrackingItem] = {
val trackingItem = new TrackingItem(Some(BSONObjectID.generate), url)
val futureResult = collection.insert(trackingItem)
futureResult.map {
response =>
logger.debug(s"response is $response")
logger.debug(s"t is now $trackingItem")
trackingItem
}
}
def find(id: String): Future[Option[TrackingItem]] = {
collection.find((BSONDocument("_id" -> BSONObjectID(id)))).one[TrackingItem]
}
def count(): Future[Int] = {
collection.count()
}
}