Skip to content
Closed
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
48 changes: 48 additions & 0 deletions CodingTracker.Tests/CalendarServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

using CodingTracker.Entities;
using CodingTracker.Services;

namespace CodingTracker.Tests
{
public class CalendarServiceTests
{
[Theory]
[InlineData(1, "░ ", "darkgreen")]
[InlineData(30, "░ ", "darkgreen")]
[InlineData(31, "▒ ", "darkgreen")]
[InlineData(60, "▒ ", "darkgreen")]
[InlineData(61, "▓ ", "darkgreen")]
[InlineData(90, "▓ ", "darkgreen")]
[InlineData(91, "█ ", "darkgreen")]
[InlineData(120, "█ ", "darkgreen")]
[InlineData(121, "▒ ", "green")]
[InlineData(150, "▒ ", "green")]
[InlineData(151, "█ ", "green")]
[InlineData(160, "█ ", "green")]
public void AssignSymbolAndColorAccordingToDuration_ExpectedResult(int duration, string symbol, string color)
{
var d = new CalendarDay
{
CodingMinutes = duration
};

CalendarServiceBuilder.AssignSymbolAndColor(d);

Assert.Equal(symbol, d.ActivitySymbol);
Assert.Equal(color, d.ActivityColor);
}

[Fact]
public void AssignSymbolAndColorAccordingToDuration_DurationIsZero_ExpectedResult()
{
var d = new CalendarDay
{
CodingMinutes = 0
};

CalendarServiceBuilder.AssignSymbolAndColor(d);

Assert.Equal("gray", d.ActivityColor);
}
}
}
25 changes: 25 additions & 0 deletions CodingTracker.Tests/CodingTracker.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CodingTracker\CodingTracker.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions CodingTracker.Tests/ValidationServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CodingTracker.Services;

namespace CodingTracker.Tests
{
public class ValidationServiceTests
{

[Theory]
[InlineData(10, 11, true)]
[InlineData(10, 10, false)]
[InlineData(10, 09, false)]
public void StartSmallerThanEnd_ReturnsExpectedResult(int startHour, int endHour, bool expected)
{
var start = new DateTime(2026, 9, 18, startHour, 0, 0);
var end = new DateTime(2026, 9, 18, endHour, 0, 0);

var result = ValidationService.StartSmallerThanEnd(start, end);

Assert.Equal(expected, result);
}
}
}
4 changes: 4 additions & 0 deletions CodingTracker.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Solution>
<Project Path="CodingTracker.Tests/CodingTracker.Tests.csproj" Id="6dca2346-fd54-4724-8c7b-f4436757e225" />
<Project Path="CodingTracker/CodingTracker.csproj" />
</Solution>
63 changes: 63 additions & 0 deletions CodingTracker/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
20 changes: 20 additions & 0 deletions CodingTracker/CodingTracker.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<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.79" />
<PackageReference Include="Spectre.Console" Version="0.57.2" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


</Project>
16 changes: 16 additions & 0 deletions CodingTracker/Configuration/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingTracker.Configuration
{
public static class AppConfig
{
public static AppSettings Settings { get; private set; } = null!;

public static void Initialize(AppSettings settings)
{
Settings = settings;
}
}
}
11 changes: 11 additions & 0 deletions CodingTracker/Configuration/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingTracker.Configuration
{
public sealed class AppSettings
{
public string ConnectionString { get; init; } = string.Empty;
}
}
18 changes: 18 additions & 0 deletions CodingTracker/Configuration/ConfigurationLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;

namespace CodingTracker.Configuration
{
public static class ConfigurationLoader
{
public static AppSettings Load()
{
string json = File.ReadAllText("appsettings.json");

return JsonSerializer.Deserialize<AppSettings>(json) ?? throw new InvalidOperationException(
"Unable to load appsettings.json");
}
}
}
107 changes: 107 additions & 0 deletions CodingTracker/Controllers/CodingController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using CodingTracker.Entities;
using CodingTracker.Repositories;
using CodingTracker.Services;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingTracker.Controllers
{
public static class CodingController
{
public static void HandleNewSession(string project, DateTime startTime, DateTime endTime)
{
var codingSession = new CodingSession()
{
Project = project,
StartTime = startTime,
EndTime = endTime,
Duration = (int)endTime.Subtract(startTime).TotalMinutes
};

CalendarRepository.CreateCodingSessions(codingSession);
}

public static string EditCodingSession(CodingSession session, string project, string startTime, string endTime)
{
string result = "Success";

session.Project = project == string.Empty ? session.Project : project;

if(startTime != string.Empty)
{

if (ValidationService.TryParseDate(startTime, out DateTime newStartTime))
{
session.StartTime = newStartTime;
}
else
{
return result = "Invalid Start Time provided";
}
}

if (endTime != string.Empty)
{
if(ValidationService.TryParseDate(endTime, out DateTime newEndTime))
{
session.EndTime = newEndTime;
}
else
{
return result = "Invalid End Time Provided";
}
}

if(!ValidationService.StartSmallerThanEnd(session.StartTime, session.EndTime))
{
return result = "End time cannot be bofore start Time";
}

session.Duration = (int)session.EndTime.Subtract(session.StartTime).TotalMinutes;

CalendarRepository.UpateCodingSession(session);

return result;
}

public static IEnumerable<CodingSession> GetSessionsByYear(DateTime year, string sortOrder)
{
var start = new DateTime(year.Year, 1, 1);
var end = start.AddYears(1);

return CalendarRepository.GetCodingSessions(start, end, sortOrder);
}

private static DateTime StartOfWeek(DateTime date)
{
int difference = (7 + (date.DayOfWeek - DayOfWeek.Monday)) % 7;

return date.Date.AddDays(-difference);
}

public static IEnumerable<CodingSession> GetSessionsByWeek(DateTime date, string sortOrder)
{
var start = StartOfWeek(date);
var end = date.Date.AddDays(7);

return CalendarRepository.GetCodingSessions(start, end, sortOrder);
}

public static IEnumerable<CodingSession> GetSessionsByMonth(DateTime date, string sortOrder)
{
var start = new DateTime(date.Year, date.Month, 1);
var end = start.AddMonths(1);

return CalendarRepository.GetCodingSessions(start, end, sortOrder);
}

public static IEnumerable<CodingSession> GetSessionsByDay(DateTime date, string sortOrder)
{
var start = date.Date;
var end = start.AddDays(1);

return CalendarRepository.GetCodingSessions(start, end, sortOrder);
}
}
}
16 changes: 16 additions & 0 deletions CodingTracker/Entities/CalenderDay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingTracker.Entities
{
public class CalendarDay
{
public int Column { get; set; }
public int Row { get; set; }
public DateTime Date { get; set; }
public int CodingMinutes { get; set; }
public string ActivitySymbol { get; set; } = "░ ";
public string ActivityColor { get; set; } = "gray";
}
}
15 changes: 15 additions & 0 deletions CodingTracker/Entities/CodingSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


namespace CodingTracker.Entities
{
public class CodingSession
{
public int Id { get; set; }
public string Project { get; set; } = "";
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public int Duration { get; set; }
}


}
Loading
Loading