mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
some minor refactoring
This commit is contained in:
@@ -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,17 +18,13 @@ 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<FileStorageManager>(NullLogger<FileStorageManager>.Instance);
|
||||
var markdownLoaderLogger = new MyLogger<CourseMarkdownLoader>(NullLogger<CourseMarkdownLoader>.Instance);
|
||||
var markdownSaverLogger = new MyLogger<MarkdownCourseSaver>(NullLogger<MarkdownCourseSaver>.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() {
|
||||
|
||||
@@ -35,6 +35,7 @@ builder.Services.AddRazorPages();
|
||||
builder.Services.AddServerSideBlazor();
|
||||
|
||||
builder.Services.AddLogging();
|
||||
|
||||
builder.Services.AddScoped(typeof(MyLogger<>));
|
||||
|
||||
builder.Services.AddScoped<IWebRequestor, WebRequestor>();
|
||||
@@ -45,7 +46,10 @@ builder.Services.AddScoped<CanvasQuizService>();
|
||||
builder.Services.AddScoped<CanvasModuleService>();
|
||||
builder.Services.AddScoped<CanvasService, CanvasService>();
|
||||
|
||||
builder.Services.AddScoped<MarkdownCourseSaver>();
|
||||
builder.Services.AddScoped<CourseMarkdownLoader>();
|
||||
builder.Services.AddScoped<FileStorageManager>();
|
||||
|
||||
builder.Services.AddScoped<CoursePlanner>();
|
||||
builder.Services.AddScoped<AssignmentEditorContext>();
|
||||
builder.Services.AddScoped<QuizEditorContext>();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
132
Management/Services/Files/SaveMarkdownCourse.cs
Normal file
132
Management/Services/Files/SaveMarkdownCourse.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user