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
Binary file added .DS_Store
Binary file not shown.
4 changes: 3 additions & 1 deletion Asana.API/Asana.API.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.7" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="System.Data.SQLite" Version="1.0.119" />
</ItemGroup>

<ItemGroup>
Expand Down
24 changes: 24 additions & 0 deletions Asana.API/Asana.API.sln
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion Asana.API/Controllers/ToDoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ public IEnumerable<ToDo> 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]
Expand Down
134 changes: 134 additions & 0 deletions Asana.API/Database/SqLiteContext.cs
Original file line number Diff line number Diff line change
@@ -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<ToDo> ToDos
{
get
{
var toDos = new List<ToDo>();

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;
}
}
}
24 changes: 12 additions & 12 deletions Asana.API/Enterprise/ToDoEC.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
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<ToDo> GetToDos()
{
return Filebase.Current.ToDos.Take(100);
return _context.ToDos.Take(100);
}

public ToDo? GetById(int id)
{
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);
}
}
}
1 change: 1 addition & 0 deletions Asana.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"Microsoft.AspNetCore": "Warning"
}
},

"AllowedHosts": "*"
}
Binary file added Asana.API/asana.db
Binary file not shown.
3 changes: 2 additions & 1 deletion Asana.CLI/Asana.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

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

<ItemGroup>
<ProjectReference Include="..\Asana.API\Asana.API.csproj" />
<ProjectReference Include="..\Asana.Library\Asana.Library.csproj" />
</ItemGroup>

Expand Down
109 changes: 29 additions & 80 deletions Asana.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
Binary file added Asana.CLI/asana.db
Binary file not shown.
Loading