Hi
I'm having a little difficulty trying to use this key path. So wondering if someone can help point me in the right direction. I'm basically using a protocol to see whether the resource requested is able to be accessed by the user in a REST API.
OrganizationRepresentable
protocol OrganizationRepresentable: Model {
typealias OrganizationKey = ReferenceWritableKeyPath<Self, UUID>
static var organizationKey: OrganizationKey { get } // basically a reference to the organization FK on another model
}
extension OrganizationRepresentable where Self: Model {
static func isBelongingToOrganization(user: User, on connection: DatabaseConnectable) throws -> EventLoopFuture<Int>
{
let userId = try user.requireID()
// checks a user can access this resource
return UserOrganization
.query(on: connection)
.filter(\.userId == userId)
.filter(\UserOrganization.organizationId == Self.organizationKey) --> Cannot convert value of type 'Bool' to expected argument type 'FilterOperator<_, _>'
.count()
}
}
So, the filter() above, I feel like I've tried almost every possible way to reference the keyPath, but I'm obviously missing something. For completeness, this is how I conform the model Campaign
and the extension on the request.
final class Campaign: Codable {
var id: UUID?
var organizationId: Organization.ID
...
}
extension Campaign: PostgreSQLUUIDModel {}
extension Campaign: Content {}
extension Campaign: Parameter {}
extension Campaign: OrganizationRepresentable {
static var organizationKey: OrganizationKey = \Campaign.organizationId
}
// extension on request to
extension Request {
func isOrganizationRepresentable<O>(resource: O.Type, user: User, on connection: Request) throws -> EventLoopFuture<Bool> where O: OrganizationRepresentable
{
return try resource.isBelongingToOrganization(user: user, on: connection).map { total in
return total > 0
}
}
}