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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private async Task TInsert(Exercise exercise)
Type = exercise.Type
});
Console.Error.WriteLine(affectedRows);
}
}

public void Update(Exercise exercise)
{
Expand Down
118 changes: 118 additions & 0 deletions ExerciseTracker.TwilightSaw/Controller/ExerciseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using ExerciseTracker.TwilightSaw.Helper;
using ExerciseTracker.TwilightSaw.Model;
using ExerciseTracker.TwilightSaw.Service;
using Spectre.Console;

namespace ExerciseTracker.TwilightSaw.Controller;

public class ExerciseController(ExerciseService service)
{
public void AddExercise(string type)
{
Console.Clear();
AnsiConsole.Write(new Rule("[cyan]Format - hh:mm[/]"));
var addStartInput = UserInput.CreateRegex("^(?:([0-1][0-9]|2[0-3]):([0-5][0-9])|0)$",
"Insert the start of your Exercise", "Wrong format, try again.");
if (addStartInput == "0") return;
AnsiConsole.Write(new Rule("[cyan]Format - hh:mm[/]"));
var addEndInput = UserInput.CreateRegex("^(?:([0-1][0-9]|2[0-3]):([0-5][0-9])|0|(N|n))$",
"Insert the end of your Exercise, N for time at this moment", "Wrong format.");
addEndInput = addEndInput is "N" or "n"
? DateTime.Now.AddSeconds(-DateTime.Now.Second).ToShortTimeString()
: addEndInput;
if (addEndInput == "0") return;
var addComments = UserInput.Create("Add the comments, leave this field empty");
if (addComments == "0") return;

DateTime.TryParse(addStartInput, out var startTime);
DateTime.TryParse(addEndInput, out var endTime);
var exercise = new Exercise(type, startTime, endTime, addComments == "" ? null : addComments);
service.AddExercise(exercise);
Validation.EndMessage("Exercise added successfully.");
}

public Exercise GetExercise(string type)
{
Console.Clear();
var input = UserInput.CreateExerciseChoosingList(service.GetExerciseByType(type), "Return");
return input;
}

public void DeleteExercise(Exercise exercise)
{
Console.Clear();
service.DeleteExercise(exercise.Id);
Validation.EndMessage("Exercise deleted successfully.");
}

public void ChangeExercise(Exercise exercise)
{
Console.Clear();
var type = exercise.Type;
var stringDate = exercise.StartTime.ToShortDateString();
var stringStartTime = exercise.StartTime.TimeOfDay.ToString();
var stringEndTime = exercise.EndTime.TimeOfDay.ToString();
var comment = exercise.Comments;

var changeInput = UserInput.CreateUpdateChoosingList([
$"Type: {type}",
$"Date: {stringDate}", $"Start Time: {stringStartTime}",
$"End Time: {stringEndTime}", $"Comment: {comment}"
],
exercise, "Return");
switch (changeInput)
{
case "1":
var typeInput = UserInput.CreateChoosingList(["Cardio", "Weights"], "Return");
if (typeInput == "Return") return;
exercise.Type = typeInput;
break;
case "2":
AnsiConsole.Write(new Rule("[olive]Format: dd.mm.yyyy[/]"));
var newDateInput = UserInput.CreateRegex(@"^(?:([0-2][0-9]|3[01])\.(0[1-9]|1[0-2])\.(\d{4})|(T|t)|0)$",
"Insert your new date, T for today's date", "Wrong format, try again.");
switch (newDateInput)
{
case "0":
return;
case "T" or "t":
newDateInput = DateTime.Now.ToShortDateString();
break;
}

DateTime.TryParse(newDateInput, out var newDate);
exercise.StartTime = newDate + exercise.StartTime.TimeOfDay;
exercise.EndTime = newDate + exercise.EndTime.TimeOfDay;
break;
case "3":
AnsiConsole.Write(new Rule("[olive]Format: hh:mm[/]"));
var newStartTimeInput = UserInput.CreateRegex("^(?:([0-1][0-9]|2[0-3]):([0-5][0-9])|0)$",
"Insert the start of your Exercise", "Wrong format, try again.");
if (newStartTimeInput == "0") return;

DateTime.TryParse(newStartTimeInput, out var newStartTime);
exercise.StartTime = exercise.StartTime.Date + newStartTime.TimeOfDay;
break;
case "4":
AnsiConsole.Write(new Rule("[olive]Format: hh:mm[/]"));
var newEndTimeInput = UserInput.CreateRegex("^(?:([0-1][0-9]|2[0-3]):([0-5][0-9])|0|(N|n))$",
"Insert the end of your Exercise, N for time at this moment", "Wrong format.");
if (newEndTimeInput == "0") return;
newEndTimeInput = newEndTimeInput is "N" or "n"
? DateTime.Now.AddSeconds(-DateTime.Now.Second).ToShortTimeString()
: newEndTimeInput;

DateTime.TryParse(newEndTimeInput, out var newEndTime);
exercise.EndTime = exercise.EndTime.Date + newEndTime.TimeOfDay;
break;
case "5":
var newComment = UserInput.Create("Change your comment, leave this field empty");
if (newComment == "0") return;
exercise.Comments = newComment;
break;
}

service.UpdateExercise(exercise);
Validation.EndMessage("Changed successfully.");
}
}
22 changes: 22 additions & 0 deletions ExerciseTracker.TwilightSaw/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using ExerciseTracker.TwilightSaw.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace ExerciseTracker.TwilightSaw.Data;

public class AppDbContext : DbContext
{
public DbSet<Exercise> Exercises { get; set; }

private readonly IConfiguration _configuration;
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Exercise>();
}
}
37 changes: 37 additions & 0 deletions ExerciseTracker.TwilightSaw/ExerciseTracker.TwilightSaw.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<Folder Include="Factory\" />
<Folder Include="images\" />
<Folder Include="View\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.0" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
</ItemGroup>

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

</Project>
25 changes: 25 additions & 0 deletions ExerciseTracker.TwilightSaw/ExerciseTracker.TwilightSaw.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExerciseTracker.TwilightSaw", "ExerciseTracker.TwilightSaw.csproj", "{792D3445-2C8A-43F8-A586-03388D66D249}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{792D3445-2C8A-43F8-A586-03388D66D249}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{792D3445-2C8A-43F8-A586-03388D66D249}.Debug|Any CPU.Build.0 = Debug|Any CPU
{792D3445-2C8A-43F8-A586-03388D66D249}.Release|Any CPU.ActiveCfg = Release|Any CPU
{792D3445-2C8A-43F8-A586-03388D66D249}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C26453EB-0465-4FF9-A21B-737455131161}
EndGlobalSection
EndGlobal
24 changes: 24 additions & 0 deletions ExerciseTracker.TwilightSaw/Factory/HostFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using ExerciseTracker.TwilightSaw.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ExerciseTracker.TwilightSaw.Factory;

public class HostFactory
{
public static IHost CreateDbHost(string[] args)
{
var builder = Host.CreateDefaultBuilder(args);
var configuration = builder.ConfigureServices((context, services) =>
{
var configuration = context.Configuration;
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"))
.LogTo(Console.WriteLine, LogLevel.None)
.UseLazyLoadingProxies());
});
return configuration.Build();
}
}
77 changes: 77 additions & 0 deletions ExerciseTracker.TwilightSaw/Helper/UserInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Text.RegularExpressions;
using ExerciseTracker.TwilightSaw.Model;
using Spectre.Console;

namespace ExerciseTracker.TwilightSaw.Helper;

public class UserInput
{

public static string CreateRegex(string regexString, string messageStart, string messageError)
{
var regex = new Regex(regexString);
var input = AnsiConsole.Prompt(
new TextPrompt<string>($"[green]{messageStart} or 0 to exit:[/]")
.Validate(value => regex.IsMatch(value)
? ValidationResult.Success()
: ValidationResult.Error($"[red]{messageError}[/]")));
Console.Clear();
return input;
}

public static string Create(string messageStart)
{
var input = AnsiConsole.Prompt(
new TextPrompt<string>($"[green]{messageStart} or 0 to exit: [/]")
.AllowEmpty());
Console.Clear();
return input;
}

public static string CreateChoosingList(List<string> variants, string backVariant)
{
variants.Add(backVariant);
return AnsiConsole.Prompt(new SelectionPrompt<string>()
.Title("[blue]Please, choose an option from the list below:[/]")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to reveal more categories[/]")
.AddChoices(variants));
}

public static Exercise CreateExerciseChoosingList(List<Exercise> variants, string? backVariant)
{
variants.Add(new Exercise("",DateTime.Now, DateTime.Now, backVariant));
return AnsiConsole.Prompt(new SelectionPrompt<Exercise>()
.Title("[blue]Please, choose an option from the list below:[/]")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to reveal more categories[/]")
.UseConverter(
exercise => exercise.Comments != backVariant ? $"Date: {exercise.StartTime.ToShortDateString()}\n " +
$" Start Time: {exercise.StartTime.TimeOfDay}, " +
$"End Time: {exercise.EndTime.TimeOfDay}, " +
$"Duration: {exercise.Duration}\n" +
$" Comments: {exercise.Comments}" :
"[red]Return[/]"
)
.AddChoices(variants));
}

public static string CreateUpdateChoosingList(List<string> variants, Exercise exercise, string backVariant)
{
var var = AnsiConsole.Prompt(new SelectionPrompt<string>()
.Title("[blue]Please, choose an option from the list below:[/]")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to reveal more categories[/]")
.AddChoices(variants));

var selectedIndex = variants.IndexOf(var);
return selectedIndex switch
{
0 => "1",
1 => "2",
2 => "3",
3 => "4",
_ => "5"
};
}
}
Loading