//
//  PersistenceDELETE.swift
//  Feels
//
//  Created by Trey Tartt on 2/17/22.
//

import CoreData

extension PersistenceController {
    func clearDB() {
        let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "MoodEntry")
        let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
        
        do {
            try viewContext.executeAndMergeChanges(using: deleteRequest)
            try viewContext.save()
        } catch let error as NSError {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    }
    
    func delete(forDate: Date) {
        if let entry = PersistenceController.shared.getEntry(byDate: forDate) {
            viewContext.delete(entry)
            try! viewContext.save()
        }
    }
}
