mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
testing markdown storage and retrieval
This commit is contained in:
17
Management/Services/Files/FileConfiguration.cs
Normal file
17
Management/Services/Files/FileConfiguration.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Management.Services;
|
||||
|
||||
public class FileConfiguration
|
||||
{
|
||||
|
||||
public static string GetBasePath()
|
||||
{
|
||||
string? storageDirectory = Environment.GetEnvironmentVariable("storageDirectory");
|
||||
var basePath = storageDirectory ?? Path.GetFullPath("../storage");
|
||||
|
||||
if (!Directory.Exists(basePath))
|
||||
throw new Exception("storage folder not found");
|
||||
|
||||
return basePath;
|
||||
|
||||
}
|
||||
}
|
||||
183
Management/Services/Files/FileStorageManager.cs
Normal file
183
Management/Services/Files/FileStorageManager.cs
Normal file
@@ -0,0 +1,183 @@
|
||||
using LocalModels;
|
||||
using Management.Services;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
public class FileStorageManager
|
||||
{
|
||||
private readonly MyLogger<FileStorageManager> logger;
|
||||
private readonly CourseMarkdownLoader _courseMarkdownLoader;
|
||||
private readonly string _basePath;
|
||||
|
||||
public FileStorageManager(
|
||||
MyLogger<FileStorageManager> logger,
|
||||
CourseMarkdownLoader courseMarkdownLoader
|
||||
)
|
||||
{
|
||||
this.logger = logger;
|
||||
_courseMarkdownLoader = courseMarkdownLoader;
|
||||
_basePath = FileConfiguration.GetBasePath();
|
||||
|
||||
logger.Log("Using storage directory: " + _basePath);
|
||||
|
||||
}
|
||||
|
||||
public string CourseToYaml(LocalCourse course)
|
||||
{
|
||||
var serializer = new SerializerBuilder().DisableAliases().Build();
|
||||
|
||||
var yaml = serializer.Serialize(course);
|
||||
|
||||
return yaml;
|
||||
}
|
||||
|
||||
public LocalCourse ParseCourse(string rawCourse)
|
||||
{
|
||||
var deserializer = new DeserializerBuilder().IgnoreUnmatchedProperties().Build();
|
||||
|
||||
var course = deserializer.Deserialize<LocalCourse>(rawCourse);
|
||||
return course;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
|
||||
var fileNames = Directory.GetFiles(_basePath);
|
||||
|
||||
var courses = await Task.WhenAll(
|
||||
fileNames
|
||||
.Where(name => name.EndsWith(".yml"))
|
||||
.Select(async n => ParseCourse(await File.ReadAllTextAsync($"{_basePath}/{n}")))
|
||||
);
|
||||
return courses;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<LocalCourse>> LoadSavedMarkdownCourses()
|
||||
{
|
||||
return await _courseMarkdownLoader.LoadSavedMarkdownCourses();
|
||||
}
|
||||
|
||||
}
|
||||
3
Management/Services/Files/LoadCourseFromFileException.cs
Normal file
3
Management/Services/Files/LoadCourseFromFileException.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
public class LoadCourseFromFileException(string message) : Exception(message)
|
||||
{
|
||||
}
|
||||
127
Management/Services/Files/LoadMarkdownCourse.cs
Normal file
127
Management/Services/Files/LoadMarkdownCourse.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using LocalModels;
|
||||
using Management.Services;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
public class CourseMarkdownLoader
|
||||
{
|
||||
private readonly MyLogger<CourseMarkdownLoader> logger;
|
||||
private readonly string _basePath;
|
||||
|
||||
public CourseMarkdownLoader(MyLogger<CourseMarkdownLoader> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
_basePath = FileConfiguration.GetBasePath();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<LocalCourse>> LoadSavedMarkdownCourses()
|
||||
{
|
||||
var courseDirectories = Directory.GetDirectories(_basePath);
|
||||
|
||||
var courses = await Task.WhenAll(
|
||||
courseDirectories.Select(async n => await LoadCourseByPath(n))
|
||||
);
|
||||
return courses;
|
||||
}
|
||||
|
||||
public async Task<LocalCourse> LoadCourseByPath(string courseDirectory)
|
||||
{
|
||||
if (!Directory.Exists(courseDirectory))
|
||||
{
|
||||
var errorMessage = $"error loading course by name, could not find folder {courseDirectory}";
|
||||
logger.Log(errorMessage);
|
||||
throw new LoadCourseFromFileException(errorMessage);
|
||||
}
|
||||
|
||||
LocalCourseSettings settings = await loadCourseSettings(courseDirectory);
|
||||
var modules = await loadCourseModules(courseDirectory);
|
||||
|
||||
return new()
|
||||
{
|
||||
Settings = settings,
|
||||
Modules = modules
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
private async Task<LocalCourseSettings> loadCourseSettings(string courseDirectory)
|
||||
{
|
||||
var settingsPath = $"{courseDirectory}/settings.yml";
|
||||
if (!File.Exists(settingsPath))
|
||||
{
|
||||
var errorMessage = $"error loading course by name, settings file {settingsPath}";
|
||||
logger.Log(errorMessage);
|
||||
throw new LoadCourseFromFileException(errorMessage);
|
||||
}
|
||||
|
||||
var settingsString = await File.ReadAllTextAsync(settingsPath);
|
||||
var settings = LocalCourseSettings.ParseYaml(settingsString);
|
||||
return settings;
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<LocalModule>> loadCourseModules(string courseDirectory)
|
||||
{
|
||||
var modulePaths = Directory.GetDirectories(courseDirectory);
|
||||
var modules = await Task.WhenAll(
|
||||
modulePaths
|
||||
.Select(loadModuleFromPath)
|
||||
);
|
||||
return modules;
|
||||
}
|
||||
|
||||
private async Task<LocalModule> loadModuleFromPath(string modulePath)
|
||||
{
|
||||
var moduleName = Path.GetFileName(modulePath);
|
||||
var assignments = await loadAssignmentsFromPath(modulePath);
|
||||
var quizzes = await loadQuizzesFromPath(modulePath);
|
||||
|
||||
return new LocalModule()
|
||||
{
|
||||
Name = moduleName,
|
||||
Assignments = assignments,
|
||||
Quizzes = quizzes,
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<LocalAssignment>> loadAssignmentsFromPath(string modulePath)
|
||||
{
|
||||
var assignmentsPath = $"{modulePath}/assignments";
|
||||
if (!Directory.Exists(assignmentsPath))
|
||||
{
|
||||
var errorMessage = $"error loading course by name, assignments folder does not exist in {modulePath}";
|
||||
logger.Log(errorMessage);
|
||||
throw new LoadCourseFromFileException(errorMessage);
|
||||
}
|
||||
var assignmentFiles = Directory.GetFiles(assignmentsPath);
|
||||
var assignmentPromises = assignmentFiles
|
||||
.Select(async filePath =>
|
||||
{
|
||||
var rawFile = await File.ReadAllTextAsync(filePath);
|
||||
return LocalAssignment.ParseMarkdown(rawFile);
|
||||
})
|
||||
.ToArray();
|
||||
return await Task.WhenAll(assignmentPromises);
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<LocalQuiz>> loadQuizzesFromPath(string modulePath)
|
||||
{
|
||||
var quizzesPath = $"{modulePath}/quizzes";
|
||||
if (!Directory.Exists(quizzesPath))
|
||||
{
|
||||
var errorMessage = $"error loading course by name, quizzes folder does not exist in {modulePath}";
|
||||
logger.Log(errorMessage);
|
||||
throw new LoadCourseFromFileException(errorMessage);
|
||||
}
|
||||
|
||||
var quizFiles = Directory.GetFiles(quizzesPath);
|
||||
var quizPromises = quizFiles
|
||||
.Select(async path =>
|
||||
{
|
||||
var rawQuiz = await File.ReadAllTextAsync(path);
|
||||
return LocalQuiz.ParseMarkdown(rawQuiz);
|
||||
});
|
||||
|
||||
return await Task.WhenAll(quizPromises);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user