-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathCodingController.cs
More file actions
150 lines (121 loc) · 4.08 KB
/
CodingController.cs
File metadata and controls
150 lines (121 loc) · 4.08 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
using CodingTracker.urasylmaz1.Helpers;
using CodingTracker.urasylmaz1.Models;
using Dapper;
using Microsoft.Data.Sqlite;
using Spectre.Console;
using System;
using System.Collections.Generic;
using System.Text;
namespace CodingTracker.urasylmaz1.Controllers
{
public class CodingController
{
private readonly string _connectionString;
public CodingController(string connectionString)
{
_connectionString = connectionString;
}
public void Run()
{
bool running = true;
while (running)
{
Console.Clear();
var choice = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("[green]Coding Tracker[/]")
.AddChoices(
"Add Session",
"View Sessions",
"Delete Session",
"Exit"
)
);
switch (choice)
{
case "Add Session":
AddSession();
break;
case "View Sessions":
ReadSession();
break;
case "Delete Session":
RemoveSession();
break;
case "Exit":
running = false;
break;
}
if (running)
{
AnsiConsole.MarkupLine(
"\n[yellow]Press any key to continue...[/]"
);
Console.ReadKey();
}
}
}
public void AddSession()
{
DateTime start =
UserInput.GetDateTime(
"Enter start time (yyyy-MM-dd HH:mm):"
);
DateTime end =
UserInput.GetDateTime(
"Enter end time (yyyy-MM-dd HH:mm):"
);
string duration = (end - start).ToString();
CodingSession session = new()
{
StartTime = start,
EndTime = end,
Duration = duration
};
using var connection =
new SqliteConnection(_connectionString);
string sql = @"
INSERT INTO CodingSessions
(StartTime, EndTime, Duration)
VALUES
(@StartTime, @EndTime, @Duration)
";
connection.Execute(sql, session);
}
public void ReadSession()
{
using var connection = new SqliteConnection(_connectionString);
List<CodingSession> sessions = connection.Query<CodingSession>("SELECT * FROM CodingSessions").AsList();
var table = new Table();
table.AddColumn("Id");
table.AddColumn("Start Time");
table.AddColumn("End Time");
table.AddColumn("Duration");
foreach (var session in sessions)
{
table.AddRow(
session.Id.ToString(),
session.StartTime.ToString(),
session.EndTime.ToString(),
session.Duration.ToString()
);
}
AnsiConsole.Write(table);
}
public void RemoveSession()
{
ReadSession();
int id = AnsiConsole.Ask<int>("Enter session Id to delete:");
bool exists = Validation.SessionExists(_connectionString, id );
if (!exists)
{
AnsiConsole.MarkupLine("[red]Session not found.[/]" );
return;
}
using var connection = new SqliteConnection(_connectionString);
string sql ="DELETE FROM CodingSessions WHERE Id = @Id";
connection.Execute(sql, new { Id = id });
AnsiConsole.MarkupLine("[green]Session deleted.[/]");
}
}
}