-
Notifications
You must be signed in to change notification settings - Fork 71
Program completes #68
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
anaclos
wants to merge
4
commits into
TheCSharpAcademy:main
Choose a base branch
from
anaclos:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.9.34728.123 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExerciseTracker", "ExerciseTracker\ExerciseTracker.csproj", "{D9168DBD-6026-4631-A708-5E61E31D06F8}" | ||
| EndProject | ||
| Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Elementos de la solución", "Elementos de la solución", "{34D32A39-5006-4278-AC54-C6B97F7F5C31}" | ||
| ProjectSection(SolutionItems) = preProject | ||
| Readme.txt = Readme.txt | ||
| EndProjectSection | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {D9168DBD-6026-4631-A708-5E61E31D06F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {D9168DBD-6026-4631-A708-5E61E31D06F8}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {D9168DBD-6026-4631-A708-5E61E31D06F8}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {D9168DBD-6026-4631-A708-5E61E31D06F8}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {90F22CA4-8D08-4E1D-A45C-0C4ACCC61F8D} | ||
| EndGlobalSection | ||
| EndGlobal |
294 changes: 294 additions & 0 deletions
294
ExerciseTracker.AnaClos/ExerciseTracker/Controllers/ExerciseController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,294 @@ | ||
| using ExerciseTracker.DTO; | ||
| using ExerciseTracker.Models; | ||
| using ExerciseTracker.Services; | ||
| using ExerciseTracker.UI; | ||
| using ExerciseTracker.Validators; | ||
|
|
||
| namespace ExerciseTracker.Controllers; | ||
|
|
||
| public class ExerciseController : IController | ||
| { | ||
| private List<string> mainOptions = new List<string> { "Add", "Delete", "Update", "View", "Exit Menu" }; | ||
| private UserInput _userInput; | ||
| private IService _exerciseService; | ||
| public ExerciseController(UserInput userInput, IService exerciseService) | ||
| { | ||
| _userInput = userInput; | ||
| _exerciseService = exerciseService; | ||
| } | ||
|
|
||
| public void Menu() | ||
| { | ||
| string option; | ||
| do | ||
| { | ||
| option = _userInput.Menu("Manage Exercises", "blue", mainOptions); | ||
| switch (option) | ||
| { | ||
| case "Add": | ||
| Add(); | ||
| break; | ||
| case "Delete": | ||
| Delete(); | ||
| break; | ||
| case "Update": | ||
| Update(); | ||
| break; | ||
| case "View": | ||
| View(); | ||
| break; | ||
| case "View All": | ||
| ViewAll(); | ||
| break; | ||
| } | ||
| } while (option != "Exit Menu"); | ||
| } | ||
|
|
||
| public void Add() | ||
| { | ||
| string startString; | ||
| string endString; | ||
| DateTime startTime; | ||
| DateTime endTime; | ||
|
|
||
| _userInput.ShowMessage("The new shift must be after the last shift interval. The shift interval must not exceed 8 hours. ", "blue"); | ||
|
|
||
| do | ||
| { | ||
| startString = _userInput.GetString("Start Time in format yy/MM/dd HH:mm:ss"); | ||
| startTime = ExerciseValidator.DateTimeValidator(startString); | ||
| } while (startTime == DateTime.MinValue); | ||
|
|
||
| do | ||
| { | ||
| endString = _userInput.GetString("End Time in format yy/MM/dd HH:mm:ss"); | ||
| endTime = ExerciseValidator.DateTimeValidator(endString); | ||
| } while (endTime == DateTime.MinValue); | ||
|
|
||
| if (!ExerciseValidator.IntervalValidator(startTime, endTime)) | ||
| { | ||
| _userInput.MessageAndPressKey("The shift interval does not meet the requested requirements.", "red"); | ||
| return; | ||
| } | ||
|
|
||
| var last10Exercises = _exerciseService.GetLast10(); | ||
| var lastExercise = last10Exercises.FirstOrDefault(); | ||
|
|
||
|
|
||
| if (lastExercise != null && !ExerciseValidator.OrderValidator(lastExercise, startTime)) | ||
| { | ||
| _userInput.MessageAndPressKey("The time slot is already used", "red"); | ||
| return; | ||
| } | ||
|
|
||
| string comments = _userInput.GetString("Input a comment."); | ||
|
|
||
| Exercise exercise = new Exercise | ||
| { | ||
| Id = 0, | ||
| DateStart = startTime, | ||
| DateEnd = endTime, | ||
| Duration = endTime - startTime, | ||
| Comments = comments | ||
| }; | ||
| try | ||
| { | ||
| _exerciseService.Add(exercise); | ||
| _userInput.MessageAndPressKey("Successfully created exercise.", "orange1"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _userInput.MessageAndPressKey("The exercise could not be created", "orange1"); | ||
| } | ||
| } | ||
|
|
||
| public void Delete() | ||
| { | ||
| bool confirmation = _userInput.Choice("You can only delete the last record, continue?"); | ||
| if (!confirmation) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var exercises = _exerciseService.GetLast10(); | ||
| Exercise exercise = exercises.FirstOrDefault(); | ||
| if (exercise == null) | ||
| { | ||
| _userInput.MessageAndPressKey("There is no exercise to delete", "red"); | ||
| return; | ||
| } | ||
| try | ||
| { | ||
| _exerciseService.Delete(exercise); | ||
| _userInput.MessageAndPressKey("Successfully deleted exercise.", "orange1"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _userInput.MessageAndPressKey("The exercise couldn't be deleted", "red"); | ||
| } | ||
| } | ||
|
|
||
| public void Update() | ||
| { | ||
| string startString; | ||
| DateTime startTime; | ||
| string endString; | ||
| DateTime endTime; | ||
| bool confirmation = _userInput.Choice("You can only update the last record, continue?"); | ||
| if (!confirmation) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var exercises = _exerciseService.GetLast10().ToList(); | ||
| Exercise exercise = exercises.FirstOrDefault(); | ||
| if (exercise == null) | ||
| { | ||
| _userInput.MessageAndPressKey("There is no exercise to update", "red"); | ||
| return; | ||
| } | ||
|
|
||
| do | ||
| { | ||
| startString = _userInput.GetString("Start Time in format yy/MM/dd HH:mm:ss"); | ||
| startTime = ExerciseValidator.DateTimeValidator(startString); | ||
| } while (startTime == DateTime.MinValue); | ||
|
|
||
| do | ||
| { | ||
| endString = _userInput.GetString("End Time in format yy/MM/dd HH:mm:ss"); | ||
| endTime = ExerciseValidator.DateTimeValidator(endString); | ||
| } while (endTime == DateTime.MinValue); | ||
|
|
||
| if (!ExerciseValidator.IntervalValidator(startTime, endTime)) | ||
| { | ||
| _userInput.MessageAndPressKey("The shift interval does not meet the requested requirements.", "red"); | ||
| return; | ||
| } | ||
|
|
||
| exercises.RemoveAt(0); | ||
| if (!ExerciseValidator.OrderValidator(exercises.FirstOrDefault(), startTime)) | ||
| { | ||
| _userInput.MessageAndPressKey("The time slot is already used", "red"); | ||
| } | ||
|
|
||
| string comments = _userInput.GetString("Input a comment."); | ||
| exercise.DateStart = startTime; | ||
| exercise.DateEnd = endTime; | ||
| exercise.CalculateDuration(); | ||
| exercise.Comments = comments; | ||
| try | ||
| { | ||
| _exerciseService.Update(exercise); | ||
| _userInput.MessageAndPressKey("Successfully updated exercise.", "orange1"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _userInput.MessageAndPressKey("The exercise couldn't be updated", "red"); | ||
| } | ||
| } | ||
|
|
||
| public void View() | ||
| { | ||
| int order; | ||
|
|
||
| var exercises = _exerciseService.GetLast10().ToList(); | ||
| if (exercises.Count == 0) | ||
| { | ||
| _userInput.MessageAndPressKey("There is no exercise to select ", "red"); | ||
| return; | ||
| } | ||
|
|
||
| var orderedExercises = exercises.OrderBy(sh => sh.Id).ToList(); | ||
| List<string> stringExercises = ExerciseToString(orderedExercises); | ||
| string stringExercise = GetExerciseFromMenu("Select a exercise to view details", stringExercises); | ||
| if (stringExercise == "Exit Menu") | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| int.TryParse(stringExercise.Split('#')[1], out order); | ||
|
|
||
| string[] columns = { "Property", "Value" }; | ||
|
|
||
| var exercise = orderedExercises.ElementAt(order - 1); | ||
|
|
||
| var recordExercise = ExerciseToProperties(exercise); | ||
|
|
||
| _userInput.ShowTable("Exercise", columns, recordExercise); | ||
| _userInput.PressKey("Press a key to continue."); | ||
| } | ||
|
|
||
| public void ViewAll() | ||
| { | ||
| var exercises = _exerciseService.GetLast10().ToList(); | ||
| if (exercises.Count == 0) | ||
| { | ||
| _userInput.MessageAndPressKey("There is no exercises to view ", "red"); | ||
| return; | ||
| } | ||
|
|
||
| foreach (Exercise exercise in exercises) | ||
| { | ||
|
|
||
| exercise.CalculateDuration(); | ||
| _userInput.ShowMessage($"Exercise Date: {exercise.DateStart.Year}/{exercise.DateStart.Month}/{exercise.DateStart.Day} Duration: {exercise.Duration.Hours} hours, {exercise.Duration.Minutes} minutes, {exercise.Duration.Seconds} seconds", "green"); | ||
| } | ||
|
|
||
| _userInput.PressKey("Press a key to continue."); | ||
| } | ||
| public List<string> ExerciseToString(List<Exercise> exercises) | ||
| { | ||
| var tableRecord = new List<string>(); | ||
|
|
||
| for (int i = 1; i <= exercises.Count; i++) | ||
| { | ||
| tableRecord.Add($"Exercise #{i}"); | ||
| } | ||
| return tableRecord; | ||
| } | ||
|
|
||
| public int GetOrderFromList(List<string> stringShifts, string stringShift) | ||
| { | ||
| int order = 0; | ||
| for (int i = 0; i < stringShifts.Count; i++) | ||
| { | ||
| if (stringShifts[i] == stringShift) | ||
| { | ||
| order = i; | ||
| } | ||
| } | ||
| return order; | ||
| } | ||
|
|
||
| public string GetExerciseFromMenu(string title, List<string> stringShifts) | ||
| { | ||
| stringShifts.Add("Exit Menu"); | ||
|
|
||
| string stringExercise = _userInput.Menu(title, "blue", stringShifts); | ||
| if (stringExercise == "Exit Menu") | ||
| { | ||
| return null; | ||
| } | ||
| return stringExercise; | ||
| } | ||
|
|
||
| public List<RecordDto> ExerciseToProperties(Exercise exercise) | ||
| { | ||
| var tableRecord = new List<RecordDto>(); | ||
| RecordDto record = null; | ||
| foreach (var property in exercise.GetType().GetProperties()) | ||
| { | ||
| if (property.GetValue(exercise) != null) | ||
| { | ||
| string value = ""; | ||
| value = property.GetValue(exercise).ToString(); | ||
|
|
||
| record = new RecordDto { Column1 = property.Name, Column2 = value }; | ||
| tableRecord.Add(record); | ||
| } | ||
| } | ||
|
|
||
| return tableRecord; | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
ExerciseTracker.AnaClos/ExerciseTracker/Controllers/IController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace ExerciseTracker.Controllers; | ||
|
|
||
| public interface IController | ||
| { | ||
| public void Menu(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace ExerciseTracker.DTO; | ||
|
|
||
| public class RecordDto | ||
| { | ||
| public string Column1 { get; set; } | ||
| public string Column2 { get; set; } | ||
| } |
19 changes: 19 additions & 0 deletions
19
ExerciseTracker.AnaClos/ExerciseTracker/Data/ExerciseDbContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using ExerciseTracker.Models; | ||
|
|
||
| namespace ExerciseTracker.Data; | ||
|
|
||
| public class ExerciseDbContext : DbContext | ||
| { | ||
| public string _connectionString; | ||
| public ExerciseDbContext(DbContextOptions<ExerciseDbContext> options) : base(options) | ||
| { | ||
| } | ||
|
|
||
| public DbSet<Exercise> Exercises { get; set; } | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| modelBuilder.Entity<Exercise>().ToTable("Exercise"); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
ExerciseTracker.AnaClos/ExerciseTracker/ExerciseTracker.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.1" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.1"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1" /> | ||
| <PackageReference Include="Spectre.Console" Version="0.49.1" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Update="appsettings.Development.json"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| <None Update="appsettings.json"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 Shift
❓ Shift?