Skip to content
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

Habit Logger complete #229

Open
wants to merge 27 commits into
base: master
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
Binary file not shown.
25 changes: 25 additions & 0 deletions HabitTracker.Wolfieeex/HabitLoggerConsole/HabitLoggerConsole.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.10.35004.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HabitLoggerConsole", "HabitLoggerConsole\HabitLoggerConsole.csproj", "{6CAEEDE5-FCFA-4D0F-9393-CDBD8F4D8411}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6CAEEDE5-FCFA-4D0F-9393-CDBD8F4D8411}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6CAEEDE5-FCFA-4D0F-9393-CDBD8F4D8411}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6CAEEDE5-FCFA-4D0F-9393-CDBD8F4D8411}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6CAEEDE5-FCFA-4D0F-9393-CDBD8F4D8411}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6E047BED-91F3-4BFA-8622-9F3CB42932DD}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System.Globalization;
using Microsoft.Data.Sqlite;

namespace HabitLoggerConsole.Models;

internal enum TrackerName
{
going_to_gym,
drinking_water,
power_walking,
sleeping,
programming
};

internal enum TrackerHabitDescription
{
per_day,
per_week,
per_month,
per_year,
per_hour,
};

class DataSeed()
{
internal static void CreateTestRecord()
{
int numberOfTestTables = 3;

int habitsCreatedCounter = 0;

using (var connection = new SqliteConnection(Program.ConnectionString))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = $"SELECT COUNT(*) name FROM sqlite_master WHERE type = 'table' AND name LIKE 'test%'";
long testHabitCount = (long)cmd.ExecuteScalar();

while (testHabitCount < numberOfTestTables)
{
Random random = new Random();
int sequence = 0;

string habitName = "";
string trackedName = "";
string? measurementType;

bool habitNameSelected = false;
while (!habitNameSelected)
{
sequence = random.Next(0, Enum.GetNames(typeof(TrackerName)).Length);
habitName = "test_" + (TrackerName)sequence;

cmd.CommandText = $"SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '{habitName}'";

var reader = cmd.ExecuteReader();

if (!reader.HasRows)
{
habitNameSelected = true;
}
else
{
reader.Close();
continue;
}

trackedName = Enum.GetName(typeof(TrackerHabitDescription), (TrackerHabitDescription)sequence);

reader.Close();
}

sequence = random.Next(0, Enum.GetNames(typeof(MeasurementType)).Length);
measurementType = Enum.GetName(typeof(MeasurementType), (MeasurementType)sequence);

habitsCreatedCounter++;
cmd.CommandText = $"CREATE TABLE IF NOT EXISTS '{habitName}' (Id INTEGER PRIMARY KEY AUTOINCREMENT, Date TEXT, '{trackedName}' REAL, '{measurementType}' TEXT)";

cmd.ExecuteNonQuery();

sequence = random.Next(1, 2001);

for (int i = 0; i < sequence; i++)
{
string? date;
int? valueAchieved;

int day = random.Next(1, 29);
int month = random.Next(1, 13);
int preYearValue = random.Next(0, 6);
int year = 2024 - preYearValue;
date = $"{day.ToString()}/{month.ToString()}/{year.ToString()}";
DateTime datetime = DateTime.Parse(date, new CultureInfo("en-GB"));
date = datetime.ToString("dddd, dd MMMM, yyyy");

valueAchieved = random.Next(10, 1001);

cmd.CommandText = $"INSERT INTO '{habitName}'(Date, '{trackedName}') VALUES('{date}', '{valueAchieved}')";

cmd.ExecuteNonQuery();
}

cmd.CommandText = $"SELECT COUNT(*) name FROM sqlite_master WHERE type = 'table' AND name LIKE 'test%'";
testHabitCount = (long)cmd.ExecuteScalar();
}

Console.BackgroundColor = ConsoleColor.DarkGreen;
switch (habitsCreatedCounter)
{
case 0:
Console.WriteLine($"Developer message: <No seed table could be created: 3 test tables already exists.>\n\n");
break;
case 1:
Console.WriteLine($"Developer message: <1 test table have been created. To disable this option, assign false to /runDeveloperSeedTest/ variable in Program.cs class.>\n\n");
break;
default:
Console.WriteLine($"Developer message: <{habitsCreatedCounter} additional test tables have been created. To disable this option, assign false to /runDeveloperSeedTest/ variable in Program.cs class.>\n\n");
break;

}
Console.ResetColor();
connection.Close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace HabitLoggerConsole.Models;

public enum MeasurementType
{
blank,
l,
ml,
cm,
m,
km,
inch,
ft,
g,
kg,
lbs,
stones,
}

public enum MeasurementDivision
{
None,
Liquid,
Length,
Weight,
Time
}
internal class MeasurementUnits
{
internal static Dictionary<MeasurementType, MeasurementDivision> MeasurementDivisions { get; private set; } = new Dictionary<MeasurementType, MeasurementDivision>()
{
{ MeasurementType.blank, MeasurementDivision.None },
{ MeasurementType.l, MeasurementDivision.Liquid },
{ MeasurementType.ml, MeasurementDivision.Liquid },
{ MeasurementType.cm, MeasurementDivision.Length },
{ MeasurementType.m, MeasurementDivision.Length },
{ MeasurementType.km, MeasurementDivision.Length },
{ MeasurementType.inch, MeasurementDivision.Length },
{ MeasurementType.ft, MeasurementDivision.Length },
{ MeasurementType.g, MeasurementDivision.Weight },
{ MeasurementType.kg, MeasurementDivision.Weight },
{ MeasurementType.lbs, MeasurementDivision.Weight },
{ MeasurementType.stones, MeasurementDivision.Weight },
};

internal static Dictionary<MeasurementType, string> MeasurementFullName { get; private set; } = new Dictionary<MeasurementType, string>
{
{ MeasurementType.blank, "(blank)" },
{ MeasurementType.l, "liters" },
{ MeasurementType.ml, "mililiters" },
{ MeasurementType.cm, "centimeters" },
{ MeasurementType.m, "meters" },
{ MeasurementType.km, "kilometers" },
{ MeasurementType.inch, "inches" },
{ MeasurementType.ft, "feet" },
{ MeasurementType.g, "grams" },
{ MeasurementType.kg, "kilograms" },
{ MeasurementType.lbs, "pounds" },
{ MeasurementType.stones, "stones" },
};

internal static MeasurementType[] DisplayMeasurements()
{
var sortedMeasurementList = from entry in MeasurementUnits.MeasurementDivisions orderby entry.Value ascending select entry;
var sortedDivisionDictionary = sortedMeasurementList.ToDictionary(pair => pair.Key, pair => pair.Value);
var arrayOfKeys = sortedDivisionDictionary.Keys;

MeasurementDivision? previousDivision = null;
int listCounter = 0;
foreach (MeasurementType key in arrayOfKeys)
{
listCounter++;

if (previousDivision == null || previousDivision != MeasurementUnits.MeasurementDivisions[key])
{
Console.WriteLine($"\n{MeasurementUnits.MeasurementDivisions[key]}:");
previousDivision = MeasurementUnits.MeasurementDivisions[key];
}

string measurementName = MeasurementUnits.MeasurementFullName[key];
measurementName = measurementName[0].ToString().ToUpper() + measurementName.Substring(1);


Console.WriteLine($"{listCounter} - {measurementName}");
}

return arrayOfKeys.ToArray();
}
}
Loading