some minor refactoring

This commit is contained in:
2023-12-04 12:39:15 -07:00
parent c6ce528ba1
commit 6b3e0bbae1
5 changed files with 159 additions and 155 deletions

View File

@@ -6,15 +6,18 @@ public class FileStorageManager
{
private readonly MyLogger<FileStorageManager> logger;
private readonly CourseMarkdownLoader _courseMarkdownLoader;
private readonly MarkdownCourseSaver _saveMarkdownCourse;
private readonly string _basePath;
public FileStorageManager(
MyLogger<FileStorageManager> 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<IEnumerable<LocalCourse>> 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;
}

View File

@@ -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<LocalCourseSettings> loadCourseSettings(string courseDirectory)

View File

@@ -0,0 +1,132 @@
using LocalModels;
using Management.Services;
public class MarkdownCourseSaver
{
private readonly MyLogger<MarkdownCourseSaver> logger;
private readonly string _basePath;
public MarkdownCourseSaver(MyLogger<MarkdownCourseSaver> 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);
}
}
}