Skip to content

Finished project #186

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
wants to merge 2 commits into
base: main
Choose a base branch
from
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
25 changes: 25 additions & 0 deletions CodingTracker.mrgee1978/CodingTracker.mrgee1978.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35806.99 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodingTracker.mrgee1978", "CodingTracker.mrgee1978\CodingTracker.mrgee1978.csproj", "{58D01472-F371-4652-B7A3-93D42A8B9491}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{58D01472-F371-4652-B7A3-93D42A8B9491}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58D01472-F371-4652-B7A3-93D42A8B9491}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58D01472-F371-4652-B7A3-93D42A8B9491}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58D01472-F371-4652-B7A3-93D42A8B9491}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1FEB1F1F-F2DB-480F-924B-D728DADCCC59}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.66" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.2" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>




</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Data.Sqlite;
using Dapper;

namespace CodingTracker.mrgee1978.DataAccessLayer;

public static class DatabaseInitialization
{
private static IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
public static string? DatabaseConnectionString { get; } = config.GetConnectionString("DatabaseConnection");



/// <summary>
/// Initializes the database and creates the necessary tables if they
/// don't already exist
/// </summary>
public static void InitializeDatabase()
{
try
{
using (SqliteConnection connection = new SqliteConnection(DatabaseConnectionString))
{
connection.Open();

string tableCreationCommand = @"
CREATE TABLE IF NOT EXISTS sessions (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
StartDate TEXT NOT NULL,
EndDate TEXT NOT NULL);";

connection.Execute(tableCreationCommand);
}
}
catch (SqliteException ex)
{
Console.WriteLine($"{ex.Message}");
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Data.Sqlite;
using Dapper;

namespace CodingTracker.mrgee1978.DataAccessLayer;

public class DeleteData
{
/// <summary>
/// Deletes a coding session based on the id of the session
/// </summary>
/// <param name="id"></param>
public int DeleteSession(int id)
{
try
{
using (SqliteConnection connection = new SqliteConnection(DatabaseInitialization.DatabaseConnectionString))
{
connection.Open();

string deleteStatement = "DELETE FROM sessions WHERE Id = @Id";

// Get the number of rows affected so that you can check to make
// sure that the row was actually deleted
return connection.Execute(deleteStatement, new { Id = id });
}
}
catch (SqliteException ex)
{
Console.WriteLine($"{ex.Message}");
return -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.Data.Sqlite;
using Dapper;
using CodingTracker.mrgee1978.DomainLayer.Models;

namespace CodingTracker.mrgee1978.DataAccessLayer;

public class InsertData
{
/// <summary>
/// Inserts a coding session into the database using
/// a coding session object
/// </summary>
/// <param name="session"></param>
public bool InsertSession(CodingSession session)
{
try
{
using (SqliteConnection connection = new SqliteConnection(DatabaseInitialization.DatabaseConnectionString))
{
connection.Open();

string insertStatement = @"INSERT INTO sessions (StartDate, EndDate)
VALUES (@StartDate, @EndDate);";

connection.Execute(insertStatement, new { session.StartDate, session.EndDate });
return true;
}
}
catch (SqliteException ex)
{
Console.WriteLine($"{ex.Message}");
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.Data.Sqlite;
using Dapper;
using CodingTracker.mrgee1978.DomainLayer.Models;

namespace CodingTracker.mrgee1978.DataAccessLayer;

public class RetrieveData
{
/// <summary>
/// Retrieves all coding sessions from the database and returns them as
/// a list of coding session objects
/// </summary>
/// <returns></returns>
public List<CodingSession> RetrieveSessions()
{
try
{
using (SqliteConnection connection = new SqliteConnection(DatabaseInitialization.DatabaseConnectionString))
{
connection.Open();

string retrievalStatement = "SELECT * FROM sessions";

List<CodingSession> sessions = connection.Query<CodingSession>(retrievalStatement).ToList();

// Loop through the list of coding sessions to calculate the duration of each session
foreach (CodingSession session in sessions)
{
session.Duration = session.EndDate.Subtract(session.StartDate);
}

return sessions;
}
}
catch (SqliteException ex)
{
Console.WriteLine($"{ex.Message}");
return new List<CodingSession>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.Data.Sqlite;
using Dapper;
using CodingTracker.mrgee1978.DomainLayer.Models;

namespace CodingTracker.mrgee1978.DataAccessLayer;

public class UpdateData
{
/// <summary>
/// Updates a coding session in the database using a
/// coding session object
/// </summary>
/// <param name="session"></param>
public bool UpdateSession(CodingSession session)
{
try
{
using (SqliteConnection connection = new SqliteConnection(DatabaseInitialization.DatabaseConnectionString))
{
connection.Open();

string updateStatement = @"UPDATE sessions SET
StartDate = @StartDate, EndDate = @EndDate WHERE Id = @Id";

connection.Execute(updateStatement, new {session.Id, session.StartDate, session.EndDate});
return true;
}
}
catch (SqliteException ex)
{
Console.WriteLine($"{ex.Message}");
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CodingTracker.mrgee1978.DomainLayer.Models;

// A simple class that will be used to store all
// necessary information about each coding session
public class CodingSession
{
public int Id { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public TimeSpan Duration { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;

namespace CodingTracker.mrgee1978.PresentationLayer.Enumerations;

// Enums for menu display
public enum MenuOptions
{
[Display (Name = "Add coding session")]
AddSession,

[Display (Name = "Update coding session")]
UpdateSession,

[Display (Name = "Delete coding session")]
DeleteSession,

[Display (Name = "View coding sessions")]
ViewSessions,

[Display (Name = "Quit program")]
Quit,
}
Loading