-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFoodCategory+CoreDataClass.swift
More file actions
133 lines (113 loc) · 4.74 KB
/
FoodCategory+CoreDataClass.swift
File metadata and controls
133 lines (113 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// FoodCategory+CoreDataClass.swift
// EasyFPU
//
// Created by Ulrich Rüth on 13/08/2025.
// Copyright © 2025 Ulrich Rüth. All rights reserved.
//
//
import Foundation
import CoreData
public class FoodCategory: NSManagedObject {
//
// MARK: - Static methods for entity creation/deletion/fetching
//
static func fetchAll(
category: FoodItemCategory? = nil,
viewContext: NSManagedObjectContext = CoreDataStack.viewContext
) -> [FoodCategory] {
let request: NSFetchRequest<FoodCategory> = FoodCategory.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
if let category = category {
request.predicate = NSPredicate(format: "category == %@", category.rawValue)
}
guard let foodCategories = try? CoreDataStack.viewContext.fetch(request) else {
return []
}
return foodCategories
}
static func deleteAll(viewContext: NSManagedObjectContext = CoreDataStack.viewContext) {
FoodCategory.fetchAll(viewContext: viewContext).forEach({
viewContext.delete($0)
})
try? viewContext.save()
}
/// Creates a new Core Data FoodCategory with the given values. Does not check for duplicates.
/// - Parameters:
/// - id: The unique identifier for the FoodCategory.
/// - name: The name of the FoodCategory.
/// - category: The category type of the FoodCategory, as a FoodItemCategory..
/// - saveContext: Whether to save the Core Data context after creation.
/// - Returns: The created FoodCategory object.
static func create(id: UUID, name: String, category: FoodItemCategory, saveContext: Bool) -> FoodCategory {
// Create the FoodCategory
let cdFoodCategory = FoodCategory(context: CoreDataStack.viewContext)
// Fill data
cdFoodCategory.id = id
cdFoodCategory.name = name
cdFoodCategory.category = category.rawValue
// Save
if saveContext {
CoreDataStack.shared.save()
}
return cdFoodCategory
}
/// Deletes the given FoodCategory from Core Data.
/// - Parameter foodCategory: The FoodCategory to delete.
/// - Parameter saveContext: Whether to save the Core Data context after deletion.
static func delete(_ foodCategory: FoodCategory, saveContext: Bool) {
// Delete the food category
CoreDataStack.viewContext.delete(foodCategory)
// And save the context
if saveContext {
CoreDataStack.shared.save()
}
}
/// Checks if a FoodCategory with the given id exists.
/// - Parameters:
/// - name: The name of the FoodCategory to check for.
/// - category: The category type of the FoodCategory, as a FoodItemCategory.
/// - isNew: Whether this is a new FoodCategory (true) or an existing one being edited (false).
/// - Returns: True if a FoodCategory with the given name exists, false otherwise.
static func exists(name: String, category: FoodItemCategory, isNew: Bool) -> Bool {
return getFoodCategoriesByName(name: name, category: category)?.count ?? 0 > (isNew ? 0 : 1)
}
/**
Returns the Core Data FoodCategory with the given id.
- Parameter id: The Core Data entry id.
- Returns: The related Core Data FoodCategory, nil if not found.
*/
static func getFoodCategoryByID(id: UUID) -> FoodCategory? {
let predicate = NSPredicate(format: "id == %@", id as CVarArg)
let request: NSFetchRequest<FoodCategory> = FoodCategory.fetchRequest()
request.predicate = predicate
do {
let result = try CoreDataStack.viewContext.fetch(request)
if !result.isEmpty {
return result[0]
}
} catch {
debugPrint("Error fetching food category: \(error)")
}
return nil
}
/**
Returns the Core Data FoodCategory with the given name.
- Parameter name: The Core Data entry name.
- Returns: The related Core Data FoodCategory, nil if not found.
*/
static func getFoodCategoriesByName(name: String, category: FoodItemCategory) -> [FoodCategory]? {
let predicate = NSPredicate(format: "name == %@ AND category == %@", name, category.rawValue)
let request: NSFetchRequest<FoodCategory> = FoodCategory.fetchRequest()
request.predicate = predicate
do {
let result = try CoreDataStack.viewContext.fetch(request)
if !result.isEmpty {
return result
}
} catch {
debugPrint("Error fetching food category: \(error)")
}
return nil
}
}