-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCodingController.cs
More file actions
263 lines (210 loc) · 8.93 KB
/
Copy pathCodingController.cs
File metadata and controls
263 lines (210 loc) · 8.93 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
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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using ConsoleTableExt;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
namespace CodeTracker1
{
internal static class CodingController
{
static readonly string connectionString = ConfigurationManager.AppSettings.Get("ConnectionString");
internal static void GetUserCommand()
{
bool closeArea = false;
while (closeArea == false)
{
Console.WriteLine("\n\nMAIN MENU");
Console.WriteLine("\nWhat would you like to do?");
Console.WriteLine("\nType 0 to Close Application.");
Console.WriteLine("Type 1 to View All Records.");
Console.WriteLine("Type 2 to Insert Records.");
Console.WriteLine("Type 3 to Delete Records.");
Console.WriteLine("Type 4 to Update Records.");
Console.WriteLine("Type 5 to Generate Reports.");
string commandInput = Console.ReadLine();
int n;
if (string.IsNullOrEmpty(commandInput) || !int.TryParse(commandInput, out n))
{
Console.WriteLine("\nInvalid Command. Please choose an option\n");
continue;
}
int command = Convert.ToInt32(commandInput);
switch (command)
{
case 0:
closeArea = true;
break;
case 1:
Get();
break;
case 2:
Post();
break;
case 3:
Delete();
break;
case 4:
Update();
break;
case 5:
Reports.GetReportCommand();
break;
default:
Console.WriteLine("\nInvalid Command. Please type a number from 0 to 5.\n");
break;
}
}
}
internal static void Post()
{
long date = GetDateInput();
long duration = GetDurationInput();
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var tableCmd = connection.CreateCommand();
tableCmd.CommandText = $"INSERT INTO coding (date, duration) VALUES ({date}, {duration})";
tableCmd.ExecuteNonQuery();
connection.Close();
}
string inputDate = new DateTime(date).ToString("dd-MM-yy");
string inputDuration = new DateTime(duration).ToString("HH:mm");
Console.WriteLine($"\n\nYour time was logged. Date: {inputDate}; Duration: {inputDuration}.\n\n");
GetUserCommand();
}
internal static void Delete()
{
Console.WriteLine("\n\nPlease type the Id of the record would like to delete. Type 0 to return to main menu.\n\n");
string commandInput = Console.ReadLine();
if (string.IsNullOrEmpty(commandInput))
{
Console.WriteLine("\nYou have to type an Id.\n");
Delete();
}
var Id = Int32.Parse(commandInput);
if (Id == 0) GetUserCommand();
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var tableCmd = connection.CreateCommand();
tableCmd.CommandText = $"DELETE from coding WHERE Id = '{Id}'";
int rowCount = tableCmd.ExecuteNonQuery();
if (rowCount == 0)
{
Console.WriteLine($"\n\nRecord with Id {Id} doesn't exist. \n\n");
Delete();
}
Console.WriteLine($"\n\nRecord with Id {Id} was deleted. \n\n");
}
GetUserCommand();
}
internal static void Get()
{
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var tableCmd = connection.CreateCommand();
tableCmd.CommandText = "SELECT * FROM coding";
List<Coding> tableData = new List<Coding>();
SqliteDataReader reader = tableCmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
tableData.Add(
new Coding
{
Id = reader.GetInt32(0),
Date = new DateTime(reader.GetInt64(1)).ToShortDateString(),
Duration = new TimeSpan(reader.GetInt64(2)).ToString()
});
}
}
else
{
Console.WriteLine("\n\nNo rows found.\n\n");
}
reader.Close();
Console.WriteLine("\n\n");
ConsoleTableBuilder
.From(tableData)
.ExportAndWriteLine();
Console.WriteLine("\n\n");
GetUserCommand();
}
}
internal static void Update()
{
Console.WriteLine("\n\nPlease type Id of the record would like to update. Type 0 to return to main manu.\n\n");
string commandInput = Console.ReadLine();
if (string.IsNullOrEmpty(commandInput))
{
Console.WriteLine("\nYou have to type an Id.\n");
Update();
}
var Id = Int32.Parse(commandInput);
if (Id == 0) GetUserCommand();
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var checkCmd = connection.CreateCommand();
checkCmd.CommandText = $"SELECT EXISTS(SELECT 1 FROM coding WHERE Id = {Id})";
int checkQuery = Convert.ToInt32(checkCmd.ExecuteScalar());
if (checkQuery == 0)
{
Console.WriteLine($"\n\nRecord with Id {Id} doesn't exist.\n\n");
GetUserCommand();
}
long date = GetDateInput();
long duration = GetDurationInput();
var tableCmd = connection.CreateCommand();
tableCmd.CommandText = $"UPDATE coding SET date = {date}, duration = {duration} WHERE Id = {Id}";
tableCmd.ExecuteNonQuery();
string inputDate = new DateTime(date).ToString("dd-MM-yy");
string inputDuration = new DateTime(duration).ToString("HH:mm");
Console.WriteLine($"\n\nYour time was logged: date({inputDate}), duration({inputDuration}).\n\n");
connection.Close();
}
GetUserCommand();
}
internal static long GetDateInput()
{
Console.WriteLine("\n\nPlease insert the date: (Format: dd-mm-yy). Type 0 to return to main manu.\n\n");
DateTime result;
string dateInput = Console.ReadLine();
if (dateInput == "0") GetUserCommand();
bool success = DateTime.TryParseExact(dateInput, "dd-MM-yy", new CultureInfo("en-US"), DateTimeStyles.None, out result);
if (success)
{
var parsedDate = DateTime.ParseExact(dateInput, "dd-MM-yy", new CultureInfo("en-US"));
long date = parsedDate.Ticks;
return date;
}
Console.WriteLine("\n\nNot a valid date. Please insert the date with the format: dd-mm-yy.\n\n");
return GetDateInput();
}
internal static long GetDurationInput()
{
Console.WriteLine("\n\nPlease insert the duration: (Format: hh:mm). Type 0 to return to main manu.\n\n");
TimeSpan timeSpan;
string durationInput = Console.ReadLine();
if (durationInput == "0") GetUserCommand();
bool success = TimeSpan.TryParseExact(durationInput, "h\\:mm", CultureInfo.InvariantCulture, out timeSpan);
if (success)
{
var parsedDuration = TimeSpan.Parse(durationInput);
long date = parsedDuration.Ticks;
if (date < 0)
{
Console.WriteLine("\n\nNegative Time Not allowed.\n\n");
GetDurationInput();
}
return date;
}
Console.WriteLine("\n\nNot a valid time.\n\n");
return GetDurationInput();
}
}
}