Reuse base fetch request building code
Set .predicate first for consistency.
This commit is contained in:
parent
ae6b5c50d9
commit
57f7b15aa2
|
@ -65,13 +65,13 @@ class ProfileRepository: Repository {
|
||||||
|
|
||||||
func profile(withId id: UUID) -> Profile? {
|
func profile(withId id: UUID) -> Profile? {
|
||||||
let request = CDProfile.fetchRequest()
|
let request = CDProfile.fetchRequest()
|
||||||
request.sortDescriptors = [
|
|
||||||
.init(keyPath: \CDProfile.lastUpdate, ascending: false)
|
|
||||||
]
|
|
||||||
request.predicate = NSPredicate(
|
request.predicate = NSPredicate(
|
||||||
format: "uuid == %@",
|
format: "uuid == %@",
|
||||||
id.uuidString
|
id.uuidString
|
||||||
)
|
)
|
||||||
|
request.sortDescriptors = [
|
||||||
|
.init(keyPath: \CDProfile.lastUpdate, ascending: false)
|
||||||
|
]
|
||||||
do {
|
do {
|
||||||
let results = try context.fetch(request)
|
let results = try context.fetch(request)
|
||||||
guard let recent = results.first else {
|
guard let recent = results.first else {
|
||||||
|
|
|
@ -49,12 +49,7 @@ class InfrastructureRepository: Repository {
|
||||||
return provider
|
return provider
|
||||||
}()
|
}()
|
||||||
|
|
||||||
let request = CDInfrastructure.fetchRequest()
|
let request = fetchRequest(infrastructure.name, vpnProtocol)
|
||||||
request.predicate = NSPredicate(
|
|
||||||
format: "provider.name == %@ AND vpnProtocol == %@",
|
|
||||||
infrastructure.name,
|
|
||||||
vpnProtocol.rawValue
|
|
||||||
)
|
|
||||||
let existing = try context.fetch(request)
|
let existing = try context.fetch(request)
|
||||||
existing.forEach(context.delete)
|
existing.forEach(context.delete)
|
||||||
|
|
||||||
|
@ -75,15 +70,10 @@ class InfrastructureRepository: Repository {
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultUsername(forProviderWithName name: ProviderName, vpnProtocol: VPNProtocolType) -> String? {
|
func defaultUsername(forProviderWithName name: ProviderName, vpnProtocol: VPNProtocolType) -> String? {
|
||||||
let request = CDInfrastructure.fetchRequest()
|
let request = fetchRequest(name, vpnProtocol)
|
||||||
request.sortDescriptors = [
|
request.sortDescriptors = [
|
||||||
.init(keyPath: \CDInfrastructure.lastUpdate, ascending: false)
|
.init(keyPath: \CDInfrastructure.lastUpdate, ascending: false)
|
||||||
]
|
]
|
||||||
request.predicate = NSPredicate(
|
|
||||||
format: "provider.name == %@ AND vpnProtocol == %@",
|
|
||||||
name,
|
|
||||||
vpnProtocol.rawValue
|
|
||||||
)
|
|
||||||
request.relationshipKeyPathsForPrefetching = [
|
request.relationshipKeyPathsForPrefetching = [
|
||||||
"defaults"
|
"defaults"
|
||||||
]
|
]
|
||||||
|
@ -99,19 +89,11 @@ class InfrastructureRepository: Repository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func lastInfrastructureUpdate(
|
func lastInfrastructureUpdate(withName name: ProviderName, vpnProtocol: VPNProtocolType) -> Date? {
|
||||||
withName name: ProviderName,
|
let request = fetchRequest(name, vpnProtocol)
|
||||||
vpnProtocol: VPNProtocolType
|
|
||||||
) -> Date? {
|
|
||||||
let request = CDInfrastructure.fetchRequest()
|
|
||||||
request.sortDescriptors = [
|
request.sortDescriptors = [
|
||||||
.init(keyPath: \CDInfrastructure.lastUpdate, ascending: false)
|
.init(keyPath: \CDInfrastructure.lastUpdate, ascending: false)
|
||||||
]
|
]
|
||||||
request.predicate = NSPredicate(
|
|
||||||
format: "provider.name == %@ AND vpnProtocol == %@",
|
|
||||||
name,
|
|
||||||
vpnProtocol.rawValue
|
|
||||||
)
|
|
||||||
request.relationshipKeyPathsForPrefetching = [
|
request.relationshipKeyPathsForPrefetching = [
|
||||||
"provider",
|
"provider",
|
||||||
"provider.infrastructures"
|
"provider.infrastructures"
|
||||||
|
@ -131,6 +113,16 @@ class InfrastructureRepository: Repository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func fetchRequest(_ name: ProviderName, _ vpnProtocol: VPNProtocolType) -> NSFetchRequest<CDInfrastructure> {
|
||||||
|
let request = CDInfrastructure.fetchRequest()
|
||||||
|
request.predicate = NSPredicate(
|
||||||
|
format: "provider.name == %@ AND vpnProtocol == %@",
|
||||||
|
name,
|
||||||
|
vpnProtocol.rawValue
|
||||||
|
)
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
|
||||||
private func providerDTO(forName name: ProviderName) throws -> CDProvider? {
|
private func providerDTO(forName name: ProviderName) throws -> CDProvider? {
|
||||||
let request = CDProvider.fetchRequest()
|
let request = CDProvider.fetchRequest()
|
||||||
request.sortDescriptors = [
|
request.sortDescriptors = [
|
||||||
|
|
|
@ -58,10 +58,10 @@ class ProviderRepository: Repository {
|
||||||
|
|
||||||
func provider(withName name: ProviderName) -> ProviderMetadata? {
|
func provider(withName name: ProviderName) -> ProviderMetadata? {
|
||||||
let request = CDProvider.fetchRequest()
|
let request = CDProvider.fetchRequest()
|
||||||
|
request.predicate = NSPredicate(format: "name == %@", name)
|
||||||
request.sortDescriptors = [
|
request.sortDescriptors = [
|
||||||
.init(keyPath: \CDProvider.lastUpdate, ascending: false)
|
.init(keyPath: \CDProvider.lastUpdate, ascending: false)
|
||||||
]
|
]
|
||||||
request.predicate = NSPredicate(format: "name == %@", name)
|
|
||||||
request.relationshipKeyPathsForPrefetching = [
|
request.relationshipKeyPathsForPrefetching = [
|
||||||
"infrastructures"
|
"infrastructures"
|
||||||
]
|
]
|
||||||
|
|
|
@ -139,9 +139,8 @@ class ServerRepository: Repository {
|
||||||
|
|
||||||
func anyDefaultServer(forProviderWithName providerName: ProviderName, vpnProtocol: VPNProtocolType) -> ProviderServer? {
|
func anyDefaultServer(forProviderWithName providerName: ProviderName, vpnProtocol: VPNProtocolType) -> ProviderServer? {
|
||||||
let request = CDInfrastructureServer.fetchRequest()
|
let request = CDInfrastructureServer.fetchRequest()
|
||||||
let format = "countryCode == category.infrastructure.defaults.countryCode AND category.infrastructure.provider.name == %@ AND category.infrastructure.vpnProtocol == %@"
|
|
||||||
request.predicate = NSPredicate(
|
request.predicate = NSPredicate(
|
||||||
format: format,
|
format: "countryCode == category.infrastructure.defaults.countryCode AND category.infrastructure.provider.name == %@ AND category.infrastructure.vpnProtocol == %@",
|
||||||
providerName,
|
providerName,
|
||||||
vpnProtocol.rawValue
|
vpnProtocol.rawValue
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue