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
11 changes: 11 additions & 0 deletions CodingTracker.Myhos0/CodingSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CodingTrackerProgram;

internal class CodingSession
{
public int Id { get; set; }
public DateTime Date { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string Duration { get; set; }
}

77 changes: 77 additions & 0 deletions CodingTracker.Myhos0/CodingSessionRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Dapper;

namespace CodingTrackerProgram;

internal class CodingSessionRepository
{
private readonly DataBase _database;

public CodingSessionRepository(DataBase dataBase)
{
_database = dataBase;
}

public void Insert(CodingSession session)
{
const string SQL = @"INSERT INTO CodingSession(Date,StartTime,EndTime,Duration) Values(@Date,@StartTime,@EndTime,@Duration)";

using var connection = _database.GetConnection();
connection.Execute(SQL, session);
}

public IEnumerable<CodingSession> GetAll()
{
const string SQL = @"SELECT Id,Date,StartTime,EndTime,Duration FROM CodingSession ORDER BY Date ASC";

using var connection = _database.GetConnection();
return connection.Query<CodingSession>(SQL);
}

public void Delete(int id)
{
const string SQL = @"DElETE FROM CodingSession WHERE Id = @Id";

using var connection = _database.GetConnection();
int rows = connection.Execute(SQL, new { Id = id });

if (rows == 0)
{
throw new Exception("No records Found.");
}
}

public void Update(CodingSession session)
{
const string SQL = @"UPDATE CodingSession SET Date = @Date, StartTime = @StartTime, EndTime = @EndTime, Duration = @Duration WHERE Id = @Id";

using var connection = _database.GetConnection();
int rows = connection.Execute(SQL, session);

if (rows == 0)
throw new Exception("No record found to update.");
}

public bool SessionExist(int id)
{
const string SQL = "SELECT 1 FROM CodingSession WHERE Id = @Id LIMIT 1";

using var connection = _database.GetConnection();
return connection.ExecuteScalar<int?>(SQL, new { Id = id }) != null;
}

public IEnumerable<CodingSession> GetSessionsByDateRange(string start, string end)
{
const string SQL = @"SELECT * FROM CodingSession WHERE Date BETWEEN @Start AND @End ORDER BY Date ASC, Duration ASC;";

using var connection = _database.GetConnection();
return connection.Query<CodingSession>(SQL, new { Start = start, End = end });
}

public bool HasAnySessions()
{
const string SQL = "SELECT 1 FROM CodingSession LIMIT 1";

using var connection = _database.GetConnection();
return connection.ExecuteScalar<int?>(SQL) != null;
}
}
48 changes: 48 additions & 0 deletions CodingTracker.Myhos0/CodingSessionSeeder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using CodingTrackerProgram;
using Spectre.Console;
using System;

namespace CodingTrackerProgram;

internal class CodingSessionSeeder
{
private readonly CodingSessionRepository repository;
private readonly Random random = new();

public CodingSessionSeeder(DataBase dataBase)
{
repository = new CodingSessionRepository(dataBase);
}

public void Seed(int numberOfSessions)
{
for (int i = 0; i < numberOfSessions; i++)
{
CodingSession session = GenerateRandomSession();
repository.Insert(session);
}
}

private CodingSession GenerateRandomSession()
{
DateTime startRange = DateTime.Today.AddYears(-3);
int range = (DateTime.Today - startRange).Days;

DateTime date = startRange.AddDays(random.Next(0, range));

TimeSpan start = TimeSpan.FromMinutes(random.Next(6 * 60, 22 * 60));

TimeSpan duration = TimeSpan.FromMinutes(random.Next(60,241));

TimeSpan end = start + duration;

return new CodingSession
{
Date = date,
StartTime = start.ToString(@"hh\:mm\:ss"),
EndTime = end.ToString(@"hh\:mm\:ss"),
Duration = duration.ToString(@"hh\:mm\:ss")
};
}
}

Loading