Skip to content

Commit

Permalink
Add comment summaries
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jan 7, 2025
1 parent 2725a3c commit d5b794f
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 1 deletion.
3 changes: 3 additions & 0 deletions JournalApp/Data/AppDataService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace JournalApp;

/// <summary>
/// Provides services for managing app data, including backup and restore operations.
/// </summary>
public sealed class AppDataService(ILogger<AppDataService> logger, IDbContextFactory<AppDbContext> dbFactory, PreferenceService preferences)
{
public async Task DeleteDbSets()
Expand Down
3 changes: 3 additions & 0 deletions JournalApp/Data/AppDataUIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace JournalApp;

/// <summary>
/// Provides UI-related services for managing app data, including import and export wizards.
/// </summary>
public sealed class AppDataUIService(ILogger<AppDataUIService> logger, AppDataService appDataService, IShare share, PreferenceService preferenceService)
{
public async Task<bool> StartImportWizard(IDialogService dialogService, string path)
Expand Down
11 changes: 10 additions & 1 deletion JournalApp/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public async Task<Day> GetOrCreateDayAndAddPoints(DateOnly date)
return day;
}

/// <summary>
/// Gets missing data points for the specified day and category.
/// </summary>
public HashSet<DataPoint> GetMissingPoints(Day day, DataPointCategory category, Random random)
{
var newPoints = new HashSet<DataPoint>();
Expand Down Expand Up @@ -110,6 +113,9 @@ public HashSet<DataPoint> GetMissingPoints(Day day, DataPointCategory category,
return newPoints;
}

/// <summary>
/// Adds a new category to the database.
/// </summary>
public void AddCategory(DataPointCategory category)
{
// Set index to the end of the last category in the same group.
Expand All @@ -123,6 +129,9 @@ public void AddCategory(DataPointCategory category)
Categories.Add(category);
}

/// <summary>
/// Moves the specified category up in the order.
/// </summary>
public async Task MoveCategoryUp(DataPointCategory category)
{
// Ensure no conflicts.
Expand All @@ -139,7 +148,7 @@ public async Task MoveCategoryUp(DataPointCategory category)
}

/// <summary>
/// Removes gaps or overlap between indexes.
/// Fixes the indexes of the categories to remove gaps or overlap.
/// </summary>
public void FixCategoryIndexes()
{
Expand Down
16 changes: 16 additions & 0 deletions JournalApp/Data/AppDbSeeder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
namespace JournalApp;

/// <summary>
/// Seeds the database with initial data and prepares it for use.
/// </summary>
public class AppDbSeeder(ILogger<AppDbSeeder> logger, IDbContextFactory<AppDbContext> dbFactory)
{
/// <summary>
/// Prepares the database by migrating it and ensuring it is up to date.
/// </summary>
public void PrepareDatabase()
{
logger.LogInformation("Preparing database");
Expand Down Expand Up @@ -35,6 +41,9 @@ public void PrepareDatabase()
logger.LogInformation($"Prepared database in {sw.ElapsedMilliseconds}ms");
}

/// <summary>
/// Seeds the database with predefined categories.
/// </summary>
public void SeedCategories()
{
logger.LogInformation("Seeding categories");
Expand Down Expand Up @@ -261,6 +270,10 @@ void AddOrUpdate(
logger.LogInformation($"Seeded categories in {sw.ElapsedMilliseconds}ms");
}

/// <summary>
/// Seeds the database with debug data for the specified dates.
/// </summary>
/// <param name="dates">The dates to seed.</param>
public void SeedDays(IEnumerable<DateOnly> dates)
{
using var db = dbFactory.CreateDbContext();
Expand Down Expand Up @@ -309,6 +322,9 @@ void SeedDaysWithCategory()
db.SaveChanges();
}

/// <summary>
/// Seeds the database with debug data.
/// </summary>
public void SeedDays()
{
var sw = Stopwatch.StartNew();
Expand Down
18 changes: 18 additions & 0 deletions JournalApp/Data/BackupFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace JournalApp;

/// <summary>
/// Handles backup and restore operations for the journal data.
/// </summary>
public class BackupFile
{
private const string InternalBackupFileName = "journalapp-data.json";
Expand All @@ -23,6 +26,9 @@ public class BackupFile

public IReadOnlyCollection<PreferenceBackup> PreferenceBackups { get; set; }

/// <summary>
/// Reads a backup file from the specified stream.
/// </summary>
public static async Task<BackupFile> ReadArchive(Stream stream)
{
using var archive = new ZipArchive(stream, ZipArchiveMode.Read);
Expand All @@ -40,12 +46,18 @@ public static async Task<BackupFile> ReadArchive(Stream stream)
throw new InvalidOperationException("No valid backup found!");
}

/// <summary>
/// Reads a backup file from the specified path.
/// </summary>
public static async Task<BackupFile> ReadArchive(string path)
{
await using var fs = File.Open(path, FileMode.Open);
return await ReadArchive(fs);
}

/// <summary>
/// Writes the backup file to the specified stream.
/// </summary>
public async Task WriteArchive(Stream stream)
{
using var archive = new ZipArchive(stream, ZipArchiveMode.Create);
Expand All @@ -56,11 +68,17 @@ public async Task WriteArchive(Stream stream)
await JsonSerializer.SerializeAsync(entryStream, this, SerializerOptions);
}

/// <summary>
/// Writes the backup file to the specified path.
/// </summary>
public async Task WriteArchive(string path)
{
await using var stream = File.Create(path);
await WriteArchive(stream);
}
}

/// <summary>
/// Represents a preference.
/// </summary>
public record class PreferenceBackup(string Name, string Value);
54 changes: 54 additions & 0 deletions JournalApp/Data/DataPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,88 @@

namespace JournalApp;

/// <summary>
/// Represents a single data point recorded in the journal.
/// </summary>
public class DataPoint
{
/// <summary>
/// The unique identifier for the data point.
/// </summary>
[Key]
public Guid Guid { get; set; }

/// <summary>
/// The day to which the data point belongs.
/// </summary>
[JsonIgnore]
public virtual Day Day { get; set; }

/// <summary>
/// The category of the data point.
/// </summary>
[JsonIgnore]
public virtual DataPointCategory Category { get; set; }

/// <summary>
/// The creation timestamp of the data point.
/// </summary>
public DateTimeOffset CreatedAt { get; set; }

/// <summary>
/// The type of the data point.
/// </summary>
public PointType Type { get; set; }

/// <summary>
/// Indicates whether the data point is deleted.
/// </summary>
public bool Deleted { get; set; }

/// <summary>
/// The mood value of the data point, if applicable.
/// </summary>
public string Mood { get; set; }

/// <summary>
/// The sleep hours value of the data point, if applicable.
/// </summary>
public decimal? SleepHours { get; set; }

/// <summary>
/// The scale index value of the data point, if applicable.
/// </summary>
public int? ScaleIndex { get; set; }

/// <summary>
/// The boolean value of the data point, if applicable.
/// </summary>
public bool? Bool { get; set; }

/// <summary>
/// The numeric value of the data point, if applicable.
/// </summary>
public double? Number { get; set; }

/// <summary>
/// The text value of the data point, if applicable.
/// </summary>
public string Text { get; set; }

/// <summary>
/// The medication dose value of the data point, if applicable.
/// </summary>
public decimal? MedicationDose { get; set; }

/// <summary>
/// Indicates whether the data point is a timestamped note.
/// </summary>
[JsonIgnore]
public bool IsTimestampedNote => Category?.Group == "Notes";

/// <summary>
/// The list of predefined moods.
/// </summary>
[JsonIgnore]
public static IReadOnlyList<string> Moods { get; } = ["🤔", "🤩", "😀", "🙂", "😐", "😕", "😢", "😭"];

Expand Down
42 changes: 42 additions & 0 deletions JournalApp/Data/DataPointCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,75 @@

public class DataPointCategory
{
/// <summary>
/// The unique identifier for the category.
/// </summary>
[Key]
public Guid Guid { get; set; }

/// <summary>
/// The group to which the category belongs.
/// </summary>
public string Group { get; set; }

/// <summary>
/// The name of the category.
/// </summary>
public string Name { get; set; }

/// <summary>
/// The index of the category.
/// </summary>
public int Index { get; set; }

/// <summary>
/// Indicates whether the category is read-only.
/// </summary>
public bool ReadOnly { get; set; }

/// <summary>
/// Indicates whether the category is enabled.
/// </summary>
public bool Enabled { get; set; } = true;

/// <summary>
/// Indicates whether the category is deleted.
/// </summary>
public bool Deleted { get; set; }

/// <summary>
/// The type of data points in the category.
/// </summary>
public PointType Type { get; set; }

/// <summary>
/// The medication dose for the category, if applicable.
/// </summary>
public decimal? MedicationDose { get; set; }

/// <summary>
/// The unit of the medication dose, if applicable.
/// </summary>
public string MedicationUnit { get; set; }

/// <summary>
/// The date since the medication is taken every day, if applicable.
/// </summary>
public DateTimeOffset? MedicationEveryDaySince { get; set; }

/// <summary>
/// Additional details about the category.
/// </summary>
public string Details { get; set; }

/// <summary>
/// The collection of data points in the category.
/// </summary>
public virtual HashSet<DataPoint> Points { get; set; } = [];

/// <summary>
/// Indicates whether the category should be shown on a single line.
/// </summary>
public bool SingleLine => Type is PointType.Mood or PointType.Sleep or PointType.Scale or PointType.Number;

public override string ToString()
Expand Down
6 changes: 6 additions & 0 deletions JournalApp/Data/Day.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
namespace JournalApp;

/// <summary>
/// Represents a single day in the journal, containing data points.
/// </summary>
public class Day
{
/// <summary>
/// The unique identifier for the day.
/// </summary>
[Key]
public Guid Guid { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions JournalApp/Data/PointType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace JournalApp;

/// <summary>
/// Represents different types of data points that can be recorded in the journal.
/// </summary>
public enum PointType
{
None,
Expand Down

0 comments on commit d5b794f

Please sign in to comment.