mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
tests allow for pages to be stored and retrieved
This commit is contained in:
@@ -30,6 +30,8 @@ public class FileStorageManager
|
||||
}
|
||||
public async Task SaveCourseAsync(LocalCourse course, LocalCourse? previouslyStoredCourse)
|
||||
{
|
||||
using var activity = DiagnosticsConfig.Source.StartActivity("Saving Course");
|
||||
activity?.AddTag("CourseName", course.Settings.Name);
|
||||
await _saveMarkdownCourse.Save(course, previouslyStoredCourse);
|
||||
}
|
||||
|
||||
|
||||
@@ -79,12 +79,14 @@ public class CourseMarkdownLoader
|
||||
var moduleName = Path.GetFileName(modulePath);
|
||||
var assignments = await loadAssignmentsFromPath(modulePath);
|
||||
var quizzes = await loadQuizzesFromPath(modulePath);
|
||||
var pages = await loadPagesFromPath(modulePath);
|
||||
|
||||
return new LocalModule()
|
||||
{
|
||||
Name = moduleName,
|
||||
Assignments = assignments,
|
||||
Quizzes = quizzes,
|
||||
Pages = pages,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -127,4 +129,23 @@ public class CourseMarkdownLoader
|
||||
|
||||
return await Task.WhenAll(quizPromises);
|
||||
}
|
||||
private async Task<IEnumerable<LocalCoursePage>> loadPagesFromPath(string modulePath)
|
||||
{
|
||||
var pagesPath = $"{modulePath}/pages";
|
||||
if (!Directory.Exists(pagesPath))
|
||||
{
|
||||
logger.Log($"pages folder does not exist in {modulePath}, creating now");
|
||||
Directory.CreateDirectory(pagesPath);
|
||||
}
|
||||
|
||||
var pageFiles = Directory.GetFiles(pagesPath);
|
||||
var pagePromises = pageFiles
|
||||
.Select(async path =>
|
||||
{
|
||||
var rawPage = (await File.ReadAllTextAsync(path)).Replace("\r\n", "\n");
|
||||
return LocalCoursePage.ParseMarkdown(rawPage);
|
||||
});
|
||||
|
||||
return await Task.WhenAll(pagePromises);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ public class MarkdownCourseSaver(MyLogger<MarkdownCourseSaver> logger, FileConfi
|
||||
|
||||
await saveQuizzes(course, module, previouslyStoredCourse);
|
||||
await saveAssignments(course, module, previouslyStoredCourse);
|
||||
await savePages(course, module, previouslyStoredCourse);
|
||||
}
|
||||
|
||||
var moduleNames = course.Modules.Select(m => m.Name);
|
||||
@@ -42,7 +43,7 @@ public class MarkdownCourseSaver(MyLogger<MarkdownCourseSaver> logger, FileConfi
|
||||
|
||||
private static async Task saveSettings(LocalCourse course, string courseDirectory)
|
||||
{
|
||||
var settingsFilePath = courseDirectory + "/settings.yml"; ;
|
||||
var settingsFilePath = courseDirectory + "/settings.yml";
|
||||
var settingsYaml = course.Settings.ToYaml();
|
||||
await File.WriteAllTextAsync(settingsFilePath, settingsYaml);
|
||||
}
|
||||
@@ -140,4 +141,54 @@ public class MarkdownCourseSaver(MyLogger<MarkdownCourseSaver> logger, FileConfi
|
||||
}
|
||||
}
|
||||
|
||||
private async Task savePages(LocalCourse course, LocalModule module, LocalCourse? previouslyStoredCourse)
|
||||
{
|
||||
var pagesDirectory = $"{_basePath}/{course.Settings.Name}/{module.Name}/pages";
|
||||
if (!Directory.Exists(pagesDirectory))
|
||||
Directory.CreateDirectory(pagesDirectory);
|
||||
|
||||
|
||||
foreach (var page in module.Pages)
|
||||
{
|
||||
var previousModule = previouslyStoredCourse?.Modules.FirstOrDefault(m => m.Name == module.Name);
|
||||
var previousPage = previousModule?.Pages.FirstOrDefault(a => a == page);
|
||||
|
||||
if (previousPage == null)
|
||||
{
|
||||
var assignmentMarkdown = page.ToMarkdown();
|
||||
|
||||
var filePath = pagesDirectory + "/" + page.Name + ".md";
|
||||
using var activity = DiagnosticsConfig.Source.StartActivity("saving page in module");
|
||||
activity?.AddTag("PageName", page.Name);
|
||||
_logger.Log("saving page " + filePath);
|
||||
await File.WriteAllTextAsync(filePath, assignmentMarkdown);
|
||||
}
|
||||
}
|
||||
removeOldPages(pagesDirectory, module);
|
||||
}
|
||||
|
||||
private void removeOldPages(string path, LocalModule module)
|
||||
{
|
||||
var existingFiles = Directory.EnumerateFiles(path);
|
||||
|
||||
var filesToDelete = existingFiles.Where((f) =>
|
||||
{
|
||||
foreach (var page in module.Pages)
|
||||
{
|
||||
var markdownPath = path + "/" + page.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}");
|
||||
using var activity = DiagnosticsConfig.Source.StartActivity("removing untracked page from module");
|
||||
activity?.AddTag("FileName", file);
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user