only initialize courses in directories that exist

This commit is contained in:
2023-12-04 16:49:20 -07:00
parent b0cf5d8189
commit 209e8f991f
4 changed files with 67 additions and 88 deletions

View File

@@ -21,46 +21,16 @@ public class FileStorageManager
_basePath = FileConfiguration.GetBasePath();
this.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);
// await File.WriteAllTextAsync($"{_basePath}/{course.Settings.Name}.yml", courseString);
await _saveMarkdownCourse.Save(course);
}
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(n)))
// );
// return courses;
return await LoadSavedMarkdownCourses();
}
@@ -69,4 +39,15 @@ public class FileStorageManager
return await _courseMarkdownLoader.LoadSavedMarkdownCourses();
}
public async Task<IEnumerable<string>> GetEmptyDirectories()
{
if(!Directory.Exists(_basePath))
throw new DirectoryNotFoundException($"Cannot get empty directories, {_basePath} does not exist");
return Directory
.GetDirectories(_basePath, "*")
.Where(dir => !Directory.EnumerateFileSystemEntries(dir).Any())
.ToArray();
}
}

View File

@@ -17,7 +17,13 @@ public class CourseMarkdownLoader
var courseDirectories = Directory.GetDirectories(_basePath);
var courses = await Task.WhenAll(
courseDirectories.Select(async n => await LoadCourseByPath(n))
courseDirectories
.Where(d =>
{
var settingsPath = $"{d}/settings.yml";
return File.Exists(settingsPath);
})
.Select(async d => await LoadCourseByPath(d))
);
return courses;
}
@@ -31,10 +37,13 @@ public class CourseMarkdownLoader
throw new LoadCourseFromFileException(errorMessage);
}
LocalCourseSettings settings = await loadCourseSettings(courseDirectory);
var modules = await loadCourseModules(courseDirectory);
return new() {
return new()
{
Settings = settings,
Modules = modules
};