美文网首页Swift Vaporvapor 实践
vapor 安装包和历史版本查看

vapor 安装包和历史版本查看

作者: 搞好关系 | 来源:发表于2018-10-10 18:44 被阅读3次

    阅读VaporStyle指导之后突然感觉写代码思路变慢了很多,思路总是被终端,明显感觉不适应那种代码和思维方式,不过强制扭转之后感觉代码组织和易读性明显提高了,不说了我们先来实现一个人员上传安装包的汇总:

    我们需要使用leaf,因此数据需要遵循Encode,因此要定义组织一种数据格式

     /// 最终的数据列表
            struct  PInfoList: Codable {
                var title: String = "Hello"
                var list: [PInfo] = [PInfo]()
                init(list: [PInfo] = []) {
                    self.list.append(contentsOf: list)
                    self.title = "Hello"
                }
            }
            
            /// 包和用户相关的数据结构
            struct PInfo: Codable{
                var package: SKPackage
                var user: SKUser?
                var installs: [SKInstallPackage] = [SKInstallPackage]()
                init(_ p: SKPackage) {
                    package = p
                }
                init(_ p: SKPackage, user: SKUser?) {
                    self.package = p
                    self.user = user
                }
                init(_ p: SKPackage, user: SKUser?, installs:[SKInstallPackage]) {
                    self.package = p
                    self.user = user
                    self.installs.append(contentsOf: installs)
                }
            }
    

    感觉按照官方给的代码书写格式来进行查询时最让人头疼的一件事情,当然了是由于map flatMap和思路转换的不够适应

    let view =  SKPackage.query(on: req).all().flatMap({ (ps) -> EventLoopFuture<PInfoList> in
                
              return  ps.map({ (p) -> EventLoopFuture<(SKPackage, SKUser?,[SKInstallPackage?])> in
                    return p.owner.query(on: req).first().flatMap({ (u) -> EventLoopFuture<(SKPackage, SKUser?)> in
                        let resutl = req.eventLoop.newPromise((SKPackage, SKUser?).self)
                        resutl.succeed(result: (p,u))
                        return resutl.futureResult
                    }).flatMap({ (pk) -> EventLoopFuture<(SKPackage, SKUser?, [SKInstallPackage?])> in
                       return try pk.0.packages.query(on: req).all().flatMap({ (pks) -> EventLoopFuture<(SKPackage, SKUser?, [SKInstallPackage?])> in
                            let resutl = req.eventLoop.newPromise((SKPackage, SKUser?,[SKInstallPackage?]).self)
                            resutl.succeed(result: (pk.0,pk.1, pks))
                            return resutl.futureResult
                        })
                    })
                }).map({ (e) -> EventLoopFuture<PInfo> in
                 
                   return e.map({ (value:(SKPackage, SKUser?, [SKInstallPackage?])) -> PInfo in
                    
                    let pInfo = PInfo(value.0, user: value.1, installs: value.2 as! [SKInstallPackage])
                    
                    
                    return pInfo
                    })
                }).flatten(on: req).flatMap({ (ps) -> EventLoopFuture<PInfoList> in
                    var pList = PInfoList(list: ps)
                    pList.title = "安装包查看"
                    let result  = req.eventLoop.newPromise(PInfoList.self)
                    
                    result.succeed(result: pList)
                    return result.futureResult
                })
            }).flatMap({ (pList) -> EventLoopFuture<View> in
                return try req.view().render("package.leaf", pList)
            })
    
    

    直接上结果

    查询结果

    相关文章

      网友评论

        本文标题:vapor 安装包和历史版本查看

        本文链接:https://www.haomeiwen.com/subject/ngdmaftx.html