-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask_controller.go
281 lines (246 loc) · 7.22 KB
/
task_controller.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package controllers
import (
"errors"
"time"
"codeberg.org/dynnian/gplan/models"
"codeberg.org/dynnian/gplan/repository"
"codeberg.org/dynnian/gplan/utils"
)
// Error constants for task operations.
var (
ErrNoTaskName = errors.New("task name is required")
ErrNoProject = errors.New("project name or numeric ID is required")
ErrNoProjectFound = errors.New("project not found")
ErrInvalidParentTask = errors.New("parent task must be identified by a numeric ID")
ErrInvalidDueDate = errors.New("invalid due date format")
ErrTaskNotFound = errors.New("task not found")
ErrParentTaskNotFound = errors.New("parent task not found")
)
// TaskController manages the task-related business logic.
type TaskController struct {
repo *repository.Repository
}
// NewTaskController creates and returns a new instance of TaskController.
func NewTaskController(repo *repository.Repository) *TaskController {
return &TaskController{repo: repo}
}
// CreateTask handles the creation of a new task.
func (tc *TaskController) CreateTask(
name, description, projectIdentifier, parentTaskIdentifier, dueDateStr string,
priority int,
) error {
// Validate mandatory arguments
if name == "" {
return ErrNoTaskName
}
if projectIdentifier == "" {
return ErrNoProject
}
// Try to parse projectIdentifier as an integer (ID), otherwise get the project by name
projectID, projectErr := utils.ParseIntOrError(projectIdentifier)
if projectErr != nil {
project, lookupErr := tc.repo.GetProjectByName(projectIdentifier)
if lookupErr != nil || project == nil {
return ErrNoProjectFound
}
projectID = project.ID
} else {
// Check if the project exists using the ID
project, getProjectErr := tc.repo.GetProjectByID(projectID)
if getProjectErr != nil || project == nil {
return ErrNoProjectFound
}
}
// Get parent task ID (optional)
var parentTaskID *int
if parentTaskIdentifier != "" {
id, parentErr := utils.ParseIntOrError(parentTaskIdentifier)
if parentErr != nil {
return ErrInvalidParentTask
}
parentTaskID = &id
}
// Parse due date (optional)
var dueDate *time.Time
if dueDateStr != "" {
parsedDate, dueDateErr := utils.ParseDueDate(dueDateStr)
if dueDateErr != nil {
return ErrInvalidDueDate
}
dueDate = parsedDate
}
// Create a new task
task := &models.Task{
Name: name,
Description: description,
ProjectID: projectID,
DueDate: dueDate,
Priority: priority,
ParentTaskID: parentTaskID,
}
// Store the task in the repository
if createErr := tc.repo.CreateTask(task); createErr != nil {
return createErr
}
return nil
}
// EditTask handles updating an existing task by its ID.
func (tc *TaskController) EditTask(
id int,
name, description, dueDateStr string,
priority int,
parentTaskIdentifier string,
) error {
task, getTaskErr := tc.repo.GetTaskByID(id)
if getTaskErr != nil {
return ErrTaskNotFound
}
// Apply updates
if name != "" {
task.Name = name
}
if description != "" {
task.Description = description
}
if dueDateStr != "" {
dueDate, dueDateErr := utils.ParseDueDate(dueDateStr)
if dueDateErr != nil {
return ErrInvalidDueDate
}
task.DueDate = dueDate
}
if priority != 0 {
task.Priority = priority
}
if parentTaskIdentifier != "" {
parentTaskID, parentErr := utils.ParseIntOrError(parentTaskIdentifier)
if parentErr != nil {
return ErrInvalidParentTask
}
task.ParentTaskID = &parentTaskID
}
// Update the task in the repository
if updateErr := tc.repo.UpdateTask(task); updateErr != nil {
return updateErr
}
return nil
}
// ListTasks returns all tasks stored in the repository.
func (tc *TaskController) ListTasks() ([]*models.Task, error) {
tasks, getAllErr := tc.repo.GetAllTasks()
if getAllErr != nil {
return nil, getAllErr
}
return tasks, nil
}
// ListTasksByProjectFilter returns tasks filtered by project.
func (tc *TaskController) ListTasksByProjectFilter(
projectFilter string,
) ([]*models.Task, *models.Project, error) {
if projectFilter == "" {
// Return all tasks if no filter is provided
tasks, getAllErr := tc.repo.GetAllTasks()
return tasks, nil, getAllErr
}
// Try to parse the projectFilter as a numeric ID first
projectID, projectErr := utils.ParseIntOrError(projectFilter)
if projectErr != nil {
// If parsing fails, assume it's a project name and get the project by name
project, lookupErr := tc.repo.GetProjectByName(projectFilter)
if lookupErr != nil || project == nil {
return nil, nil, ErrNoProjectFound
}
projectID = project.ID
}
// Retrieve the project by ID
project, getProjectErr := tc.repo.GetProjectByID(projectID)
if getProjectErr != nil || project == nil {
return nil, nil, ErrNoProjectFound
}
// Get tasks by project ID
tasks, getTasksErr := tc.repo.GetTasksByProjectID(project.ID)
if getTasksErr != nil {
return nil, nil, getTasksErr
}
return tasks, project, nil
}
// ToggleTaskCompletion toggles the completion status of a task.
// If recursive is true, it also toggles the completion status of all subtasks.
func (tc *TaskController) ToggleTaskCompletion(id int, recursive bool) (string, error) {
// Retrieve the task by its ID
task, getTaskErr := tc.repo.GetTaskByID(id)
if getTaskErr != nil {
return "", ErrTaskNotFound
}
// Toggle the completion status
completion := "not completed"
if task.TaskCompleted {
task.TaskCompleted = false
task.CompletionDate = nil
} else {
task.TaskCompleted = true
now := time.Now()
task.CompletionDate = &now
completion = "completed"
}
// Update the task in the repository
updateErr := tc.repo.UpdateTask(task)
if updateErr != nil {
return "", updateErr
}
// If recursive flag is set, toggle the completion status of all subtasks
if recursive {
subtasks, getSubtasksErr := tc.ListSubtasks(id)
if getSubtasksErr != nil {
return "", getSubtasksErr
}
for _, subtask := range subtasks {
if _, toggleErr := tc.ToggleTaskCompletion(subtask.ID, true); toggleErr != nil {
return "", toggleErr
}
}
}
return completion, nil
}
// RemoveTask handles the recursive removal of a task and all its subtasks.
func (tc *TaskController) RemoveTask(id int) error {
// Get all subtasks for the given task
subtasks, getSubtasksErr := tc.repo.GetSubtasks(id)
if getSubtasksErr != nil {
return getSubtasksErr
}
// Recursively remove subtasks
for _, subtask := range subtasks {
if removeErr := tc.RemoveTask(subtask.ID); removeErr != nil {
return removeErr
}
}
// Remove the parent task
if deleteErr := tc.repo.DeleteTask(id); deleteErr != nil {
return deleteErr
}
return nil
}
// GetTaskByID returns the task details for a given task ID.
func (tc *TaskController) GetTaskByID(id int) (*models.Task, error) {
task, getTaskErr := tc.repo.GetTaskByID(id)
if getTaskErr != nil {
return nil, ErrTaskNotFound
}
return task, nil
}
func (tc *TaskController) GetTaskProjectName(id int) (*string, error) {
task, getTaskErr := tc.GetTaskByID(id)
if getTaskErr != nil {
return nil, getTaskErr
}
return &task.Project.Name, nil
}
// ListSubtasks returns the subtasks for a given task ID.
func (tc *TaskController) ListSubtasks(taskID int) ([]*models.Task, error) {
subtasks, getSubtasksErr := tc.repo.GetSubtasks(taskID)
if getSubtasksErr != nil {
return nil, getSubtasksErr
}
return subtasks, nil
}