vapor 实践

vapor 用户订阅安装包(1)

2018-10-09  本文已影响0人  搞好关系

用户订阅安装包分为功能分为两步来开发:
。1用户订阅对应包
。2当订阅的安装包更新之后,自动发送email给订阅者,通知其更新安装
我们目前只进行1--订阅安装包更新

分析

根据展示出来的包,看到的用户可以进行订阅
因此我们可以根据当前用户ID和对应的包ID作为已经生成Pivot

Pivot


/// 用户订阅安装包更新通知
struct SKPackageScribePivot: SQLitePivot {
    static var leftIDKey: WritableKeyPath<SKPackageScribePivot, Int> = \.userId
    
    static var rightIDKey: WritableKeyPath<SKPackageScribePivot, Int> = \.packageId
    
    
    typealias Left = SKUser
    
    typealias Right = SKPackage
    
    var id: Int?
    var userId: Int
    var packageId: Int
    
}
extension SKPackageScribePivot :   Content & Migration {}

逻辑层: 验证用户ID和包ID是否正确,然后查看是否已经订阅

//订阅安装包
    public func subcribeUserPackage(req: Request)throws -> EventLoopFuture<String>{
        struct InnerPackage: Content{
            var packageId: Int
            var userId: Int
        }
        let innerPackage: InnerPackage = try! req.query.decode(InnerPackage.self)
        return SKPackage.query(on: req).filter(\.id, .equal, innerPackage.packageId).first()
            .flatMap({ (package: SKPackage?) -> EventLoopFuture<(SKPackage?, SKUser?, InnerPackage)> in
                return  SKUser.query(on: req).filter(\.id, .equal,innerPackage.userId)
                    .first()
                    .flatMap({ (user:SKUser?) -> EventLoopFuture<(SKPackage?, SKUser?, InnerPackage)> in
                        let result = req.eventLoop.newPromise((SKPackage?, SKUser?, InnerPackage).self)
                        result.succeed(result: (package, user, innerPackage))
                        return result.futureResult
                    })
            }).flatMap({ (value:(SKPackage?, SKUser?, InnerPackage)) -> EventLoopFuture<(SKPackageScribePivot?, SKPackage?, SKUser?)> in
                
                return SKPackageScribePivot.query(on: req)
                    .filter(\.packageId, .equal, value.2.packageId)
                    .filter(\.userId, .equal, value.2.userId)
                    .first().flatMap({ (pivot: SKPackageScribePivot?) -> EventLoopFuture<(SKPackageScribePivot?, SKPackage?, SKUser?)> in
                        let result = req.eventLoop.newPromise((SKPackageScribePivot?, SKPackage?, SKUser?).self)
                        result.succeed(result: (pivot,value.0, value.1))
                        return result.futureResult
                    })
            }).flatMap({ (p) -> EventLoopFuture<String> in
                
                //发送的订阅信息有问题
                if p.1 == nil || p.2 == nil {
                    let result = req.eventLoop.newPromise(String.self)
                    if p.2 == nil {
                        result.succeed(result: "用户不存在")
                    }
                    if p.2 == nil {
                        result.succeed(result: "订阅的包不存在")
                    }
                    return result.futureResult
                }
                
                if p.0 == nil {//可以订阅
                    return try SKPackageScribePivot.init(p.2!, p.1!)
                        .create(on: req).map({ (p) -> String in
                            return "订阅成功"
                        })
                }else {//重复订阅
                    let result = req.eventLoop.newPromise(String.self)
                    result.succeed(result: "不能重复订阅")
                    return result.futureResult
                }
            })
    }
    

最后是结果

重复订阅提示
上一篇下一篇

猜你喜欢

热点阅读