-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
159 lines (135 loc) · 3.89 KB
/
Program.cs
File metadata and controls
159 lines (135 loc) · 3.89 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
using Microsoft.Extensions.DependencyInjection;
using TaskTrackerCLI.Interfaces;
using TaskTrackerCLI.Services;
var serviceProvider = BuildServiceProvider();
var taskService = serviceProvider.GetRequiredService<ITaskService>();
var printService = serviceProvider.GetRequiredService<IPrintService>();
if (args.Length == 0)
{
printService.PrintError("Please provide a command.");
return;
}
string command = args[0].ToLower();
string[] commandArgs = [.. args.Skip(1)];
switch (command)
{
case "add":
await AddCommand(commandArgs);
break;
case "update":
await UpdateCommand(commandArgs);
break;
case "delete":
await DeleteCommand(commandArgs);
break;
case "mark-in-progress":
await MarkTaskWithStatusCommand(commandArgs, "in-progress");
break;
case "mark-done":
await MarkTaskWithStatusCommand(commandArgs, "done");
break;
case "list":
await ListTasksCommand(commandArgs);
break;
default:
printService.PrintError("Invalid command.");
break;
}
async Task AddCommand(string[] commandArgs)
{
if (commandArgs.Length != 1)
{
printService.PrintError("Please provide a task description.");
return;
}
var result = await taskService.AddTask(commandArgs[0]);
if (!result.IsSuccess)
{
printService.PrintError(result.ErrorMessage!);
return;
}
printService.PrintSuccess($"Task added successfully (ID: {result.Value})");
}
async Task UpdateCommand(string[] commandArgs)
{
if (commandArgs.Length != 2)
{
printService.PrintError("Please provide a task ID and a new description.");
return;
}
if (!int.TryParse(commandArgs[0], out int id))
{
printService.PrintError("Invalid task ID.");
return;
}
var result = await taskService.UpdateTask(id, commandArgs[1]);
if (!result.IsSuccess)
{
printService.PrintError(result.ErrorMessage!);
return;
}
printService.PrintSuccess($"Task updated successfully (ID: {id})");
}
async Task DeleteCommand(string[] commandArgs)
{
if (commandArgs.Length != 1)
{
printService.PrintError("Please provide a task ID.");
return;
}
if (!int.TryParse(commandArgs[0], out int id))
{
printService.PrintError("Invalid task ID.");
return;
}
var result = await taskService.DeleteTask(id);
if (!result.IsSuccess)
{
printService.PrintError(result.ErrorMessage!);
return;
}
printService.PrintSuccess($"Task deleted successfully (ID: {id})");
}
async Task MarkTaskWithStatusCommand(string[] commandArgs, string status)
{
if (commandArgs.Length != 1)
{
printService.PrintError("Please provide a task ID.");
return;
}
if (!int.TryParse(commandArgs[0], out int id))
{
printService.PrintError("Invalid task ID.");
return;
}
var result = await taskService.MarkTaskWithStatus(id, status);
if (!result.IsSuccess)
{
printService.PrintError(result.ErrorMessage!);
return;
}
printService.PrintSuccess($"Task marked as '{status}' successfully (ID: {id})");
}
async Task ListTasksCommand(string[] commandArgs)
{
string? status = commandArgs.Length > 0 ? commandArgs[0] : null;
if (commandArgs.Length > 0 && string.IsNullOrWhiteSpace(status))
{
printService.PrintError("Invalid status.");
return;
}
var result = await taskService.GetTasks(status);
if (!result.IsSuccess)
{
printService.PrintError(result.ErrorMessage!);
return;
}
printService.PrintTasks(result.Value ?? []);
}
ServiceProvider BuildServiceProvider()
{
var services = new ServiceCollection();
services.AddScoped<ITaskService, TaskService>();
services.AddScoped<IPrintService, PrintService>();
return services.BuildServiceProvider();
}