Skip to content

review #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions CodingTracker/CodingTracker.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodingTracker", "CodingTracker\CodingTracker.csproj", "{03120830-35AE-49F7-AC34-1EDC2B3F8ACF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{03120830-35AE-49F7-AC34-1EDC2B3F8ACF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03120830-35AE-49F7-AC34-1EDC2B3F8ACF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03120830-35AE-49F7-AC34-1EDC2B3F8ACF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03120830-35AE-49F7-AC34-1EDC2B3F8ACF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions CodingTracker/CodingTracker/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Connectionstring" value="Data Source=CodingTracker.db"/>
<add key="DatabasePath" value="CodingTracker.db"/>
</appSettings>
</configuration>
17 changes: 17 additions & 0 deletions CodingTracker/CodingTracker/CodingTracker.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="9.0.1" />
</ItemGroup>

</Project>
Binary file added CodingTracker/CodingTracker/CodingTracker.db
Binary file not shown.
101 changes: 101 additions & 0 deletions CodingTracker/CodingTracker/Controllers/CodingSessionController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using CodingTracker.Models;
using Dapper;
using Microsoft.Data.Sqlite;
using System.Configuration;

namespace CodingTracker.Controllers;

internal class CodingSessionController
{
protected readonly string _connectionString = ConfigurationManager.AppSettings.Get("Connectionstring") ?? throw new Exception("Key value pair doesn't exist in the config-file!");

public List<CodingSession> GetAll()
{
using (var connection = new SqliteConnection(_connectionString))
{
string query = "SELECT * FROM CodingSession";

connection.Open();

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

public CodingSession GetById(int id)
{
using (var connection = new SqliteConnection(_connectionString))
{
string query = "SELECT * FROM CodingSession WHERE Id = @Id";

connection.Open();

return connection.Query<CodingSession>(query, new { Id = id }).First();
}
}

/// <summary>
/// Returns the number of rows affected
/// </summary>
/// <param name="codingSession"></param>
/// <returns></returns>
public int Add(CodingSession codingSession)
{
using (var connection = new SqliteConnection(_connectionString))
{
string sql = "INSERT INTO CodingSession (StartTime, EndTime, Duration) VALUES (@StartTime, @EndTime, @Duration)";

connection.Open();

return connection.Execute(sql, codingSession);
}
}

/// <summary>
/// Returns the number of rows affected
/// </summary>
/// <param name="codingSession"></param>
/// <returns></returns>
public int Update(CodingSession codingSession)
{
using (var connection = new SqliteConnection(_connectionString))
{
string sql = "UPDATE CodingSession SET StartTime = @StartTime, EndTime = @EndTime, Duration = @Duration WHERE id = @Id";

connection.Open();

return connection.Execute(sql, codingSession);
}
}

/// <summary>
/// Returns the number of rows affected
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public int Delete(int id)
{
using (var connection = new SqliteConnection(_connectionString))
{
string sql = "DELETE FROM CodingSession WHERE id = @Id";

connection.Open();

return connection.Execute(sql, new { Id = id});
}
}

internal bool Exists(int id)
{
using (var connection = new SqliteConnection(_connectionString))
{
string sql = "SELECT * FROM CodingSession WHERE id = @Id";

connection.Open();

var reader = connection.ExecuteScalar(sql, new { Id = id });

return reader != null;
}

}
}
35 changes: 35 additions & 0 deletions CodingTracker/CodingTracker/Database.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.Data.Sqlite;
using System.Configuration;

namespace CodingTracker;

internal static class Database
{
private static readonly string _connectionString = ConfigurationManager.AppSettings.Get("Connectionstring") ??
throw new Exception("Key value pair doesn't exist in the config-file!");

private static readonly string _databasePath = ConfigurationManager.AppSettings.Get("DatabasePath") ??
throw new Exception("Key value pair doesn't exist in the config-file!");
internal static void CreateDatabase()
{
if (!File.Exists(_databasePath))
{
using (SqliteConnection connection = new SqliteConnection(_connectionString))
{
connection.Open();

var command = connection.CreateCommand();
command.CommandText =
@"CREATE TABLE CodingSession
(
Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
StartTime TEXT NOT NULL,
EndTime TEXT NOT NULL,
Duration INTEGER NOT NULL
)";

command.ExecuteNonQuery();
}
}
}
}
11 changes: 11 additions & 0 deletions CodingTracker/CodingTracker/Enums/MenuItems.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CodingTracker.Enums;

internal enum MenuItems
{
StartCodingSession,
ViewAll,
Add,
Update,
Delete,
Quit
}
9 changes: 9 additions & 0 deletions CodingTracker/CodingTracker/Models/CodingSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CodingTracker.Models;

internal class CodingSession
{
public int Id { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public double Duration { get; set; }
}
7 changes: 7 additions & 0 deletions CodingTracker/CodingTracker/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using CodingTracker;
using CodingTracker.UserInterface;

Database.CreateDatabase();

UserMenu userMenu = new UserMenu();
userMenu.ShowMenu();
8 changes: 8 additions & 0 deletions CodingTracker/CodingTracker/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"CodingTracker": {
"commandName": "Project",
"workingDirectory": "C:\\Users\\gilles.lagrilliere\\OneDrive - Codraft\\Documents\\C#-projects\\CodeReviews.Console.CodingTracker\\CodingTracker\\CodingTracker"
}
}
}
159 changes: 159 additions & 0 deletions CodingTracker/CodingTracker/UserInterface/UserInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using CodingTracker.Controllers;
using CodingTracker.Models;
using Spectre.Console;
using System.Diagnostics;

namespace CodingTracker.UserInterface;

internal static class UserInput
{
private static readonly CodingSessionController _controller = new CodingSessionController();
public static void ViewAll()
{
var AllCodingSessions = _controller.GetAll();

PrintCodingSessions(AllCodingSessions);

Console.WriteLine("Press any key to continue");
Console.ReadKey();
}

public static void Add()
{
var session = AskForInput();

_controller.Add(session);
}

public static void Update()
{
var AllCodingSessions = _controller.GetAll();

PrintCodingSessions(AllCodingSessions);

int id;
do
{
id = AnsiConsole.Ask<int>("Choose the [yellow]Id[/] of a session to update.");
}
while (!_controller.Exists(id));

var session = AskForInput();
session.Id = id;

_controller.Update(session);
}

public static void Delete()
{
var AllCodingSessions = _controller.GetAll();

PrintCodingSessions(AllCodingSessions);
int id;

do
{
id = AnsiConsole.Ask<int>("Choose a valid [yellow]Id[/] of a session to delete.");
}
while (!_controller.Exists(id));

_controller.Delete(id);
}

public static void StartCodingSession()
{
Console.WriteLine("press any key to start the session");
Console.ReadKey(true);

Stopwatch stopwatch = Stopwatch.StartNew();
stopwatch.Start();

var startTime = DateTime.Now;
startTime = new DateTime(
startTime.Ticks - (startTime.Ticks % TimeSpan.TicksPerSecond),
startTime.Kind
);

Console.WriteLine("press enter to stop the session");

ConsoleKeyInfo input;
do
{
input = Console.ReadKey();
}
while (input.Key is not ConsoleKey.Enter);

stopwatch.Stop();
var endTime = DateTime.Now;
endTime = new DateTime(
endTime.Ticks - (endTime.Ticks % TimeSpan.TicksPerSecond),
endTime.Kind
);

var duration = new TimeSpan(stopwatch.Elapsed.Ticks / TimeSpan.TicksPerSecond * TimeSpan.TicksPerSecond); //truncate the milliseconds

string elapsedTime = $"{duration.Hours:00}:{duration.Minutes:00}:{duration.Seconds:00}";
Console.WriteLine("Codingtime: " + elapsedTime);
Console.ReadKey();

var session = new CodingSession()
{
StartTime = startTime,
EndTime = endTime,
Duration = duration.TotalSeconds
};

_controller.Add(session);
}

private static string ConvertDateToString(DateTime date)
{
return date.ToString("dd-MM-yyyy HH:mm");
}

private static string ConvertTimeSpanToString(double duration)
{
return TimeSpan.FromSeconds(duration).ToString();
}

private static void PrintCodingSessions(List<CodingSession> sessions)
{
var table = new Table();
table.AddColumns("Id", "StartTime", "EndTime", "Duration");

foreach (var session in sessions)
{
var startTime = ConvertDateToString(session.StartTime);
var endTime = ConvertDateToString(session.EndTime);
var duration = ConvertTimeSpanToString(session.Duration);

table.AddRow([session.Id.ToString(), startTime, endTime, duration]);
}

AnsiConsole.Write(table);
}

private static CodingSession AskForInput()
{
var startTime = AnsiConsole.Ask<DateTime>("What time did you start(mm-dd-yyyy hh:mm): ");
var endTime = AnsiConsole.Ask<DateTime>("What time did you end(mm-dd-yyyy hh:mm): ");
while (DateTime.Compare(startTime, endTime) > 0)
{
Console.WriteLine("EndTime can not be before the starttime.");
endTime = AnsiConsole.Ask<DateTime>("What time did you end(mm-dd-yyyy hh:mm): ");
}

var duration = endTime - startTime;

CodingSession session = new CodingSession()
{
StartTime = startTime,
EndTime = endTime,
Duration = duration.TotalSeconds
};

return session;
}


}
Loading