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

finished the project #227

Open
wants to merge 5 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
25 changes: 25 additions & 0 deletions HabitTracker.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.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HabitTracker", "HabitTracker\HabitTracker.csproj", "{92CE25FA-B589-48E7-B35A-A426F7E1CFDF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{92CE25FA-B589-48E7-B35A-A426F7E1CFDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{92CE25FA-B589-48E7-B35A-A426F7E1CFDF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92CE25FA-B589-48E7-B35A-A426F7E1CFDF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92CE25FA-B589-48E7-B35A-A426F7E1CFDF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {875D0802-5575-4A76-8935-8CFBF1874BF4}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions HabitTracker/Credits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace HabitTracker
{
internal class Credits
{
static public void GetCredits()
{
var credits = new List<string>();
credits.Add("Created by Nikita Kostin.\n");
credits.ForEach(i => Console.Write(i));
Console.Write("Press any key to go back to the main menu...");
Console.ReadKey();
}
}
}
172 changes: 172 additions & 0 deletions HabitTracker/Data/DbConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using Microsoft.Data.Sqlite;

namespace HabitTracker.Data
{
public class DbConnection
{
private static SqliteConnection _connection;

private DbConnection() { }
enum Frequency
{
Daily,
Weekly,
Monthly
}

public static SqliteConnection GetConnection(bool isStart = false)
{
if (_connection == null)
{
const string dbPath = "testDb.db";
_connection = new SqliteConnection($"Data Source={dbPath}");
_connection.Open();
Console.WriteLine("The database has been connected.");

if (isStart)
{
DbConnection.CreateTable("Habits");
DbConnection.CreateTable("Records");
DbConnection.SeedData();
}


_connection.Close();
}
return _connection;
}

public static void CloseConnection()
{
if (_connection != null)
{
_connection.Close();
_connection.Dispose();
_connection = null;
Console.WriteLine("The database connection has been closed.");
}
}

public static void CreateTable(string name)
{
try
{
var command = _connection.CreateCommand();
if (name == "Habits")
{
command.CommandText =
@"
CREATE TABLE IF NOT EXISTS Habits (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT NOT NULL,
Frequency TEXT NOT NULL,
TimesPerPeriod INTEGER NOT NULL,
StartDate TEXT NOT NULL
)
";
command.ExecuteNonQuery();
}
else
{
command.CommandText =
@"
CREATE TABLE IF NOT EXISTS Records (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name TEXT NOT NULL,
HabitDate TEXT NOT NULL,
HabitId INTEGER,
FOREIGN KEY (HabitId) REFERENCES Habits(Id) ON DELETE CASCADE
)
";
command.ExecuteNonQuery();
}

}
catch (Exception ex)
{
Console.WriteLine("Error creating table: " + ex.Message);
}

}


public static void SeedData()
{
bool create = false;
using (var command = _connection.CreateCommand())
{
command.CommandText = "SELECT COUNT(*) FROM Habits";
long habitCount = (long)command.ExecuteScalar();

if (habitCount == 0)
{
create = true;
Console.WriteLine("Seeding data into the Habits table.");
var random = new Random();

for (int i = 0; i < 100; i++)
{
string habitName = $"Habit {i + 1}";
Frequency frequency = (Frequency)random.Next(0, 3);
string frequencyString = frequency.ToString();
int timesPerPeriod = random.Next(1, 5);
string startDate = DateTime.Now.AddDays(-random.Next(0, 100)).ToString("yyyy-MM-dd");

command.CommandText =
@"
INSERT INTO Habits (Name, Frequency, TimesPerPeriod, StartDate)
VALUES (@habitName, @frequency, @timesPerPeriod, @startDate)
";

command.Parameters.Clear();
command.Parameters.AddWithValue("@habitName", habitName);
command.Parameters.AddWithValue("@frequency", frequencyString);
command.Parameters.AddWithValue("@timesPerPeriod", timesPerPeriod);
command.Parameters.AddWithValue("@startDate", startDate);

command.ExecuteNonQuery();
}
}
}

if (create)
{
using (var command = _connection.CreateCommand())
{
command.CommandText = "SELECT Id, Name FROM Habits";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
int habitId = reader.GetInt32(0);
string habitName = reader.GetString(1);

using (var insertCommand = _connection.CreateCommand())
{
for (int j = 0; j < 10; j++)
{
string habitDate = DateTime.Now.AddDays(-new Random().Next(0, 30)).ToString("yyyy-MM-dd");

insertCommand.CommandText =
@"
INSERT INTO Records (Name, HabitDate, HabitId)
VALUES (@habitName, @habitDate, @habitId)
";

insertCommand.Parameters.Clear();
insertCommand.Parameters.AddWithValue("@habitName", habitName);
insertCommand.Parameters.AddWithValue("@habitDate", habitDate);
insertCommand.Parameters.AddWithValue("@habitId", habitId);

insertCommand.ExecuteNonQuery();
}
}
}
}
}
}
}


}
}
Loading