removed assignment id from edit and delete workflow

This commit is contained in:
2023-10-24 14:33:22 -06:00
parent 5280d49523
commit 8342e9cdd3
6 changed files with 55 additions and 17 deletions

View File

@@ -0,0 +1,11 @@
public class AssignmentMarkdownTests
{
[Test]
public void TestCanParseAssignmentSettings()
{
var assignmentMarkdown = @"
";
}
}

View File

@@ -19,7 +19,6 @@ public class QuizQuestionMarkdownTests
{ {
new LocalQuizQuestion() new LocalQuizQuestion()
{ {
Id = "someid",
Points = 2, Points = 2,
Text = @"`some type` of question Text = @"`some type` of question
@@ -75,7 +74,6 @@ endline
{ {
new() new()
{ {
Id = "somesdid",
Text = "oneline question", Text = "oneline question",
Points = 1, Points = 1,
QuestionType = QuestionType.MULTIPLE_ANSWERS, QuestionType = QuestionType.MULTIPLE_ANSWERS,

View File

@@ -61,16 +61,14 @@
.LocalCourse .LocalCourse
.Modules .Modules
.First(m => .First(m =>
m.Assignments m.Assignments.Contains(assignment)
.Select(a => a.Id)
.Contains(assignment.Id)
) ?? throw new Exception("handling assignment delete, could not find module"); ) ?? throw new Exception("handling assignment delete, could not find module");
var newModules = planner.LocalCourse.Modules.Select(m => var newModules = planner.LocalCourse.Modules.Select(m =>
m.Name == currentModule.Name m.Name == currentModule.Name
? m with ? m with
{ {
Assignments = m.Assignments.Where(a => a.Id != assignment.Id).ToArray() Assignments = m.Assignments.Where(a => a != assignment).ToArray()
} }
: m : m
).ToArray(); ).ToArray();

View File

@@ -26,9 +26,10 @@ public class AssignmentEditorContext
{ {
if (planner.LocalCourse != null) if (planner.LocalCourse != null)
{ {
// run discovery on Assignment, it was the last stored version of the assignment
var currentModule = var currentModule =
planner.LocalCourse.Modules.First( planner.LocalCourse.Modules.First(
m => m.Assignments.Select(a => a.Id).Contains(newAssignment.Id) m => m.Assignments.Contains(Assignment)
) ?? throw new Exception("could not find current module in assignment editor context"); ) ?? throw new Exception("could not find current module in assignment editor context");
var updatedModules = planner.LocalCourse.Modules var updatedModules = planner.LocalCourse.Modules
@@ -38,7 +39,7 @@ public class AssignmentEditorContext
? currentModule with ? currentModule with
{ {
Assignments = currentModule.Assignments Assignments = currentModule.Assignments
.Select(a => a.Id == newAssignment.Id ? newAssignment : a) .Select(a => a == Assignment ? newAssignment : a)
.ToArray() .ToArray()
} }
: m : m

View File

@@ -105,4 +105,17 @@ public record LocalAssignment
var yaml = serializer.Serialize(this); var yaml = serializer.Serialize(this);
return yaml; return yaml;
} }
public string ToMarkdown()
{
var assignmentYaml = ToYaml();
var assignmentMarkdown =
"```yaml" + Environment.NewLine
+ assignmentYaml
+ "```" + Environment.NewLine
+ "<!-- assignment markdown below -->" + Environment.NewLine
+ Description;
return assignmentMarkdown;
}
} }

View File

@@ -96,7 +96,7 @@ public class FileStorageManager
return true; return true;
}); });
foreach(var file in filesToDelete) foreach (var file in filesToDelete)
{ {
logger.Log($"removing old quiz, it has probably been renamed {file}"); logger.Log($"removing old quiz, it has probably been renamed {file}");
File.Delete(file); File.Delete(file);
@@ -104,7 +104,7 @@ public class FileStorageManager
} }
private static async Task saveAssignments(LocalCourse course, LocalModule module) private async Task saveAssignments(LocalCourse course, LocalModule module)
{ {
var assignmentsDirectory = $"../storage/{course.Settings.Name}/{module.Name}/assignments"; var assignmentsDirectory = $"../storage/{course.Settings.Name}/{module.Name}/assignments";
if (!Directory.Exists(assignmentsDirectory)) if (!Directory.Exists(assignmentsDirectory))
@@ -112,17 +112,34 @@ public class FileStorageManager
foreach (var assignment in module.Assignments) foreach (var assignment in module.Assignments)
{ {
var assignmentYaml = assignment.ToYaml(); var assignmentMarkdown = assignment.ToMarkdown();
var assignmentMarkdown =
"```yaml" + Environment.NewLine
+ assignmentYaml
+ "```" + Environment.NewLine
+ "<!-- assignment markdown below -->" + Environment.NewLine
+ assignment.Description;
var filePath = assignmentsDirectory + "/" + assignment.Name + ".md"; var filePath = assignmentsDirectory + "/" + assignment.Name + ".md";
await File.WriteAllTextAsync(filePath, assignmentMarkdown); await File.WriteAllTextAsync(filePath, assignmentMarkdown);
} }
removeOldAssignments(assignmentsDirectory, module);
}
private void removeOldAssignments(string path, LocalModule module)
{
var existingFiles = Directory.EnumerateFiles(path);
var filesToDelete = existingFiles.Where((f) =>
{
foreach (var assignment in module.Assignments)
{
var markdownPath = path + "/" + assignment.Name + ".md";
if (f == markdownPath)
return false;
}
return true;
});
foreach (var file in filesToDelete)
{
logger.Log($"removing old assignment, it has probably been renamed {file}");
File.Delete(file);
}
} }
public async Task<IEnumerable<LocalCourse>> LoadSavedCourses() public async Task<IEnumerable<LocalCourse>> LoadSavedCourses()