diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..783e7f6 Binary files /dev/null and b/.DS_Store differ diff --git a/Asana.API/Asana.API.csproj b/Asana.API/Asana.API.csproj index 3260759..92ffb75 100644 --- a/Asana.API/Asana.API.csproj +++ b/Asana.API/Asana.API.csproj @@ -1,14 +1,16 @@ - net8.0 + net9.0 enable enable + + diff --git a/Asana.API/Asana.API.sln b/Asana.API/Asana.API.sln new file mode 100644 index 0000000..0ce6a9b --- /dev/null +++ b/Asana.API/Asana.API.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Asana.API", "Asana.API.csproj", "{447D3E3A-83F3-91C7-4B6B-DAFF35B66491}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {447D3E3A-83F3-91C7-4B6B-DAFF35B66491}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {447D3E3A-83F3-91C7-4B6B-DAFF35B66491}.Debug|Any CPU.Build.0 = Debug|Any CPU + {447D3E3A-83F3-91C7-4B6B-DAFF35B66491}.Release|Any CPU.ActiveCfg = Release|Any CPU + {447D3E3A-83F3-91C7-4B6B-DAFF35B66491}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {69FD3303-CA61-48FB-9807-0E80CD7B91E3} + EndGlobalSection +EndGlobal diff --git a/Asana.API/Controllers/ToDoController.cs b/Asana.API/Controllers/ToDoController.cs index 80d9b17..2fa90a1 100644 --- a/Asana.API/Controllers/ToDoController.cs +++ b/Asana.API/Controllers/ToDoController.cs @@ -24,7 +24,13 @@ public IEnumerable Get() [HttpDelete("{id}")] public ToDo? Delete(int id) { - return new ToDoEC().Delete(id); + var ec = new ToDoEC(); + var toDo = ec.GetById(id); // get it before it's deleted + if (toDo == null) + return null; + + ec.Delete(id); + return toDo; } [HttpPost] diff --git a/Asana.API/Database/SqLiteContext.cs b/Asana.API/Database/SqLiteContext.cs new file mode 100644 index 0000000..35bd0bb --- /dev/null +++ b/Asana.API/Database/SqLiteContext.cs @@ -0,0 +1,134 @@ +using Asana.Library.Models; +using Microsoft.Data.Sqlite; +using System.Collections.Generic; + +namespace Asana.API.Database +{ + public class SqliteContext + { + private string _connectionString; + + public SqliteContext() + { + _connectionString = "Data Source=asana.db"; + InitializeDatabase(); + } + + private void InitializeDatabase() + { + using (var connection = new SqliteConnection(_connectionString)) + { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = @" + CREATE TABLE IF NOT EXISTS ToDos ( + Id INTEGER PRIMARY KEY AUTOINCREMENT, + Name TEXT NOT NULL, + Description TEXT, + IsCompleted INTEGER NOT NULL, + Priority INTEGER NOT NULL + );"; + command.ExecuteNonQuery(); + } + } + + public List ToDos + { + get + { + var toDos = new List(); + + using (var connection = new SqliteConnection(_connectionString)) + { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = "SELECT * FROM ToDos"; + + using (var reader = command.ExecuteReader()) + { + while (reader.Read()) + { + var toDo = new ToDo + { + Id = reader.GetInt32(reader.GetOrdinal("Id")), + Name = reader.GetString(reader.GetOrdinal("Name")), + Description = reader.GetString(reader.GetOrdinal("Description")), + IsCompleted = reader.GetInt32(reader.GetOrdinal("IsCompleted")) == 1, + Priority = reader.GetInt32(reader.GetOrdinal("Priority")) + }; + toDos.Add(toDo); + } + } + } + + return toDos; + } + } + + public ToDo? AddOrUpdateToDo(ToDo? toDo) + { + if (toDo == null) return null; + + using (var connection = new SqliteConnection(_connectionString)) + { + connection.Open(); + + var command = connection.CreateCommand(); + + if (toDo.Id <= 0) + { + command.CommandText = @" + INSERT INTO ToDos (Name, Description, IsCompleted, Priority) + VALUES (@name, @description, @isCompleted, @priority); + SELECT last_insert_rowid();"; + } + else + { + command.CommandText = @" + UPDATE ToDos + SET Name = @name, + Description = @description, + IsCompleted = @isCompleted, + Priority = @priority + WHERE Id = @id;"; + command.Parameters.AddWithValue("@id", toDo.Id); + } + + command.Parameters.AddWithValue("@name", toDo.Name); + command.Parameters.AddWithValue("@description", toDo.Description); + command.Parameters.AddWithValue("@isCompleted", toDo.IsCompleted); + command.Parameters.AddWithValue("@priority", toDo.Priority); + + if (toDo.Id <= 0) + { + var result = command.ExecuteScalar(); + toDo.Id = Convert.ToInt32(result); + } + else + { + command.ExecuteNonQuery(); + } + } + + return toDo; + } + + public int DeleteToDo(int toDoId) + { + using (var connection = new SqliteConnection(_connectionString)) + { + connection.Open(); + + var command = connection.CreateCommand(); + command.CommandText = "DELETE FROM ToDos WHERE Id = @id"; + command.Parameters.AddWithValue("@id", toDoId); + + command.ExecuteNonQuery(); + } + + return toDoId; + } + } +} diff --git a/Asana.API/Enterprise/ToDoEC.cs b/Asana.API/Enterprise/ToDoEC.cs index 75609f2..410e30e 100644 --- a/Asana.API/Enterprise/ToDoEC.cs +++ b/Asana.API/Enterprise/ToDoEC.cs @@ -1,18 +1,20 @@ -using Api.ToDoApplication.Persistence; -using Asana.API.Database; +using Asana.API.Database; using Asana.Library.Models; namespace Asana.API.Enterprise { public class ToDoEC { - public ToDoEC() { - + private readonly SqliteContext _context; + + public ToDoEC() + { + _context = new SqliteContext(); } public IEnumerable GetToDos() { - return Filebase.Current.ToDos.Take(100); + return _context.ToDos.Take(100); } public ToDo? GetById(int id) @@ -20,20 +22,18 @@ public IEnumerable GetToDos() return GetToDos().FirstOrDefault(t => t.Id == id); } - public ToDo? Delete(int id) + public int Delete(int id) { - var toDoToDelete = GetById(id); - if (toDoToDelete != null) + if (id > 0) { - //Filebase.Current.Delete(toDoToDelete); + return _context.DeleteToDo(id); } - return toDoToDelete; + return -1; } public ToDo? AddOrUpdate(ToDo? toDo) { - Filebase.Current.AddOrUpdate(toDo); - return toDo; + return _context.AddOrUpdateToDo(toDo); } } } diff --git a/Asana.API/appsettings.json b/Asana.API/appsettings.json index 10f68b8..612bbd1 100644 --- a/Asana.API/appsettings.json +++ b/Asana.API/appsettings.json @@ -5,5 +5,6 @@ "Microsoft.AspNetCore": "Warning" } }, + "AllowedHosts": "*" } diff --git a/Asana.API/asana.db b/Asana.API/asana.db new file mode 100644 index 0000000..770b291 Binary files /dev/null and b/Asana.API/asana.db differ diff --git a/Asana.CLI/Asana.CLI.csproj b/Asana.CLI/Asana.CLI.csproj index 7c06fea..ce657ba 100644 --- a/Asana.CLI/Asana.CLI.csproj +++ b/Asana.CLI/Asana.CLI.csproj @@ -2,12 +2,13 @@ Exe - net8.0 + net9.0 enable enable + diff --git a/Asana.CLI/Program.cs b/Asana.CLI/Program.cs index a3be611..b266b06 100644 --- a/Asana.CLI/Program.cs +++ b/Asana.CLI/Program.cs @@ -1,88 +1,37 @@ -using Asana.Library.Models; -using Asana.Library.Services; +using Asana.API.Database; +using Asana.Library.Models; using System; -namespace Asana +class Program { - public class Program + static void Main(string[] args) { - - public static void Main(string[] args) - { - var toDoSvc = ToDoServiceProxy.Current; - int choiceInt; - do - { - Console.WriteLine("Choose a menu option:"); - Console.WriteLine("1. Create a ToDo"); - Console.WriteLine("2. List all ToDos"); - Console.WriteLine("3. List all outstanding ToDos"); - Console.WriteLine("4. Delete a ToDo"); - Console.WriteLine("5. Update a ToDo"); - Console.WriteLine("6. Exit"); - - var choice = Console.ReadLine() ?? "6"; - - if (int.TryParse(choice, out choiceInt)) - { - switch (choiceInt) - { - case 1: - Console.Write("Name:"); - var name = Console.ReadLine(); - Console.Write("Description:"); - var description = Console.ReadLine(); - - toDoSvc.AddOrUpdate(new ToDo - { - Name = name, - Description = description, - IsCompleted = false, - Id = 0 - }); - break; - case 2: - toDoSvc.DisplayToDos(true); - break; - case 3: - toDoSvc.DisplayToDos(); - break; - case 4: - toDoSvc.DisplayToDos(true); - Console.Write("ToDo to Delete: "); - var toDoChoice4 = int.Parse(Console.ReadLine() ?? "0"); - - var reference = toDoSvc.GetById(toDoChoice4); - toDoSvc.DeleteToDo(reference?.Id ?? 0); - break; - case 5: - toDoSvc.DisplayToDos(true); - Console.Write("ToDo to Update: "); - var toDoChoice5 = int.Parse(Console.ReadLine() ?? "0"); - var updateReference = toDoSvc.GetById(toDoChoice5); - if (updateReference != null) - { - Console.Write("Name:"); - updateReference.Name = Console.ReadLine(); - Console.Write("Description:"); - updateReference.Description = Console.ReadLine(); - } - toDoSvc.AddOrUpdate(updateReference); - break; - case 6: - break; - default: - Console.WriteLine("ERROR: Unknown menu selection"); - break; - } - } else - { - Console.WriteLine($"ERROR: {choice} is not a valid menu selection"); - } - - } while (choiceInt != 6); + // Added the sql connection and then delete it + var context = new SqliteContext(); + // Add a new ToDo + var newToDo = new ToDo + { + Name = "Test Task", + Description = "Testing SQLite insert", + IsCompleted = false, + Priority = 1 + }; + + context.AddOrUpdateToDo(newToDo); + Console.WriteLine($"Added ToDo with ID: {newToDo.Id}"); + + // Get all ToDos + var allToDos = context.ToDos; + Console.WriteLine("\nAll ToDos:"); + foreach (var todo in allToDos) + { + Console.WriteLine($"ID: {todo.Id}, Name: {todo.Name}, Completed: {todo.IsCompleted}"); } + + // Delete test ToDo + context.DeleteToDo(newToDo.Id); + Console.WriteLine($"\nDeleted ToDo with ID: {newToDo.Id}"); } -} \ No newline at end of file +} diff --git a/Asana.CLI/asana.db b/Asana.CLI/asana.db new file mode 100644 index 0000000..95d1a05 Binary files /dev/null and b/Asana.CLI/asana.db differ diff --git a/Asana.Library/DTOs/ToDoDTO.cs b/Asana.Library/DTOs/ToDoDTO.cs new file mode 100644 index 0000000..c51d1a6 --- /dev/null +++ b/Asana.Library/DTOs/ToDoDTO.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Asana.Library.DTOs +{ + public class ToDoDTO + { + public string? Name { get; set; } + public string? Description { get; set; } + public int? Priority { get; set; } + public bool? IsCompleted { get; set; } + + public int? ProjectId { get; set; } + + public int Id { get; set; } + + public ToDoDTO() { } + public ToDoDTO(ToDoDTO td) { + Id = td.Id; + Name = td.Name; + Description = td.Description; + Priority = td.Priority; + IsCompleted = td.IsCompleted; + ProjectId = td.ProjectId; + } + } +} \ No newline at end of file diff --git a/Asana.Library/Models/ToDo.cs b/Asana.Library/Models/ToDo.cs index ae521c9..46e8888 100644 --- a/Asana.Library/Models/ToDo.cs +++ b/Asana.Library/Models/ToDo.cs @@ -1,4 +1,5 @@ -using System; +using Asana.Library.DTOs; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -13,6 +14,15 @@ public ToDo() Id = 0; IsCompleted = false; } + public ToDo(ToDoDTO dto) + { + Id = dto.Id; + IsCompleted = dto.IsCompleted; + Name = dto.Name; + Priority = dto.Priority; + Description = dto.Description; + + } public string? Name { get; set; } public string? Description { get; set; } public int? Priority { get; set; } @@ -26,5 +36,7 @@ public override string ToString() { return $"[{Id}] {Name} - {Description}"; } + + } -} +} \ No newline at end of file diff --git a/Asana.Maui/.DS_Store b/Asana.Maui/.DS_Store new file mode 100644 index 0000000..5c12618 Binary files /dev/null and b/Asana.Maui/.DS_Store differ