working on difference calculations

This commit is contained in:
2024-05-03 21:33:06 -06:00
parent 26bf2bbbd1
commit 5c931af141
6 changed files with 309 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
namespace LocalModels;
public record LocalAssignment : IModuleItem
public sealed record LocalAssignment : IModuleItem
{
private string _name = "";
public string Name
@@ -37,4 +37,13 @@ public record LocalAssignment : IModuleItem
public string RubricToMarkdown() => this.AssignmentRubricToMarkdown();
public static LocalAssignment ParseMarkdown(string input) => LocalAssignmentMarkdownParser.ParseMarkdown(input);
public static IEnumerable<RubricItem> ParseRubricMarkdown(string rawMarkdown) => LocalAssignmentMarkdownParser.ParseRubricMarkdown(rawMarkdown);
public bool Equals(LocalAssignment? otherAssignment)
{
return ToMarkdown() == otherAssignment?.ToMarkdown();
}
public override int GetHashCode() => ToMarkdown().GetHashCode();
}

View File

@@ -3,7 +3,7 @@ namespace LocalModels;
public record LocalCourse
{
public IEnumerable<LocalModule> Modules { get; init; } = Enumerable.Empty<LocalModule>();
public required LocalCourseSettings Settings { get; set; }
public required LocalCourseSettings Settings { get; init; }
}
public record SimpleTimeOnly

View File

@@ -1,6 +1,6 @@
namespace LocalModels;
public record LocalModule
public sealed record LocalModule
{
public string Name { get; init; } = string.Empty;
public string Notes { get; set; } = string.Empty;
@@ -8,10 +8,57 @@ public record LocalModule
public IEnumerable<LocalQuiz> Quizzes { get; init; } = [];
public IEnumerable<LocalCoursePage> Pages { get; init; } = [];
public IEnumerable<IModuleItem> SortedModuleItems =>
public IEnumerable<IModuleItem> GetSortedModuleItems() =>
Enumerable.Empty<IModuleItem>()
.Concat(Assignments)
.Concat(Quizzes)
.Concat(Pages)
.OrderBy(i => i.DueAt);
public bool Equals(LocalModule? otherModule)
{
var areEqual =
string.Equals(Name, otherModule?.Name, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Notes, otherModule?.Notes, StringComparison.OrdinalIgnoreCase)
&& CompareCollections(Assignments.OrderBy(x => x.Name), otherModule?.Assignments.OrderBy(x => x.Name))
&& CompareCollections(Quizzes.OrderBy(x => x.Name), otherModule?.Quizzes.OrderBy(x => x.Name))
&& CompareCollections(Pages.OrderBy(x => x.Name), otherModule?.Pages.OrderBy(x => x.Name));
return areEqual;
}
private static bool CompareCollections<T>(IEnumerable<T> first, IEnumerable<T>? second)
{
var firstList = first.ToList();
var secondList = second?.ToList();
if (firstList.Count != secondList?.Count)
return false;
for (int i = 0; i < firstList.Count; i++)
{
if (!Equals(firstList[i], secondList[i]))
return false;
}
return true;
}
public override int GetHashCode()
{
HashCode hash = new HashCode();
hash.Add(Name, StringComparer.OrdinalIgnoreCase);
hash.Add(Notes, StringComparer.OrdinalIgnoreCase);
AddRangeToHash(hash, Assignments.OrderBy(x => x.Name));
AddRangeToHash(hash, Quizzes.OrderBy(x => x.Name));
AddRangeToHash(hash, Pages.OrderBy(x => x.Name));
return hash.ToHashCode();
}
private void AddRangeToHash<T>(HashCode hash, IEnumerable<T> items)
{
foreach (var item in items)
{
hash.Add(item);
}
}
}

View File

@@ -0,0 +1,40 @@
using LocalModels;
public static class CourseDifferences
{
public static NewCourseChanges GetNewChanges(LocalCourse newCourse, LocalCourse oldCourse)
{
if (newCourse == oldCourse)
return new NewCourseChanges();
var differentModules = newCourse.Modules
.Where(newModule =>
!oldCourse.Modules.Any(oldModule => oldModule.Equals(newModule))
)
.Select(newModule =>
{
var oldModule = oldCourse.Modules.FirstOrDefault(m => m.Name == newModule.Name);
if (oldModule == null)
return newModule;
var newAssignments = newModule.Assignments.Where(
newAssignment => !oldModule.Assignments.Any(oldAssignment => newAssignment == oldAssignment)
);
return newModule with { Assignments = newAssignments };
})
.ToArray();
return new NewCourseChanges
{
Settings = newCourse.Settings,
Modules = differentModules,
};
}
}
public record NewCourseChanges
{
public IEnumerable<LocalModule> Modules { get; init; } = [];
public LocalCourseSettings? Settings { get; init; }
}