Skip to content

Commit

Permalink
Merge pull request #28 from roanutil/feature/make-internal-aggregate-…
Browse files Browse the repository at this point in the history
…endpoint-public

Add a unified aggregate endpoint
  • Loading branch information
r-jarvis authored Jul 18, 2024
2 parents ca4d404 + 06df31f commit 742aa43
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@ import Foundation
// swiftlint:disable file_length

extension CoreDataRepository {
public enum AggregateFunction: String {
case count
case sum
case average
case min
case max
}

@inlinable
public func aggregate<Value: Numeric>(
function: AggregateFunction,
predicate: NSPredicate,
entityDesc: NSEntityDescription,
attributeDesc: NSAttributeDescription,
groupBy: NSAttributeDescription? = nil,
as valueType: Value.Type
) async -> Result<Value, CoreDataError> {
switch function {
case .count:
return await count(predicate: predicate, entityDesc: entityDesc, as: valueType)
default:
return await Self.send(
function: .sum,
context: context,
predicate: predicate,
entityDesc: entityDesc,
attributeDesc: attributeDesc,
groupBy: groupBy
)
}
}

// MARK: Count

/// Get the count or quantity of managed object instances that satisfy the predicate.
Expand Down Expand Up @@ -375,15 +407,6 @@ extension CoreDataRepository {

// MARK: Internals

@usableFromInline
enum AggregateFunction: String {
case count
case sum
case average
case min
case max
}

private static func aggregate<Value: Numeric>(
context: NSManagedObjectContext,
request: NSFetchRequest<NSDictionary>
Expand Down

0 comments on commit 742aa43

Please sign in to comment.