diff --git a/CodeReviews.Console.CodingTracker.sln b/CodeReviews.Console.CodingTracker.sln new file mode 100644 index 00000000..abe62f9e --- /dev/null +++ b/CodeReviews.Console.CodingTracker.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "coding-tracker", "coding-tracker\coding-tracker.csproj", "{6EC1B504-2ADB-4BA0-8C85-77A0C2885808}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6EC1B504-2ADB-4BA0-8C85-77A0C2885808}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6EC1B504-2ADB-4BA0-8C85-77A0C2885808}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6EC1B504-2ADB-4BA0-8C85-77A0C2885808}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6EC1B504-2ADB-4BA0-8C85-77A0C2885808}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FCAF4BFE-3CA6-4BE3-83A6-72AEE3C5BA19} + EndGlobalSection +EndGlobal diff --git a/coding-tracker/Coding.cs b/coding-tracker/Coding.cs new file mode 100644 index 00000000..35d90ef6 --- /dev/null +++ b/coding-tracker/Coding.cs @@ -0,0 +1,6 @@ +internal class Coding +{ + public int Id { get; set; } + public string Date { get; set; } + public string Duration { get; set; } +} \ No newline at end of file diff --git a/coding-tracker/CodingController.cs b/coding-tracker/CodingController.cs new file mode 100644 index 00000000..d7ac53f2 --- /dev/null +++ b/coding-tracker/CodingController.cs @@ -0,0 +1,111 @@ +using Microsoft.Data.Sqlite; +internal class CodingController +{ + string connectionString = System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString"); + + + internal void Post (Coding coding) + { + using (var connection = new SqliteConnection(connectionString)) + { + using (var tableCmd = connection.CreateCommand()) + { + connection.Open(); + tableCmd.CommandText = $"INSERT INTO coding (date, duration) VALUES ('{coding.Date}', '{coding.Duration}')"; + tableCmd.ExecuteNonQuery(); + } + } + } + + internal void Get() + { + List tableData = new List(); + using (var connection = new SqliteConnection(connectionString)) + { + using (var tableCmd = connection.CreateCommand()) + { + connection.Open(); + tableCmd.CommandText = $"SELECT * from coding"; + + using (var reader = tableCmd.ExecuteReader()) + { + if (reader.HasRows) + { + while (reader.Read()) + { + tableData.Add(new Coding{ + Id = reader.GetInt32(0), + Date = reader.GetString(1), + Duration = reader.GetString(2), + }); + + } + } + else{ + System.Console.WriteLine("\n\nNo rows found\n\n"); + } + } + } + + TableVisualization.ShowTable(tableData); + } + } + + internal Coding GetById(int id) + { + using (var connection = new SqliteConnection(connectionString)) + { + using (var tableCmd = connection.CreateCommand()) + { + connection.Open(); + tableCmd.CommandText = $"SELECT * from coding WHERE Id = '{id}'"; + + using (var reader = tableCmd.ExecuteReader()) + { + Coding coding = new Coding(); + if (reader.HasRows) + { + reader.Read(); + coding.Id = reader.GetInt32(0); + coding.Date = reader.GetString(1); + coding.Duration = reader.GetString(2); + } + else{ + System.Console.WriteLine("\n\nNo rows found\n\n"); + } + + return coding; + } + } + } + + + } + + internal void Delete(int id) + { + using (var connection = new SqliteConnection(connectionString)) + { + using (var tableCmd = connection.CreateCommand()) + { + connection.Open(); + tableCmd.CommandText = $"DELETE from coding WHERE Id = '{id}'"; + tableCmd.ExecuteNonQuery(); + } + } + } + + internal void Update(Coding coding) + { + using (var connection = new SqliteConnection(connectionString)) + { + using (var tableCmd = connection.CreateCommand()) + { + connection.Open(); + tableCmd.CommandText = $"UPDATE coding SET Date = '{coding.Date}', Duration = '{coding.Duration}' WHERE Id = '{coding.Id}'"; + tableCmd.ExecuteNonQuery(); + } + } + } + +} \ No newline at end of file diff --git a/coding-tracker/DatabaseManager.cs b/coding-tracker/DatabaseManager.cs new file mode 100644 index 00000000..562ecd9e --- /dev/null +++ b/coding-tracker/DatabaseManager.cs @@ -0,0 +1,24 @@ +using Microsoft.Data.Sqlite; + +internal class DatabaseManager + { + internal void CreateTable(string connectionString) + { + using (var connection = new SqliteConnection(connectionString)) + { + using (var tableCmd = connection.CreateCommand()) + { + + connection.Open(); + + tableCmd.CommandText = + @"CREATE TABLE IF NOT EXISTS coding ( + Id INTEGER PRIMARY KEY AUTOINCREMENT, + Date TEXT, + Duration TEXT)"; + + tableCmd.ExecuteNonQuery(); + } + } + } + } \ No newline at end of file diff --git a/coding-tracker/GetUserInput.cs b/coding-tracker/GetUserInput.cs new file mode 100644 index 00000000..ca74788e --- /dev/null +++ b/coding-tracker/GetUserInput.cs @@ -0,0 +1,183 @@ +using System.Globalization; + +internal class GetUserinput +{ + CodingController codingController = new CodingController(); + internal void MainMenu() + { + bool closeApp = false; + while (closeApp == false) + { + Console.WriteLine("\n\nMAIN MENU"); + System.Console.WriteLine("\nWhat would you like to do?"); + System.Console.WriteLine("\nType 0 to Close Application"); + System.Console.WriteLine("Type 1 to View All Records"); + System.Console.WriteLine("Type 2 to Insert Record"); + System.Console.WriteLine("Type 3 to Update Record"); + System.Console.WriteLine("Type 4 to Delete Record"); + System.Console.WriteLine("---------------------------------"); + + string commandInput = Console.ReadLine(); + + while (string.IsNullOrEmpty(commandInput)) + { + System.Console.WriteLine("\nPlease re-enter your command."); + commandInput = Console.ReadLine(); + } + + switch (commandInput) + { + case "0": + System.Console.WriteLine("\nBye now\n"); + Environment.Exit(0); + break; + case "1": + codingController.Get(); + break; + case "2": + ProcessAdd(); + break; + case "3": + Update(); + break; + case "4": + Delete(); + break; + default: + System.Console.WriteLine("\n Invalid Command"); + break; + } + } + + } + + private void Update() + { + codingController.Get(); + + System.Console.WriteLine("\nPlease specify the id you want to update."); + string commandInput = Console.ReadLine(); + + while (!Int32.TryParse(commandInput, out _) || string.IsNullOrEmpty(commandInput) || Int32.Parse(commandInput) < 0) + { + System.Console.WriteLine("\nThis is not a valid Id format"); + commandInput = Console.ReadLine(); + } + + var id = Int32.Parse(commandInput); + + var coding = codingController.GetById(id); + + while (coding.Id == 0) + { + System.Console.WriteLine("\n\nInvalid Id"); + Update(); + } + + bool updating = true; + while (updating == true) + { + System.Console.WriteLine($"\nType d for Date"); + System.Console.WriteLine($"\nType t for Duration"); + System.Console.WriteLine($"\nType s for save the update"); + + var updateInput = Console.ReadLine(); + + switch (updateInput) + { + case "d": + coding.Date = GetDateInput(); + break; + case "t": + coding.Duration = GetDurationInput(); + break; + case "s": + updating = false; + break; + default: + System.Console.WriteLine("\nPlease type a valid input"); + break; + } + + } + codingController.Update(coding); + MainMenu(); + + } + + private void Delete() + { + codingController.Get(); + + System.Console.WriteLine("\nPlease specify the id you want to delete."); + string commandInput = Console.ReadLine(); + + while (!Int32.TryParse(commandInput, out _) || string.IsNullOrEmpty(commandInput) || Int32.Parse(commandInput) < 0) + { + System.Console.WriteLine("\nThis is not a valid Id format"); + commandInput = Console.ReadLine(); + } + + var id = Int32.Parse(commandInput); + + var coding = codingController.GetById(id); + + while (coding.Id == 0) + { + System.Console.WriteLine("\n\nInvalid Id"); + Delete(); + } + + codingController.Delete(id); + } + + private void ProcessAdd() + { + var date = GetDateInput(); + var duration = GetDurationInput(); + + Coding coding = new(); + + coding.Date = date; + coding.Duration = duration; + + codingController.Post(coding); + } + + private string GetDurationInput() + { + System.Console.WriteLine("\n\nPlease insert the duration (hh:mm)"); + + string durationInput = Console.ReadLine(); + + if (durationInput == "0") + MainMenu(); + + while (!TimeSpan.TryParseExact(durationInput, "h\\:mm", CultureInfo.InvariantCulture, out _)) + { + System.Console.WriteLine("\nNot a valid format"); + durationInput = Console.ReadLine(); + } + + return durationInput; + + } + + internal string GetDateInput() + { + System.Console.WriteLine("\n\nPlease insert the date (dd-mm-yy)"); + + string dateInput = Console.ReadLine(); + + if (dateInput == "0") + MainMenu(); + + while (!DateTime.TryParseExact(dateInput, "dd-MM-yy", new CultureInfo("en-US"), DateTimeStyles.None, out _)) + { + System.Console.WriteLine("\nNot a valid format"); + dateInput = Console.ReadLine(); + } + + return dateInput; + } +} diff --git a/coding-tracker/Program.cs b/coding-tracker/Program.cs new file mode 100644 index 00000000..b5c52f9f --- /dev/null +++ b/coding-tracker/Program.cs @@ -0,0 +1,17 @@ +namespace Coding_Tracker +{ + internal class Program + { + static string connectionString = System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString"); + static void Main(string[] args) + { + + DatabaseManager dbManager = new(); + GetUserinput userinput = new(); + + + dbManager.CreateTable(connectionString); + userinput.MainMenu(); + } + } +} \ No newline at end of file diff --git a/coding-tracker/TableVisualization.cs b/coding-tracker/TableVisualization.cs new file mode 100644 index 00000000..d7d309c3 --- /dev/null +++ b/coding-tracker/TableVisualization.cs @@ -0,0 +1,11 @@ +using ConsoleTableExt; +internal class TableVisualization +{ + internal static void ShowTable(List tableData) where T : class + { + System.Console.WriteLine("\n\n"); + + ConsoleTableBuilder.From(tableData).WithTitle("Coding").ExportAndWriteLine(); + System.Console.WriteLine("\n\n"); + } +} \ No newline at end of file diff --git a/coding-tracker/app.config b/coding-tracker/app.config new file mode 100644 index 00000000..c1792b5d --- /dev/null +++ b/coding-tracker/app.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/coding-tracker/coding-tracker.csproj b/coding-tracker/coding-tracker.csproj new file mode 100644 index 00000000..e652db2d --- /dev/null +++ b/coding-tracker/coding-tracker.csproj @@ -0,0 +1,17 @@ + + + + Exe + net9.0 + coding_tracker + enable + enable + + + + + + + + +