-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandHandlers.go
More file actions
106 lines (96 loc) · 2.62 KB
/
Copy pathcommandHandlers.go
File metadata and controls
106 lines (96 loc) · 2.62 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
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
func handleMarkCommand(userInput string) {
if !strings.HasPrefix(userInput, "mark-in-progress") && !strings.HasPrefix(userInput, "mark-done") {
fmt.Println("Error: Unsupported command. Supported commands are: mark-in-progress, mark-done")
return
}
inputs := strings.Split(userInput, " ")
if len(inputs) < 2 {
fmt.Println("Error: Missing arguments")
return
}
taskId := inputs[1]
if id, err := strconv.Atoi(taskId); err == nil {
index, error := getTaskById(id)
if error != nil {
fmt.Println("Error: ", error)
return
}
command := inputs[0]
if command == "mark-in-progress" {
TaskList[index].Status = "in-progress"
} else {
TaskList[index].Status = "done"
}
fmt.Println("Task status was updated successfully")
writeDataToFile()
}
}
func handleDeleteCommand(userInput string) {
input := strings.Trim(userInput, "delete ")
taskId := strings.Split(input, " ")[0]
if id, err := strconv.Atoi(taskId); err == nil {
index, error := getTaskById(id)
if error != nil {
fmt.Println(error)
return
}
TaskList = append(TaskList[:index], TaskList[index+1:]...)
fmt.Printf("Item of ID:%+v has been deleted successfully.\n", id)
writeDataToFile()
}
}
func handleUpdateCommand(userInput string) {
input := strings.Trim(userInput, "update ")
taskId := strings.Split(input, " ")[0]
if id, err := strconv.Atoi(taskId); err == nil {
index, error := getTaskById(id)
if error != nil {
fmt.Println("An Error occurred: ", error)
return
}
TaskList[index].Description = strings.Trim(input, fmt.Sprintf("%+v \"", taskId))
TaskList[index].UpdatedAt = time.Now()
fmt.Println("Task updated successfully...")
writeDataToFile()
} else {
fmt.Println("Incorrect id format: ", taskId)
}
}
func handleAddCommand(input string) {
taskDescription := strings.Trim(input, "add \"")
addTask(taskDescription)
writeDataToFile()
}
func handleListCommand(userInput string) {
inputs := strings.Split(userInput, " ")
if len(inputs) <= 1 {
showAllTasks()
return
}
status := inputs[1]
if status != "done" && status != "todo" && status != "in-progress" {
fmt.Println("Error: Wrong status provided, available statuses are: done, todo, in-progress")
return
}
showTasksByStatus(status)
}
func handleShowCommand(userInput string) {
input := strings.Trim(userInput, "show \"")
if id, err := strconv.Atoi(input); err == nil {
taskIndex, err := getTaskById(id)
if err != nil {
fmt.Println("Error: ", err)
} else {
showTask(taskIndex)
}
} else {
fmt.Println("Error: Must provide an integer ID of the task that you would like to show.")
}
}