-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRUD.cs
More file actions
176 lines (137 loc) · 5.09 KB
/
Copy pathCRUD.cs
File metadata and controls
176 lines (137 loc) · 5.09 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
using System.Configuration;
using System.Data.SQLite;
using CodingTracker.Model;
using Dapper;
using Spectre.Console;
namespace CodingTracker;
internal class CRUD
{
static readonly string? con = ConfigurationManager.AppSettings.Get("connectionString");
internal static void CreateDB()
{
using (var connection = new SQLiteConnection(con))
{
connection.Open();
string sql = @"CREATE TABLE IF NOT EXISTS codingSessions (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Date TEXT,
Start TEXT,
End TEXT,
Duration TEXT)";
var cmd = connection.Execute(sql);
connection.Close();
}
}
internal static void AddToTable()
{
AnsiConsole.Clear();
DateOnly date = UserInput.GetDate();
TimeSpan[] times = UserInput.GetTime();
string duration = CalculateDuration(times[0], times[1]);
using (var connection = new SQLiteConnection(con))
{
connection.Open();
string sql =
@$"INSERT INTO codingSessions (Date, Start, End, Duration)
VALUES ('{date}', '{times[0]}', '{times[1]}', '{duration}')";
var cmd = connection.Execute(sql);
connection.Close();
AnsiConsole.MarkupLine("[springgreen3_1]Records added succesfully![/]");
}
}
internal static string CalculateDuration(TimeSpan start, TimeSpan end)
{
TimeSpan dur = end - start;
return dur.ToString("c");
}
internal static void ViewRecords()
{
AnsiConsole.Clear();
using (var connection = new SQLiteConnection(con))
{
connection.Open();
var reader = connection.ExecuteReader("SELECT * FROM codingSessions");
List<CodingSession> sessions = new List<CodingSession>();
var exists = connection.ExecuteScalar<bool>("SELECT COUNT(*) FROM codingSessions");
if (!exists)
{
AnsiConsole.MarkupLine("[red]This table is empty![/]");
connection.Close();
return;
}
while (reader.Read())
{
sessions.Add(
new CodingSession
{
Id = reader.GetInt32(0),
Date = DateOnly.Parse(reader.GetString(1)),
StartTime = TimeSpan.Parse(reader.GetString(2)),
EndTime = TimeSpan.Parse(reader.GetString(3)),
Duration = TimeSpan.Parse(reader.GetString(4))
}); ;
}
connection.Close();
var table = new Table().Centered();
table.BorderColor<Table>(Color.Cyan3);
table.Border = TableBorder.Horizontal;
AnsiConsole.Live(table)
.Start(ctx =>
{
table.AddColumn("[steelblue1_1]ID[/]");
ctx.Refresh();
Thread.Sleep(850);
table.AddColumn("[mediumturquoise]Date[/]");
ctx.Refresh();
Thread.Sleep(850);
table.AddColumn("[blue]Start Time[/]");
ctx.Refresh();
Thread.Sleep(850);
table.AddColumn("[red]End Time[/]");
ctx.Refresh();
Thread.Sleep(850);
table.AddColumn("[darkmagenta_1]Duration[/]");
ctx.Refresh();
Thread.Sleep(850);
foreach (var session in sessions)
{
table.AddRow($"[steelblue1_1]{session.Id}[/]", $"[mediumturquoise]{session.Date}[/]" , $"[blue]{session.StartTime}[/]", $"[red]{session.EndTime}[/]", $"[darkmagenta_1]{session.Duration}[/]");
ctx.Refresh();
Thread.Sleep(850);
}
});
}
}
internal static void DeleteRecords()
{
int id = UserInput.GetId();
using (var connection = new SQLiteConnection(con))
{
connection.Open();
var sql = $"DELETE FROM codingSessions WHERE Id = {id}";
var cmd = connection.Execute(sql);
connection.Close();
AnsiConsole.MarkupLine("[mediumpurple3_1]Records deleted succesfully![/]");
}
}
internal static void UpdateRecords()
{
int id = UserInput.GetId();
DateOnly date = UserInput.GetDate();
TimeSpan[] time = UserInput.GetTime();
string? duration = CalculateDuration(time[0], time[1]);
using (var connection = new SQLiteConnection(con))
{
connection.Open();
string sql = @$"UPDATE codingSessions SET
Date = '{date}',
Start = '{time[0]}',
End = '{time[1]}',
Duration = '{duration}'
WHERE Id = {id}";
var cmd = connection.Execute(sql);
connection.Close();
AnsiConsole.MarkupLine("[springgreen3_1]Records updated succesfully![/]");
}
}
}