本文整理汇总了Scala中reactivemongo.bson.BSONDateTime类的典型用法代码示例。如果您正苦于以下问题:Scala BSONDateTime类的具体用法?Scala BSONDateTime怎么用?Scala BSONDateTime使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BSONDateTime类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Common
//设置package包名称以及导入依赖的类
package models
import java.util.Date
import reactivemongo.bson.{BSONDateTime,BSONReader,BSONWriter}
object Common {
val objectIdRegEx = """[a-fA-F0-9]{24}""".r
}
object BSONProducers {
implicit object DateWriter extends BSONWriter[Date,BSONDateTime]{
def write(dt:Date) : BSONDateTime = BSONDateTime(dt.getTime)
}
implicit object DateReader extends BSONReader[BSONDateTime,Date]{
def read(dt:BSONDateTime) : Date = new Date(dt.value)
}
}
示例2: write
//设置package包名称以及导入依赖的类
package de.choffmeister.microserviceutils.mongodb.bson
import java.time.Instant
import reactivemongo.bson.{BSONDateTime, BSONLong, BSONReader, BSONValue, BSONWriter}
trait InstantBSONProtocol {
implicit val instantReaderWriter = new BSONReader[BSONValue, Instant] with BSONWriter[Instant, BSONDateTime] {
override def write(t: Instant): BSONDateTime = BSONDateTime(t.toEpochMilli)
override def read(bson: BSONValue): Instant = bson match {
case BSONDateTime(value) => Instant.ofEpochMilli(value)
case BSONLong(value) => Instant.ofEpochMilli(value)
case _ => throw new RuntimeException()
}
}
}
示例3: write
//设置package包名称以及导入依赖的类
package com.realizationtime.btdogg.persist
import java.time.{Instant, LocalDate}
import com.realizationtime.btdogg.TKey
import com.realizationtime.btdogg.parsing.ParsingResult.{FileEntry, TorrentDir, TorrentFile}
import com.realizationtime.btdogg.persist.MongoPersist.{Liveness, TorrentDocument}
import com.realizationtime.btdogg.persist.MongoTorrentWriter.localDateToString
import reactivemongo.bson.{BSONDateTime, BSONDocument, BSONDocumentWriter, BSONInteger, BSONString, BSONWriter, Macros}
trait MongoTorrentWriter {
implicit val tkeyWriter = new BSONWriter[TKey, BSONString] {
override def write(k: TKey): BSONString = BSONString(k.hash)
}
implicit val torrentDataFileWriter: BSONDocumentWriter[TorrentFile] = Macros.writer[TorrentFile]
implicit val torrentDataDirWriter: BSONDocumentWriter[TorrentDir] = Macros.writer[TorrentDir]
implicit val torrentDataWriter: BSONDocumentWriter[FileEntry] = Macros.writer[FileEntry]
implicit val instantWriter = new BSONWriter[Instant, BSONDateTime] {
override def write(t: Instant): BSONDateTime = BSONDateTime(t.toEpochMilli)
}
implicit val torrentWriter: BSONDocumentWriter[TorrentDocument] = Macros.writer[TorrentDocument]
implicit val livenessWriter: BSONDocumentWriter[Liveness] = Macros.writer[Liveness]
implicit val localDateWriter = new BSONWriter[LocalDate, BSONString] {
override def write(t: LocalDate): BSONString = BSONString(localDateToString(t))
}
implicit val mapWriter: BSONDocumentWriter[Map[LocalDate, Int]] = new BSONDocumentWriter[Map[LocalDate, Int]] {
def write(map: Map[LocalDate, Int]): BSONDocument = {
val elements = map.toStream.map { tuple =>
localDateToString(tuple._1) -> BSONInteger(tuple._2)
}
BSONDocument(elements)
}
}
}
object MongoTorrentWriter {
def localDateToString(date: LocalDate): String = date.toString
}
示例4: reactiveMongoApi
//设置package包名称以及导入依赖的类
package repositories
import crypto.CryptoHelpers
import models.EncryptedAuthTokenResponse
import org.joda.time.DateTime
import play.api.Play
import play.api.libs.json.Json
import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json.ImplicitBSONHandlers._
import reactivemongo.api.commands.UpdateWriteResult
import reactivemongo.bson.BSONDateTime
import reactivemongo.play.json.collection.JSONCollection
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.ExecutionContext.Implicits.global
trait MonzoRepository {
def reactiveMongoApi: ReactiveMongoApi
def collectionFuture: Future[JSONCollection]
def save(monzoAuth: EncryptedAuthTokenResponse): Future[UpdateWriteResult] = {
val query = Json.obj("userId" -> monzoAuth.userId)
val update =
Json.obj("$setOnInsert" -> Json.obj("clientId" -> monzoAuth.clientId, "userId" -> monzoAuth.userId, "accountId" -> monzoAuth.accountId),
"$set" -> Json.obj("accessToken" -> monzoAuth.accessToken, "expiresIn" -> monzoAuth.expiresIn, "refreshToken" -> monzoAuth.refreshToken, "tokenType" -> monzoAuth.tokenType, "updated" -> BSONDateTime(new DateTime().getMillis))
)
for {
collection <- collectionFuture
upsert <- collection.update(query, update, upsert = true)
} yield upsert
}
def findByAccountId(accountId: String)(implicit ec: ExecutionContext) = {
for {
collection <- collectionFuture
result <- collection.find(Json.obj("accountId" -> CryptoHelpers.encrypt(accountId))).one[EncryptedAuthTokenResponse]
} yield result
}
}
object MonzoRepository extends MonzoRepository {
lazy val reactiveMongoApi: ReactiveMongoApi = Play.current.injector.instanceOf[ReactiveMongoApi]
lazy val collectionFuture: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection[JSONCollection]("monzo"))
}
示例5: AccessToken
//设置package包名称以及导入依赖的类
package models
import reactivemongo.bson.BSONObjectID
import reactivemongo.bson.BSONDateTime
import reactivemongo.bson.Macros
import java.util.Date
import play.api.libs.json.Json
import play.modules.reactivemongo.json.BSONFormats._
case class AccessToken (
val _id: Option[BSONObjectID],
val accessToken: String,
val refreshToken: Option[String],
val userId: BSONObjectID,
val scope: Option[String],
val expiresIn: Int,
val createdAt: BSONDateTime,
val clientId: String
)
object AccessTokenFormatter{
implicit val accessTokenFormatter = Json.format[AccessToken];
}
示例6: PublicPostsAccessor
//设置package包名称以及导入依赖的类
package me.lsbengine.api.public
import com.github.nscala_time.time.Imports.DateTime
import me.lsbengine.api.PostsAccessor
import me.lsbengine.database.DatabaseAccessor
import me.lsbengine.database.model.{MongoCollections, Post}
import me.lsbengine.database.model.MongoFormats._
import reactivemongo.api.DefaultDB
import reactivemongo.bson.{BSONDateTime, BSONDocument}
import scala.concurrent.Future
class PublicPostsAccessor(db: DefaultDB)
extends DatabaseAccessor[Post](db, MongoCollections.postsCollectionName)
with PostsAccessor {
def getPost(id: Int): Future[Option[Post]] = {
val now = DateTime.now
val query = BSONDocument("id" -> id,
"published" -> BSONDocument(
"$lte" -> BSONDateTime(now.getMillis)
))
super.getItem(query)
}
def listPosts: Future[List[Post]] = {
val now = DateTime.now
val sort = BSONDocument("published" -> -1)
val query = BSONDocument("published" -> BSONDocument(
"$lte" -> BSONDateTime(now.getMillis)
))
super.getItems(query = query, sort = sort)
}
}
示例7: Post
//设置package包名称以及导入依赖的类
package models
import reactivemongo.bson.BSONDateTime
import reactivemongo.play.json._
case class Post(
title: String,
url: String,
announce: String,
text: String,
created: BSONDateTime,
tags: List[Tag])
case class Tag(
name: String,
url: String)
object JsonFormats {
import play.api.libs.json.Json
implicit val tagFormat = Json.format[Tag]
implicit val postFormat = Json.format[Post]
}
示例8: read
//设置package包名称以及导入依赖的类
package bson
import java.util.UUID
import org.joda.time.DateTime
import reactivemongo.bson.{BSONDateTime, BSONHandler, BSONString}
package object handlers {
implicit val UUIDHandler = BSONHandler(
{ read: BSONString => UUID.fromString(read.value) },
{ write: UUID => BSONString(write.toString) }
)
implicit val DateTimeHandler = new BSONHandler[BSONDateTime, DateTime] {
override def read(bson: BSONDateTime): DateTime = {
new DateTime(bson.value)
}
override def write(t: DateTime): BSONDateTime = {
BSONDateTime(t.getMillis)
}
}
}