首页 > 解决方案 > 在 Vapor 中寻找祖父母

问题描述

我目前正在将 Vapor 用于数据库并且遇到了我可以使用一些帮助的障碍。

我目前有一个Business对象,它有[AccessPoint]孩子。每一个AccessPoint,本身就有数个[AccessPointContactMethod]

AnAccessPointContactMethod可以AccessPoint通过检查其accessPointID在创建父子关系时由 Vapor 设置的属性来引用其父级。

事实上,我使用它来确保当AccessPointContactMethod创建一个新的时,如果父级AccessPoint已经有一个具有相同地址的子级,我们会中止发布请求:

return AccessPointContactMethod.query(on: req).group(.and) { and in
            and.filter(\.contactMethodAddress == contactMethod.contactMethodAddress)
            and.filter(\.accessPointID == contactMethod.accessPointID)
            }.all()
            .flatMap(to: AccessPointContactMethod.self) { methods in
                if methods.isEmpty {
                    return contactMethod.save(on: req)
                }
                else {
                    let duplicateContactMethodStatus = HTTPResponseStatus.custom(code: 600, reasonPhrase: "Duplicate Contact Method")
                    throw Abort(duplicateContactMethodStatus)
                }
        }

所以这导致我遇到了我遇到的问题。目前,我只检查其中是否有重复的地址AccessPoint,但我想做的是检查AccessPoint属于一个的所有对象Business,如果有任何AccessPoint包含AccessPointContactMethod相同地址的对象,那么我们中止请求.

因此,在我看来,我需要一种方法来获取Business祖父母对象并遍历[AccessPoint]子对象以查找任何重复项。

Vapor、响应式编程、数据库对我来说都是全新的,所以我确信我没有很好地描述这个问题,但如果有人能理解我想要做什么并且有任何想法,我会非常感激!

标签: iosswiftvapor

解决方案


推荐阅读