mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 15:48:32 -06:00
testing markdown storage and retrieval
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
namespace LocalModels;
|
||||
|
||||
public static class AssignmentSubmissionType
|
||||
{
|
||||
public static readonly string ONLINE_TEXT_ENTRY = "online_text_entry";
|
||||
public static readonly string ONLINE_UPLOAD = "online_upload";
|
||||
public static readonly string ONLINE_QUIZ = "online_quiz";
|
||||
// public static readonly string ON_PAPER = "on_paper";
|
||||
public static readonly string DISCUSSION_TOPIC = "discussion_topic";
|
||||
// public static readonly string EXTERNAL_TOOL = "external_tool";
|
||||
public static readonly string ONLINE_URL = "online_url";
|
||||
// public static readonly string MEDIA_RECORDING = "media_recording";
|
||||
// public static readonly string STUDENT_ANNOTATION = "student_annotation";
|
||||
public static readonly string NONE = "none";
|
||||
public static readonly IEnumerable<string> AllTypes = new string[]
|
||||
{
|
||||
ONLINE_TEXT_ENTRY,
|
||||
ONLINE_UPLOAD,
|
||||
ONLINE_QUIZ,
|
||||
// ON_PAPER,
|
||||
DISCUSSION_TOPIC,
|
||||
// EXTERNAL_TOOL,
|
||||
ONLINE_URL,
|
||||
// MEDIA_RECORDING,
|
||||
// STUDENT_ANNOTATION,
|
||||
NONE,
|
||||
};
|
||||
}
|
||||
@@ -6,41 +6,6 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace LocalModels;
|
||||
|
||||
public record RubricItem
|
||||
{
|
||||
public static readonly string extraCredit = "(Extra Credit) ";
|
||||
public required string Label { get; set; }
|
||||
public required int Points { get; set; }
|
||||
public bool IsExtraCredit => Label.Contains(extraCredit.ToLower(), StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
|
||||
public static class AssignmentSubmissionType
|
||||
{
|
||||
public static readonly string ONLINE_TEXT_ENTRY = "online_text_entry";
|
||||
public static readonly string ONLINE_UPLOAD = "online_upload";
|
||||
public static readonly string ONLINE_QUIZ = "online_quiz";
|
||||
// public static readonly string ON_PAPER = "on_paper";
|
||||
public static readonly string DISCUSSION_TOPIC = "discussion_topic";
|
||||
// public static readonly string EXTERNAL_TOOL = "external_tool";
|
||||
public static readonly string ONLINE_URL = "online_url";
|
||||
// public static readonly string MEDIA_RECORDING = "media_recording";
|
||||
// public static readonly string STUDENT_ANNOTATION = "student_annotation";
|
||||
public static readonly string NONE = "none";
|
||||
public static readonly IEnumerable<string> AllTypes = new string[]
|
||||
{
|
||||
ONLINE_TEXT_ENTRY,
|
||||
ONLINE_UPLOAD,
|
||||
ONLINE_QUIZ,
|
||||
// ON_PAPER,
|
||||
DISCUSSION_TOPIC,
|
||||
// EXTERNAL_TOOL,
|
||||
ONLINE_URL,
|
||||
// MEDIA_RECORDING,
|
||||
// STUDENT_ANNOTATION,
|
||||
NONE,
|
||||
};
|
||||
}
|
||||
|
||||
public record LocalAssignment
|
||||
{
|
||||
// public ulong? CanvasId { get; init; } = null;
|
||||
9
Management/Models/Local/Assignment/RubricItem.cs
Normal file
9
Management/Models/Local/Assignment/RubricItem.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace LocalModels;
|
||||
|
||||
public record RubricItem
|
||||
{
|
||||
public static readonly string extraCredit = "(Extra Credit) ";
|
||||
public required string Label { get; set; }
|
||||
public required int Points { get; set; }
|
||||
public bool IsExtraCredit => Label.Contains(extraCredit.ToLower(), StringComparison.CurrentCultureIgnoreCase);
|
||||
}
|
||||
@@ -18,8 +18,6 @@ public record LocalCourseSettings
|
||||
public DateTime StartDate { get; init; }
|
||||
public DateTime EndDate { get; init; }
|
||||
public SimpleTimeOnly DefaultDueTime { get; init; } = new SimpleTimeOnly();
|
||||
public IEnumerable<AssignmentTemplate> AssignmentTemplates { get; init; } =
|
||||
Enumerable.Empty<AssignmentTemplate>();
|
||||
|
||||
public string ToYaml()
|
||||
{
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,22 @@ using YamlDotNet.Serialization;
|
||||
public class FileStorageManager
|
||||
{
|
||||
private readonly MyLogger<FileStorageManager> logger;
|
||||
private static readonly string _basePath = "../storage";
|
||||
private readonly CourseMarkdownLoader _courseMarkdownLoader;
|
||||
private readonly string _basePath;
|
||||
|
||||
public FileStorageManager(MyLogger<FileStorageManager> logger)
|
||||
public FileStorageManager(
|
||||
MyLogger<FileStorageManager> logger,
|
||||
CourseMarkdownLoader courseMarkdownLoader
|
||||
)
|
||||
{
|
||||
if (!Directory.Exists(_basePath))
|
||||
throw new Exception("storage folder not found");
|
||||
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();
|
||||
@@ -131,6 +139,7 @@ public class FileStorageManager
|
||||
}
|
||||
removeOldAssignments(assignmentsDirectory, module);
|
||||
}
|
||||
|
||||
private void removeOldAssignments(string path, LocalModule module)
|
||||
{
|
||||
var existingFiles = Directory.EnumerateFiles(path);
|
||||
@@ -166,68 +175,9 @@ public class FileStorageManager
|
||||
return courses;
|
||||
}
|
||||
|
||||
// public async Task<LocalCourse> LoadCourseByName(string courseName)
|
||||
// {
|
||||
// var courseDirectory = $"{_basePath}/{courseName}";
|
||||
// if (!Directory.Exists(courseDirectory))
|
||||
// {
|
||||
// var errorMessage = $"error loading course by name, could not find folder {courseDirectory}";
|
||||
// logger.Log(errorMessage);
|
||||
// throw new LoadCourseFromFileException(errorMessage);
|
||||
// }
|
||||
// var settingsPath = $"{courseDirectory}/settings.yml";
|
||||
// if (!Directory.Exists(settingsPath))
|
||||
// {
|
||||
// var errorMessage = $"error loading course by name, settings file {settingsPath}";
|
||||
// logger.Log(errorMessage);
|
||||
// throw new LoadCourseFromFileException(errorMessage);
|
||||
// }
|
||||
public async Task<IEnumerable<LocalCourse>> LoadSavedMarkdownCourses()
|
||||
{
|
||||
return await _courseMarkdownLoader.LoadSavedMarkdownCourses();
|
||||
}
|
||||
|
||||
// var settingsString = await File.ReadAllTextAsync(settingsPath);
|
||||
// var settings = LocalCourseSettings.ParseYaml(settingsString);
|
||||
|
||||
// var modulePaths = Directory.GetDirectories(courseDirectory);
|
||||
// var modules = modulePaths
|
||||
// .Select(LoadModuleFromPath)
|
||||
// .ToArray();
|
||||
|
||||
// }
|
||||
|
||||
// public async Task<LocalModule> LoadModuleFromPath(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 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 assignments = LoadAssignmentsFromPath(assignmentsPath);
|
||||
// var quizzes = LoadQuizzesFromPath(quizzesPath);
|
||||
|
||||
|
||||
// }
|
||||
// public async Task<IEnumerable<LocalAssignment>> LoadAssignmentsFromPath(string assignmentsFolder)
|
||||
// {
|
||||
|
||||
// }
|
||||
// public async Task<IEnumerable<LocalAssignment>> LoadQuizzesFromPath(string quizzesFolder)
|
||||
// {
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
public class LoadCourseFromFileException(string message) : Exception(message)
|
||||
{
|
||||
}
|
||||
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