Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CodingTracker/CodingTracker.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.66" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.3" />
<PackageReference Include="Spectre.Console" Version="0.54.0" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
63 changes: 63 additions & 0 deletions CodingTracker/Controllers/CodingController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Spectre.Console;
using CodingTracker.Models;
using CodingTracker.Data;

namespace CodingTracker.Controllers;

internal class CodingController
{
private readonly Database _database = new Database();

public void ViewSessions()
{
var table = new Table();
table.Border(TableBorder.Rounded);
table.AddColumn("[yellow]ID[/]");
table.AddColumn("[yellow]StartTime[/]");
table.AddColumn("[yellow]EndTime[/]");
table.AddColumn("[yellow]Duration[/]");
table.AddColumn("[yellow]Description[/]");

var sessions = _database.GetAll();
foreach (var session in sessions)
{
table.AddRow(
session.Id.ToString(),
session.StartTime.ToString("g"),
session.EndTime.ToString("g"),
$"{(session.EndTime - session.StartTime).TotalMinutes} mins",
session.Description ?? ""
);
}

AnsiConsole.Write(table);
AnsiConsole.MarkupLine("Press Any Key to Continue.");
Console.ReadKey();
}
public void AddSession()
{
var startTime = AnsiConsole.Ask<DateTime>("Enter the [green]start time[/] of the coding session (e.g., 2024-01-01 14:00):");
var endTime = AnsiConsole.Ask<DateTime>("Enter the [green]end time[/] of the coding session (e.g., 2024-01-01 16:00):");
var description = AnsiConsole.Ask<string>("Enter a [green]description[/] for the coding session (optional):");

var newSession = new CodingSession
{
StartTime = startTime,
EndTime = endTime,
Description = description
};

_database.Insert(newSession);
AnsiConsole.MarkupLine("[green]Coding session added successfully![/]");
AnsiConsole.MarkupLine("Press Any Key to Continue.");
Console.ReadKey();
}
public void DeleteSession()
{
var id = AnsiConsole.Ask<int>("Enter the [green]ID[/] of the session to delete:");
_database.Delete(id);
AnsiConsole.MarkupLine("[green]Session deleted successfully![/]");
AnsiConsole.MarkupLine("Press Any Key to Continue.");
Console.ReadKey();
}
}
68 changes: 68 additions & 0 deletions CodingTracker/Data/Database.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using Dapper;
using CodingTracker.Models;

namespace CodingTracker.Data;

internal class Database
{
private readonly string _connectionString;

public Database()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();

_connectionString = config.GetConnectionString("Default");
}

public SqliteConnection GetConnection()
{
return new SqliteConnection(_connectionString);
}

public void Initialize()
{
using var connection = GetConnection();

string sql = @"
CREATE TABLE IF NOT EXISTS CodingSessions (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
StartTime TEXT NOT NULL,
EndTime TEXT NOT NULL,
Description TEXT
);";

connection.Execute(sql);
}


public List<CodingSession> GetAll()
{
using var connection = GetConnection();

string sql = "SELECT * FROM CodingSessions";

return connection.Query<CodingSession>(sql).ToList();
}

public void Insert(CodingSession session)
{
using var connection = GetConnection();

string sql = @"
INSERT INTO CodingSessions (StartTime, EndTime, Description)
VALUES (@StartTime, @EndTime, @Description)";

connection.Execute(sql, session);
}
public void Delete(int id)
{
using var connection = GetConnection();
string sql = "DELETE FROM CodingSessions WHERE Id = @Id";
connection.Execute(sql, new { Id = id });
}

}
10 changes: 10 additions & 0 deletions CodingTracker/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CodingTracker
{
internal enum MenuAction
{
ViewSession,
AddSession,
DeleteSession,
Exit
}
}
21 changes: 21 additions & 0 deletions CodingTracker/Models/CodingSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace CodingTracker.Models;

internal class CodingSession
{
public int Id { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public string Description { get; set; }

public TimeSpan Duration => EndTime - StartTime;

public CodingSession() { }

public CodingSession(int id, DateTime startTime, DateTime endTime, string description)
{
Id = id;
StartTime = startTime;
EndTime = endTime;
Description = description;
}
}
10 changes: 10 additions & 0 deletions CodingTracker/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using CodingTracker;
using CodingTracker.Data;

// Initialize the database (creates the table if it doesn't exist)
var db = new Database();
db.Initialize();

// Start the user interface
UserInterface ui = new();
ui.MainMenu();
41 changes: 41 additions & 0 deletions CodingTracker/UserInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Spectre.Console;
using CodingTracker.Controllers;
using CodingTracker;

namespace CodingTracker;

internal class UserInterface
{
private readonly CodingController _controller = new CodingController();

public void MainMenu()
{
bool closeApp = false;
while (closeApp == false)
{
Console.Clear();

var actionChoice = AnsiConsole.Prompt(
new SelectionPrompt<MenuAction>()
.Title("What do you want to do next?")
.AddChoices(Enum.GetValues<MenuAction>()));

switch (actionChoice)
{
case MenuAction.ViewSession:
_controller.ViewSessions();
break;
case MenuAction.AddSession:
_controller.AddSession();
break;
case MenuAction.DeleteSession:
_controller.DeleteSession();
break;
case MenuAction.Exit:
closeApp = true;
break;

}
}
}
}
Empty file added CodingTracker/Validation.cs
Empty file.
5 changes: 5 additions & 0 deletions CodingTracker/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"Default": "Data Source=codingtracker.db"
}
}
Binary file added CodingTracker/bin/Debug/net10.0/CodingTracker
Binary file not shown.
Loading