Repository navigation
Expand file tree
/
Copy pathTaskRepository.swift
More file actions
160 lines (135 loc) · 5.72 KB
/
Copy pathTaskRepository.swift
File metadata and controls
160 lines (135 loc) · 5.72 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import Fluent
import Vapor
import Foundation
import Shared
// TEACHING NOTE: Repository Pattern
// This abstracts database operations from route handlers, following iOS best practices.
// In iOS, you'd use repositories to abstract Core Data or network calls from ViewControllers.
// This promotes:
// - Separation of concerns
// - Testability (can mock repositories)
// - Reusability (multiple routes can share logic)
struct TaskRepository {
let database: Database
// MARK: - Initialization
// Teaching note: This is dependency injection - passing in the database
// Similar to passing Core Data context to ViewControllers
init(database: Database) {
self.database = database
}
// MARK: - Read Operations (Queries)
/// Fetch all tasks (unfiltered)
/// Teaching note: Like NSFetchRequest with no predicate
func findAll() async throws -> [TaskItem] {
let models = try await TaskModel.query(on: database).all()
return try models.map { try $0.toDTO() }
}
/// Fetch only active (non-archived) tasks
/// Teaching note: Like NSFetchRequest with NSPredicate(format: "isArchived == NO")
func findActive() async throws -> [TaskItem] {
let models = try await TaskModel.query(on: database)
.filter(\.$isArchived == false)
.all()
return try models.map { try $0.toDTO() }
}
/// Fetch completed tasks (excluding archived)
/// Teaching note: Like NSPredicate with compound conditions (isCompleted AND NOT isArchived)
func findCompleted() async throws -> [TaskItem] {
let models = try await TaskModel.query(on: database)
.filter(\.$isCompleted == true)
.filter(\.$isArchived == false)
.all()
return try models.map { try $0.toDTO() }
}
/// Fetch archived tasks
/// Teaching note: Like filtering for "deleted" items (soft delete pattern)
func findArchived() async throws -> [TaskItem] {
let models = try await TaskModel.query(on: database)
.filter(\.$isArchived == true)
.all()
return try models.map { try $0.toDTO() }
}
/// Fetch overdue tasks (due date in the past, not completed)
/// Teaching note: Like NSPredicate comparing dates (dueDate < now AND isCompleted == NO)
func findOverdue() async throws -> [TaskItem] {
let now = Date()
let models = try await TaskModel.query(on: database)
.filter(\.$isCompleted == false)
.filter(\.$isArchived == false)
.filter(\.$dueDate < now)
.all()
return try models.map { try $0.toDTO() }
}
/// Find a single task by ID
/// Teaching note: Like Core Data fetchRequest with predicate for specific ObjectID
func find(id: UUID) async throws -> TaskItem? {
guard let model = try await TaskModel.find(id, on: database) else {
return nil
}
return try model.toDTO()
}
// MARK: - Write Operations (Create, Update, Delete)
/// Create a new task
/// Teaching note: Like inserting new NSManagedObject into Core Data context
func create(_ dto: TaskItem) async throws -> TaskItem {
let model = TaskModel.fromDTO(dto)
try await model.save(on: database)
return try model.toDTO()
}
/// Update an existing task
/// Teaching note: Like updating properties on NSManagedObject then saving context
func update(id: UUID, with dto: TaskItem) async throws -> TaskItem {
guard let model = try await TaskModel.find(id, on: database) else {
throw Abort(.notFound, reason: "Task not found")
}
model.update(from: dto)
try await model.save(on: database)
return try model.toDTO()
}
/// Toggle task completion status
/// Teaching note: Common iOS pattern - toggle boolean and set timestamp
func toggleComplete(id: UUID) async throws -> TaskItem {
guard let model = try await TaskModel.find(id, on: database) else {
throw Abort(.notFound, reason: "Task not found")
}
model.isCompleted.toggle()
// Set completedAt timestamp when marking complete, clear when uncompleting
if model.isCompleted {
model.completedAt = Date()
} else {
model.completedAt = nil
}
try await model.save(on: database)
return try model.toDTO()
}
/// Archive a task (soft delete)
/// Teaching note: Soft delete pattern - mark as deleted rather than actually deleting
/// Common in iOS for undo functionality
func archive(id: UUID) async throws -> TaskItem {
guard let model = try await TaskModel.find(id, on: database) else {
throw Abort(.notFound, reason: "Task not found")
}
model.isArchived = true
try await model.save(on: database)
return try model.toDTO()
}
/// Unarchive a task
/// Teaching note: Restore from soft delete
func unarchive(id: UUID) async throws -> TaskItem {
guard let model = try await TaskModel.find(id, on: database) else {
throw Abort(.notFound, reason: "Task not found")
}
model.isArchived = false
try await model.save(on: database)
return try model.toDTO()
}
/// Permanently delete a task (hard delete)
/// Teaching note: Like calling delete() on NSManagedObject
/// Usually prefer archiving for user safety
func delete(id: UUID) async throws {
guard let model = try await TaskModel.find(id, on: database) else {
throw Abort(.notFound, reason: "Task not found")
}
try await model.delete(on: database)
}
}