• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala MongoController类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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)) }
  }

} 
开发者ID:qza,项目名称:balance-service-play,代码行数:30,代码来源:BalanceInfoController.scala


示例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))
  }
} 
开发者ID:dturbay,项目名称:memo-rest,代码行数:37,代码来源:MemoController.scala


示例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)
  }
} 
开发者ID:test410,项目名称:picture-gallery,代码行数:39,代码来源:Application.scala


示例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)
  )
} 
开发者ID:test410,项目名称:picture-gallery,代码行数:45,代码来源:PlaceDAO.scala


示例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"))
  }

} 
开发者ID:tictactoki,项目名称:Immutaskable,代码行数:39,代码来源:WebSocketController.scala


示例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()
  }
} 
开发者ID:gleeb,项目名称:prodbox,代码行数:46,代码来源:TrackingDao.scala



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala KafkaUtils类代码示例发布时间:2022-05-23
下一篇:
Scala Sink类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap