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
4 changes: 4 additions & 0 deletions ExerciseTracker.Call911plz/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CS8603: Possible null reference return.
dotnet_diagnostic.CS8603.severity = suggestion
27 changes: 27 additions & 0 deletions ExerciseTracker.Call911plz/Controller/ControllerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Spectre.Console;

public class ControllerBase
{
internal virtual void OnStartOfLoop(){ Console.Clear(); }
internal virtual Task<bool> HandleUserInputAsync() { return Task.FromResult(false); }

public async Task StartAsync()
{
bool exit = false;

while (exit == false)
{
try { OnStartOfLoop(); }
catch (Exception e) { AnsiConsole.MarkupLine($"[bold red]Error on Start Of Loop[/]\n{e}"); }

try { exit = await HandleUserInputAsync(); }
catch (Exception e) { AnsiConsole.MarkupLine($"[bold red]Error on Handling User Input[/]\n{e}"); }

if (exit == false)
{
AnsiConsole.MarkupLine("[bold yellow]Press Enter to continue[/]");
Console.Read();
}
}
}
}
96 changes: 96 additions & 0 deletions ExerciseTracker.Call911plz/Controller/ExerciseController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Spectre.Console;

public class ExerciseController(IService service) : ControllerBase
{
IService _service = service;

internal override void OnStartOfLoop()
{
Console.Clear();
AnsiConsole.Write
(
new FigletText("Exercise Tracker")
.LeftJustified()
.Color(Color.Red)
);
}

internal override async Task<bool> HandleUserInputAsync()
{
MenuEnums.Main input = GetMenu.MainMenu();

switch (input)
{
case MenuEnums.Main.CREATE:
await CreateAsync();
break;
case MenuEnums.Main.READ:
await ReadByIdAsync();
break;
case MenuEnums.Main.READALL:
ReadAll();
break;
case MenuEnums.Main.UPDATE:
await UpdateAsync();
break;
case MenuEnums.Main.DELETE:
await DeleteAsync();
break;
case MenuEnums.Main.EXIT:
return true;
}
return false;
}

private async Task CreateAsync()
{
Exercise exercise = GetData.GetExercise();
Exercise createdExercise = await _service.AddAsync(exercise);

AnsiConsole.MarkupLine($"[bold grey]Inserted new[/] [bold yellow]Exercise[/] [bold grey]to db:[/]");
DisplayData.DisplayExercise([createdExercise]);
}

private async Task ReadByIdAsync()
{
int id = GetData.GetId();
Exercise exercise = await _service.GetByIdAsync(id)
?? throw new Exception("[bold red]Could not find exercise with id[/]");

DisplayData.DisplayExercise([exercise]);
}

private void ReadAll()
{
List<Exercise> exercises = _service.GetAll() ?? [];

DisplayData.DisplayExercise(exercises);
}

private async Task UpdateAsync()
{
List<Exercise> exercises = _service.GetAll()
?? throw new Exception("No exercises to update");
Exercise exerciseToUpdate = GetData.GetExerciseFromList(exercises);

AnsiConsole.MarkupLine("[bold grey]Original: [/]");
DisplayData.DisplayExercise([exerciseToUpdate]);

Exercise updatedExercise = GetData.UpdateExercise(exerciseToUpdate);
Exercise updatedExerciseInDb = await _service.UpdateAsync(updatedExercise);

AnsiConsole.MarkupLine("[bold grey]Updated: [/]");
DisplayData.DisplayExercise([updatedExerciseInDb]);
}

private async Task DeleteAsync()
{
List<Exercise> exercises = _service.GetAll()
?? throw new Exception("No exercises to delete");

Exercise exerciseToDelete = GetData.GetExerciseFromList(exercises);
await _service.DeleteAsync(exerciseToDelete);

AnsiConsole.MarkupLine("[bold grey]Exercise[/] [bold red]deleted[/]");
}
}
8 changes: 8 additions & 0 deletions ExerciseTracker.Call911plz/Data/Exercise.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class Exercise
{
public int Id { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public TimeSpan Duration { get; set; }
public string Comments { get; set; } = string.Empty;
}
17 changes: 17 additions & 0 deletions ExerciseTracker.Call911plz/Data/ExerciseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;

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

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"
Server=localhost;
Database=exercisetrackerdb;
User Id=sa;
Password=StrongP@ssword1;
TrustServerCertificate=True
");
}
}

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
@@ -0,0 +1,38 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ExerciseTracker.Call911plz.Data.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Exercises",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Start = table.Column<DateTime>(type: "datetime2", nullable: false),
End = table.Column<DateTime>(type: "datetime2", nullable: false),
Duration = table.Column<TimeSpan>(type: "time", nullable: false),
Comments = table.Column<string>(type: "nvarchar(max)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Exercises", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Exercises");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace ExerciseTracker.Call911plz.Data.Migrations
{
[DbContext(typeof(ExerciseContext))]
partial class ExerciseContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

modelBuilder.Entity("Exercise", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<string>("Comments")
.IsRequired()
.HasColumnType("nvarchar(max)");

b.Property<TimeSpan>("Duration")
.HasColumnType("time");

b.Property<DateTime>("End")
.HasColumnType("datetime2");

b.Property<DateTime>("Start")
.HasColumnType("datetime2");

b.HasKey("Id");

b.ToTable("Exercises");
});
#pragma warning restore 612, 618
}
}
}
4 changes: 4 additions & 0 deletions ExerciseTracker.Call911plz/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class MenuEnums
{
public enum Main { CREATE, READ, READALL, UPDATE, DELETE, EXIT }
}
20 changes: 20 additions & 0 deletions ExerciseTracker.Call911plz/ExerciseTracker.Call911plz.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>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.4" />
<PackageReference Include="Spectre.Console" Version="0.50.0" />
</ItemGroup>

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

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExerciseTracker.Call911plz", "ExerciseTracker.Call911plz.csproj", "{2EC88879-AAFF-4268-8602-393DD1E114AB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Debug|x64.ActiveCfg = Debug|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Debug|x64.Build.0 = Debug|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Debug|x86.ActiveCfg = Debug|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Debug|x86.Build.0 = Debug|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Release|Any CPU.Build.0 = Release|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Release|x64.ActiveCfg = Release|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Release|x64.Build.0 = Release|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Release|x86.ActiveCfg = Release|Any CPU
{2EC88879-AAFF-4268-8602-393DD1E114AB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Loading