//
//  IntentDataProvider.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 25/01/2023.
//

import Foundation
import CoreData

class IntentDataProvider {
    
    static let main: IntentDataProvider = IntentDataProvider()
    
    func countdowns() throws -> [Countdown] {
        let context = PersistenceController.shared.container.viewContext
        let request: NSFetchRequest<Countdown> = Countdown.fetchRequest()
        request.sortDescriptors = [NSSortDescriptor(keyPath: (\Countdown.order), ascending: true)]
        return try context.fetch(request)
    }
    
    func countdown(id: String) -> Countdown? {
        let context = PersistenceController.shared.container.viewContext
        return context.object(stringId: id) as? Countdown
    }
    
}
