diff --git a/Management.Test/Markdown/FileStorageTests.cs b/Management.Test/Markdown/FileStorageTests.cs index a6a8bc2..0fe39f8 100644 --- a/Management.Test/Markdown/FileStorageTests.cs +++ b/Management.Test/Markdown/FileStorageTests.cs @@ -9,7 +9,7 @@ public class FileStorageTests private FileStorageManager fileManager { get; set; } - private string setupTempDirectory() + private static string setupTempDirectory() { var tempDirectory = Path.GetTempPath(); var storageDirectory = tempDirectory + "fileStorageTests"; @@ -18,16 +18,12 @@ public class FileStorageTests Directory.CreateDirectory(storageDirectory); else { - var di = new DirectoryInfo(storageDirectory); + var dirInfo = new DirectoryInfo(storageDirectory); - foreach (FileInfo file in di.GetFiles()) - { + foreach (var file in dirInfo.GetFiles()) file.Delete(); - } - foreach (DirectoryInfo dir in di.GetDirectories()) - { + foreach (var dir in dirInfo.GetDirectories()) dir.Delete(true); - } } return storageDirectory; @@ -40,10 +36,12 @@ public class FileStorageTests var fileManagerLogger = new MyLogger(NullLogger.Instance); var markdownLoaderLogger = new MyLogger(NullLogger.Instance); + var markdownSaverLogger = new MyLogger(NullLogger.Instance); Environment.SetEnvironmentVariable("storageDirectory", storageDirectory); var markdownLoader = new CourseMarkdownLoader(markdownLoaderLogger); - fileManager = new FileStorageManager(fileManagerLogger, markdownLoader); + var markdownSaver = new MarkdownCourseSaver(markdownSaverLogger); + fileManager = new FileStorageManager(fileManagerLogger, markdownLoader, markdownSaver); } [Test] @@ -51,10 +49,7 @@ public class FileStorageTests { LocalCourse testCourse = new LocalCourse { - Settings = new() - { - Name = "test empty course", - }, + Settings = new() { Name = "test empty course" }, Modules = [] }; @@ -69,10 +64,8 @@ public class FileStorageTests [Test] public async Task CourseSettings_CanBeSavedAndLoaded() { - LocalCourse testCourse = new() - { - Settings = new() - { + LocalCourse testCourse = new() { + Settings = new() { AssignmentGroups = [], Name = "Test Course with settings", DaysOfWeek = [DayOfWeek.Monday, DayOfWeek.Wednesday], @@ -95,12 +88,8 @@ public class FileStorageTests [Test] public async Task EmptyCourseModules_CanBeSavedAndLoaded() { - LocalCourse testCourse = new() - { - Settings = new() - { - Name = "Test Course with modules", - }, + LocalCourse testCourse = new() { + Settings = new() { Name = "Test Course with modules" }, Modules = [ new() { Name="test module 1", @@ -121,12 +110,8 @@ public class FileStorageTests [Test] public async Task CourseModules_WithAssignments_CanBeSavedAndLoaded() { - LocalCourse testCourse = new() - { - Settings = new() - { - Name = "Test Course with modules and assignments", - }, + LocalCourse testCourse = new() { + Settings = new() { Name = "Test Course with modules and assignments" }, Modules = [ new() { Name="test module 1 with assignments", @@ -161,8 +146,7 @@ public class FileStorageTests [Test] public async Task CourseModules_WithQuizzes_CanBeSavedAndLoaded() { - LocalCourse testCourse = new() - { + LocalCourse testCourse = new() { Settings = new() { Name = "Test Course with modules and quiz" }, Modules = [ new() { @@ -202,7 +186,7 @@ public class FileStorageTests [Test] public async Task MarkdownStorage_FullyPopulated_DoesNotLoseData() { - LocalCourse testCourse = new (){ + LocalCourse testCourse = new() { Settings = new () { AssignmentGroups = [], Name = "Test Course with lots of data", diff --git a/Management.Web/Program.cs b/Management.Web/Program.cs index 0ff3675..4cb65f9 100644 --- a/Management.Web/Program.cs +++ b/Management.Web/Program.cs @@ -35,6 +35,7 @@ builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddLogging(); + builder.Services.AddScoped(typeof(MyLogger<>)); builder.Services.AddScoped(); @@ -45,7 +46,10 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/Management/Services/Files/FileStorageManager.cs b/Management/Services/Files/FileStorageManager.cs index d5ef579..5821350 100644 --- a/Management/Services/Files/FileStorageManager.cs +++ b/Management/Services/Files/FileStorageManager.cs @@ -6,15 +6,18 @@ public class FileStorageManager { private readonly MyLogger logger; private readonly CourseMarkdownLoader _courseMarkdownLoader; + private readonly MarkdownCourseSaver _saveMarkdownCourse; private readonly string _basePath; public FileStorageManager( MyLogger logger, - CourseMarkdownLoader courseMarkdownLoader + CourseMarkdownLoader courseMarkdownLoader, + MarkdownCourseSaver saveMarkdownCourse ) { this.logger = logger; _courseMarkdownLoader = courseMarkdownLoader; + _saveMarkdownCourse = saveMarkdownCourse; _basePath = FileConfiguration.GetBasePath(); logger.Log("Using storage directory: " + _basePath); @@ -41,126 +44,11 @@ public class FileStorageManager public async Task SaveCourseAsync(LocalCourse course) { var courseString = CourseToYaml(course); - - var courseDirectory = $"{_basePath}/{course.Settings.Name}"; - if (!Directory.Exists(courseDirectory)) - Directory.CreateDirectory(courseDirectory); - - await saveModules(course); - await File.WriteAllTextAsync($"{_basePath}/{course.Settings.Name}.yml", courseString); + + await _saveMarkdownCourse.Save(course); } - private async Task saveModules(LocalCourse course) - { - var courseDirectory = $"{_basePath}/{course.Settings.Name}"; - - await saveSettings(course, courseDirectory); - foreach (var module in course.Modules) - { - var moduleDirectory = courseDirectory + "/" + module.Name; - if (!Directory.Exists(moduleDirectory)) - Directory.CreateDirectory(moduleDirectory); - - await saveQuizzes(course, module); - await saveAssignments(course, module); - } - - var moduleNames = course.Modules.Select(m => m.Name); - foreach (var moduleDirectoryPath in Directory.EnumerateDirectories(courseDirectory)) - { - var directoryName = Path.GetFileName(moduleDirectoryPath); - if (!moduleNames.Contains(directoryName)) - { - Console.WriteLine($"deleting extra module directory, it was probably renamed {moduleDirectoryPath}"); - Directory.Delete(moduleDirectoryPath, true); - } - } - } - - private static async Task saveSettings(LocalCourse course, string courseDirectory) - { - var settingsFilePath = courseDirectory + "/settings.yml"; ; - var settingsYaml = course.Settings.ToYaml(); - await File.WriteAllTextAsync(settingsFilePath, settingsYaml); - } - - private async Task saveQuizzes(LocalCourse course, LocalModule module) - { - var quizzesDirectory = $"{_basePath}/{course.Settings.Name}/{module.Name}/quizzes"; - if (!Directory.Exists(quizzesDirectory)) - Directory.CreateDirectory(quizzesDirectory); - - - foreach (var quiz in module.Quizzes) - { - var markdownPath = quizzesDirectory + "/" + quiz.Name + ".md"; ; - var quizMarkdown = quiz.ToMarkdown(); - await File.WriteAllTextAsync(markdownPath, quizMarkdown); - } - removeOldQuizzes(quizzesDirectory, module); - } - - private void removeOldQuizzes(string path, LocalModule module) - { - var existingFiles = Directory.EnumerateFiles(path); - - var filesToDelete = existingFiles.Where((f) => - { - foreach (var quiz in module.Quizzes) - { - var markdownPath = path + "/" + quiz.Name + ".md"; - if (f == markdownPath) - return false; - } - return true; - }); - - foreach (var file in filesToDelete) - { - logger.Log($"removing old quiz, it has probably been renamed {file}"); - File.Delete(file); - } - - } - - private async Task saveAssignments(LocalCourse course, LocalModule module) - { - var assignmentsDirectory = $"{_basePath}/{course.Settings.Name}/{module.Name}/assignments"; - if (!Directory.Exists(assignmentsDirectory)) - Directory.CreateDirectory(assignmentsDirectory); - - foreach (var assignment in module.Assignments) - { - var assignmentMarkdown = assignment.ToMarkdown(); - - var filePath = assignmentsDirectory + "/" + assignment.Name + ".md"; - 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> LoadSavedCourses() { @@ -170,7 +58,7 @@ public class FileStorageManager var courses = await Task.WhenAll( fileNames .Where(name => name.EndsWith(".yml")) - .Select(async n => ParseCourse(await File.ReadAllTextAsync($"{_basePath}/{n}"))) + .Select(async n => ParseCourse(await File.ReadAllTextAsync(n))) ); return courses; } diff --git a/Management/Services/Files/LoadMarkdownCourse.cs b/Management/Services/Files/LoadMarkdownCourse.cs index 91fe2e1..a63937c 100644 --- a/Management/Services/Files/LoadMarkdownCourse.cs +++ b/Management/Services/Files/LoadMarkdownCourse.cs @@ -1,7 +1,5 @@ -using System.Reflection.Metadata.Ecma335; using LocalModels; using Management.Services; -using YamlDotNet.Serialization; public class CourseMarkdownLoader { @@ -41,8 +39,6 @@ public class CourseMarkdownLoader Settings = settings, Modules = modules }; - - } private async Task loadCourseSettings(string courseDirectory) diff --git a/Management/Services/Files/SaveMarkdownCourse.cs b/Management/Services/Files/SaveMarkdownCourse.cs new file mode 100644 index 0000000..f97a79c --- /dev/null +++ b/Management/Services/Files/SaveMarkdownCourse.cs @@ -0,0 +1,132 @@ +using LocalModels; +using Management.Services; + +public class MarkdownCourseSaver +{ + private readonly MyLogger logger; + private readonly string _basePath; + + public MarkdownCourseSaver(MyLogger logger) + { + this.logger = logger; + _basePath = FileConfiguration.GetBasePath(); + } + + public async Task Save(LocalCourse course) + { + var courseDirectory = $"{_basePath}/{course.Settings.Name}"; + if (!Directory.Exists(courseDirectory)) + Directory.CreateDirectory(courseDirectory); + await saveSettings(course, courseDirectory); + await saveModules(course, courseDirectory); + } + + private async Task saveModules(LocalCourse course, string courseDirectory) + { + foreach (var module in course.Modules) + { + var moduleDirectory = courseDirectory + "/" + module.Name; + if (!Directory.Exists(moduleDirectory)) + Directory.CreateDirectory(moduleDirectory); + + await saveQuizzes(course, module); + await saveAssignments(course, module); + } + + var moduleNames = course.Modules.Select(m => m.Name); + foreach (var moduleDirectoryPath in Directory.EnumerateDirectories(courseDirectory)) + { + var directoryName = Path.GetFileName(moduleDirectoryPath); + if (!moduleNames.Contains(directoryName)) + { + Console.WriteLine($"deleting extra module directory, it was probably renamed {moduleDirectoryPath}"); + Directory.Delete(moduleDirectoryPath, true); + } + } + } + + private static async Task saveSettings(LocalCourse course, string courseDirectory) + { + var settingsFilePath = courseDirectory + "/settings.yml"; ; + var settingsYaml = course.Settings.ToYaml(); + await File.WriteAllTextAsync(settingsFilePath, settingsYaml); + } + + private async Task saveQuizzes(LocalCourse course, LocalModule module) + { + var quizzesDirectory = $"{_basePath}/{course.Settings.Name}/{module.Name}/quizzes"; + if (!Directory.Exists(quizzesDirectory)) + Directory.CreateDirectory(quizzesDirectory); + + + foreach (var quiz in module.Quizzes) + { + var markdownPath = quizzesDirectory + "/" + quiz.Name + ".md"; ; + var quizMarkdown = quiz.ToMarkdown(); + await File.WriteAllTextAsync(markdownPath, quizMarkdown); + } + removeOldQuizzes(quizzesDirectory, module); + } + + private void removeOldQuizzes(string path, LocalModule module) + { + var existingFiles = Directory.EnumerateFiles(path); + + var filesToDelete = existingFiles.Where((f) => + { + foreach (var quiz in module.Quizzes) + { + var markdownPath = path + "/" + quiz.Name + ".md"; + if (f == markdownPath) + return false; + } + return true; + }); + + foreach (var file in filesToDelete) + { + logger.Log($"removing old quiz, it has probably been renamed {file}"); + File.Delete(file); + } + + } + + private async Task saveAssignments(LocalCourse course, LocalModule module) + { + var assignmentsDirectory = $"{_basePath}/{course.Settings.Name}/{module.Name}/assignments"; + if (!Directory.Exists(assignmentsDirectory)) + Directory.CreateDirectory(assignmentsDirectory); + + foreach (var assignment in module.Assignments) + { + var assignmentMarkdown = assignment.ToMarkdown(); + + var filePath = assignmentsDirectory + "/" + assignment.Name + ".md"; + 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); + } + } + +} \ No newline at end of file