设为首页 加入收藏

TOP

restapi(6)- do it the functional way, 重温函数式编程(五)
2019-09-03 03:40:13 】 浏览:272
Tags:restapi the functional way 重温 函数 编程
w = repository.getOnePicture(pid, optSeq.get).toFuture[WebPic]
onComplete(futPicRow) {
case Success(row) =>
val width = if (optWid == None) row.width.getOrElse(128) else optWid.getOrElse(128)
val height = if (optHght == None) row.heigth.getOrElse(128) else optHght.getOrElse(128)
if (row.pic != None) {
withoutSizeLimit {
encodeResponseWith(Gzip) {
complete(
HttpEntity(
ContentTypes.`application/octet-stream`,
ByteArrayToSource(Imaging.setImageSize(row.pic.get.getData, width, height)
))
)
}
}
} else complete(StatusCodes.NotFound)
case Failure(err) => complete(err)
}
}
}
}
} ~
pathPrefix("blob") {
(get & parameter('filter)) { filter =>
val filtr = Document(filter)
val futOptPic: CancelableFuture[Option[MGOBlob]] = repository.getOneDocument(filtr).toFuture
onComplete(futOptPic) {
case Success(optBlob) => optBlob match {
case Some(blob) =>
withoutSizeLimit {
encodeResponseWith(Gzip) {
complete(
HttpEntity(
ContentTypes.`application/octet-stream`,
ByteArrayToSource(blob.getData)
)
)
}
}
case None => complete(StatusCodes.NotFound)
}
case Failure(err) => complete(err)
}
} ~
(post & parameter('bson)) { bson =>
val bdoc = Document(bson)
withoutSizeLimit {
decodeRequest {
extractDataBytes { bytes =>
val futbytes = bytes.runFold(ByteString()) { case (hd, bs) =>
hd ++ bs
}
val futmsg:Future[Completed] = for {
bytes <- futbytes
doc = Document(bson) + ("photo" -> bytes.toArray)
c <- repository.insert(doc).toFuture[Completed]
} yield c
complete(futmsg.map(_.toString))
}
}
}
}
} ~
(get & parameters('filter.?,'fields.?,'sort.?,'top.as[Int].?,'next.?)) {
(filter,fields,sort,top,next) => {
dbor = {
filter match {
case Some(fltr) => repository.query(Document(fltr),next,sort,fields,top)
case None => repository.getAll(next,sort,fields,top)
}
}
val futRows:Future[Seq[WebPic]] = dbor.toFuture[Seq[WebPic]]
complete(futureToJson(futRows))
}
} ~ post {
entity(as[String]) { json =>
val extractedEntity: M = fromJson[M](json)
val doc: Document = extractedEntity.to
val futmsg = repository.insert(doc).toFuture[Completed]
complete(futmsg.map(_.toString))
}
} ~ (put & parameter('filter,'set.?, 'many.as[Boolean].?)) { (filter, set, many) =>
val bson = Document(filter)
if (set == None) {
entity(as[String]) { json =>
val extractedEntity: M = fromJson[M](json)
val doc: Document = extractedEntity.to
val futmsg = repository.replace(bson, doc).toFuture
complete(futureToJson(futmsg))
}
} else {
set match {
case Some(u) =>
val ubson = Document(u)
dbou = repository.update(bson, ubson, many.getOrElse(true))
case None =>
dbou = Left(new IllegalArgumentException("missing set statement for update!"))
}
val futmsg:Future[UpdateResult] = dbou.toFuture[UpdateResult]
complete(futureToJson(futmsg.map(_.toString)))
}
} ~ (delete & parameters('filter, 'many.as[Boolean].?)) { (filter,many) =>
val bson = Document(filter)
val futmsg:Future[DeleteResult] = repository.delete(bson).toFuture[DeleteResult]
complete(futureToJson(futmsg.map(_.toString)))
}
}

def addPicuture(pid: String,seqno: Int, optDesc: Option[String]
,optWid:Option[Int],optHgh:Option[Int],
bytes: Array[Byte]):DBOResult[Completed] ={
var doc = Document(
"pid" -> pid,
"seqno" -> seqno,
"pic" -> bytes
)
if (optDesc != None)
doc = doc + (&
首页 上一页 2 3 4 5 下一页 尾页 5/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Scala 函数基础入门 下一篇Scala2.12 从入门到精通实战高端..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目