-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTodoItemsController.cs
More file actions
151 lines (126 loc) · 5.1 KB
/
TodoItemsController.cs
File metadata and controls
151 lines (126 loc) · 5.1 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
namespace TodoList.ConsoleApp.Controllers
{
using System.IO;
using System.Reflection;
using Colorful;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TodoList.ConsoleApp.Commands;
using TodoList.Core.Boundaries;
public sealed class TodoItemsController
{
private IUseCase<Core.Boundaries.Todo.Request> todoUseCase;
private Core.Boundaries.Remove.IUseCase removeUseCase;
private Core.Boundaries.List.IUseCase listUseCase;
private IUseCase<Core.Boundaries.Rename.Request> renameUseCase;
private Core.Boundaries.Do.IUseCase doUseCase;
private Core.Boundaries.Undo.IUseCase undoUseCase;
public bool IsInteractive { get; private set; }
public TodoItemsController(
IUseCase<Core.Boundaries.Todo.Request> todoUseCase,
Core.Boundaries.Remove.IUseCase removeUseCase,
Core.Boundaries.List.IUseCase listUseCase,
IUseCase<Core.Boundaries.Rename.Request> renameUseCase,
Core.Boundaries.Do.IUseCase doUseCase,
Core.Boundaries.Undo.IUseCase undoUseCase)
{
this.todoUseCase = todoUseCase;
this.removeUseCase = removeUseCase;
this.listUseCase = listUseCase;
this.renameUseCase = renameUseCase;
this.doUseCase = doUseCase;
this.undoUseCase = undoUseCase;
}
public void Execute(TodoCommand todoCommand)
{
var request = new Core.Boundaries.Todo.Request(todoCommand.Title);
todoUseCase.Execute(request);
}
public void Execute(RemoveCommand removeCommand)
{
removeUseCase.Execute(removeCommand.Id);
}
public void List()
{
listUseCase.Execute();
}
public void Execute(RenameCommand renameCommand)
{
var request = new Core.Boundaries.Rename.Request(renameCommand.Id, renameCommand.NewTitle);
renameUseCase.Execute(request);
}
public void Execute(DoCommand doCommand)
{
doUseCase.Execute(doCommand.Id);
}
public void Execute(UndoCommand undoCommand)
{
undoUseCase.Execute(undoCommand.Id);
}
public void Help()
{
DisplayInstructions();
}
public void Interactive()
{
IsInteractive = true;
}
private void DisplayInstructions()
{
Console.WriteLine("The usage");
Console.WriteLine("\ttodo [title]");
Console.WriteLine("\ttodo ren [id] [title]");
Console.WriteLine("\ttodo do [id]");
Console.WriteLine("\ttodo undo [id]");
Console.WriteLine("\ttodo ls");
Console.WriteLine("\ttodo rm [id]");
Console.WriteLine("\ttodo gt [Gist Token]");
Console.WriteLine("\ttodo gi [Gist ID]");
Console.WriteLine("\texit");
}
public void Execute(GistTokenCommand gistTokenCommand)
{
SetGistTokenSettings(gistTokenCommand.GistToken);
}
public void Execute(GistIdCommand gistTokenCommand)
{
SetGistIdSettings(gistTokenCommand.GistId);
}
private void SetGistTokenSettings(string gistToken)
{
var appsettingsPath = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) +
Path.DirectorySeparatorChar +
"appsettings.json";
var contents = File.ReadAllText(appsettingsPath);
var configuration = JsonConvert.DeserializeObject<JObject>(contents);
configuration["GistToken"] = gistToken;
contents = JsonConvert.SerializeObject(configuration, Formatting.Indented);
File.WriteAllText(appsettingsPath, contents);
}
private void SetGistIdSettings(string gistId)
{
var appsettingsPath = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) +
Path.DirectorySeparatorChar +
"appsettings.json";
var contents = File.ReadAllText(appsettingsPath);
var configuration = JsonConvert.DeserializeObject<JObject>(contents);
configuration["GistId"] = gistId;
contents = JsonConvert.SerializeObject(configuration, Formatting.Indented);
File.WriteAllText(appsettingsPath, contents);
}
public void DevelopmentMode()
{
var appsettingsPath = Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) +
Path.DirectorySeparatorChar +
"appsettings.json";
var contents = File.ReadAllText(appsettingsPath);
var configuration = JsonConvert.DeserializeObject<JObject>(contents);
configuration["Environment"] = "Development";
contents = JsonConvert.SerializeObject(configuration, Formatting.Indented);
File.WriteAllText(appsettingsPath, contents);
}
}
}