mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 07:38:33 -06:00
moving data management to lots of queries
This commit is contained in:
@@ -11,6 +11,9 @@ import {
|
||||
directoryOrFileExists,
|
||||
hasFileSystemEntries,
|
||||
} from "./utils/fileSystemUtils";
|
||||
import { localAssignmentMarkdown } from "@/models/local/assignmnet/localAssignment";
|
||||
import { localQuizMarkdownUtils } from "@/models/local/quiz/localQuiz";
|
||||
import { localPageMarkdownUtils } from "@/models/local/page/localCoursePage";
|
||||
|
||||
const basePath = process.env.STORAGE_DIRECTORY ?? "./storage";
|
||||
console.log("base path", basePath);
|
||||
@@ -71,14 +74,98 @@ export const fileStorageService = {
|
||||
|
||||
const modulePromises = moduleDirectories
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) =>
|
||||
dirent.name
|
||||
);
|
||||
.map((dirent) => dirent.name);
|
||||
|
||||
const modules = await Promise.all(modulePromises);
|
||||
return modules.sort((a, b) => a.localeCompare(b));
|
||||
},
|
||||
|
||||
async getAssignmentNames(courseName: string, moduleName: string) {
|
||||
const filePath = path.join(basePath, courseName, moduleName, "assignments");
|
||||
if (!(await directoryOrFileExists(filePath))) {
|
||||
console.log(
|
||||
`Error loading course by name, assignments folder does not exist in ${filePath}`
|
||||
);
|
||||
await fs.mkdir(filePath);
|
||||
}
|
||||
|
||||
const assignmentFiles = await fs.readdir(filePath);
|
||||
return assignmentFiles;
|
||||
},
|
||||
async getQuizNames(courseName: string, moduleName: string) {
|
||||
const filePath = path.join(basePath, courseName, moduleName, "quizzes");
|
||||
if (!(await directoryOrFileExists(filePath))) {
|
||||
console.log(
|
||||
`Error loading course by name, quiz folder does not exist in ${filePath}`
|
||||
);
|
||||
await fs.mkdir(filePath);
|
||||
}
|
||||
|
||||
const files = await fs.readdir(filePath);
|
||||
return files;
|
||||
},
|
||||
async getPageNames(courseName: string, moduleName: string) {
|
||||
const filePath = path.join(basePath, courseName, moduleName, "pages");
|
||||
if (!(await directoryOrFileExists(filePath))) {
|
||||
console.log(
|
||||
`Error loading course by name, pages folder does not exist in ${filePath}`
|
||||
);
|
||||
await fs.mkdir(filePath);
|
||||
}
|
||||
|
||||
const files = await fs.readdir(filePath);
|
||||
return files;
|
||||
},
|
||||
|
||||
async getAssignment(
|
||||
courseName: string,
|
||||
moduleName: string,
|
||||
assignmentName: string
|
||||
) {
|
||||
const filePath = path.join(
|
||||
basePath,
|
||||
courseName,
|
||||
moduleName,
|
||||
"assignments",
|
||||
assignmentName + ".md"
|
||||
);
|
||||
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
||||
/\r\n/g,
|
||||
"\n"
|
||||
);
|
||||
return localAssignmentMarkdown.parseMarkdown(rawFile);
|
||||
},
|
||||
|
||||
async getQuiz(courseName: string, moduleName: string, quizName: string) {
|
||||
const filePath = path.join(
|
||||
basePath,
|
||||
courseName,
|
||||
moduleName,
|
||||
"quizzes",
|
||||
quizName + ".md"
|
||||
);
|
||||
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
||||
/\r\n/g,
|
||||
"\n"
|
||||
);
|
||||
return localQuizMarkdownUtils.parseMarkdown(rawFile);
|
||||
},
|
||||
|
||||
async getPage(courseName: string, moduleName: string, pageName: string) {
|
||||
const filePath = path.join(
|
||||
basePath,
|
||||
courseName,
|
||||
moduleName,
|
||||
"pages",
|
||||
pageName + ".md"
|
||||
);
|
||||
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
||||
/\r\n/g,
|
||||
"\n"
|
||||
);
|
||||
return localPageMarkdownUtils.parseMarkdown(rawFile);
|
||||
},
|
||||
|
||||
async getEmptyDirectories(): Promise<string[]> {
|
||||
if (!(await directoryOrFileExists(basePath))) {
|
||||
throw new Error(
|
||||
|
||||
@@ -62,116 +62,116 @@ export const courseMarkdownLoader = {
|
||||
// };
|
||||
// },
|
||||
|
||||
async loadCourseSettings(
|
||||
courseDirectory: string
|
||||
): Promise<LocalCourseSettings> {
|
||||
const settingsPath = path.join(courseDirectory, "settings.yml");
|
||||
if (!(await directoryOrFileExists(settingsPath))) {
|
||||
const errorMessage = `Error loading course by name, settings file ${settingsPath}`;
|
||||
console.log(errorMessage);
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
// async loadCourseSettings(
|
||||
// courseDirectory: string
|
||||
// ): Promise<LocalCourseSettings> {
|
||||
// const settingsPath = path.join(courseDirectory, "settings.yml");
|
||||
// if (!(await directoryOrFileExists(settingsPath))) {
|
||||
// const errorMessage = `Error loading course by name, settings file ${settingsPath}`;
|
||||
// console.log(errorMessage);
|
||||
// throw new Error(errorMessage);
|
||||
// }
|
||||
|
||||
const settingsString = await fs.readFile(settingsPath, "utf-8");
|
||||
const settings = localCourseYamlUtils.parseSettingYaml(settingsString);
|
||||
// const settingsString = await fs.readFile(settingsPath, "utf-8");
|
||||
// const settings = localCourseYamlUtils.parseSettingYaml(settingsString);
|
||||
|
||||
const folderName = path.basename(courseDirectory);
|
||||
return { ...settings, name: folderName };
|
||||
},
|
||||
// const folderName = path.basename(courseDirectory);
|
||||
// return { ...settings, name: folderName };
|
||||
// },
|
||||
|
||||
async loadCourseModules(courseDirectory: string): Promise<LocalModule[]> {
|
||||
const moduleDirectories = await fs.readdir(courseDirectory, {
|
||||
withFileTypes: true,
|
||||
});
|
||||
const modulePromises = moduleDirectories
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) =>
|
||||
this.loadModuleFromPath(path.join(courseDirectory, dirent.name))
|
||||
);
|
||||
// async loadCourseModules(courseDirectory: string): Promise<LocalModule[]> {
|
||||
// const moduleDirectories = await fs.readdir(courseDirectory, {
|
||||
// withFileTypes: true,
|
||||
// });
|
||||
// const modulePromises = moduleDirectories
|
||||
// .filter((dirent) => dirent.isDirectory())
|
||||
// .map((dirent) =>
|
||||
// this.loadModuleFromPath(path.join(courseDirectory, dirent.name))
|
||||
// );
|
||||
|
||||
const modules = await Promise.all(modulePromises);
|
||||
return modules.sort((a, b) => a.name.localeCompare(b.name));
|
||||
},
|
||||
// const modules = await Promise.all(modulePromises);
|
||||
// return modules.sort((a, b) => a.name.localeCompare(b.name));
|
||||
// },
|
||||
|
||||
async loadModuleFromPath(modulePath: string): Promise<LocalModule> {
|
||||
const moduleName = path.basename(modulePath);
|
||||
const assignments = await this.loadAssignmentsFromPath(modulePath);
|
||||
const quizzes = await this.loadQuizzesFromPath(modulePath);
|
||||
const pages = await this.loadModulePagesFromPath(modulePath);
|
||||
// async loadModuleFromPath(modulePath: string): Promise<LocalModule> {
|
||||
// const moduleName = path.basename(modulePath);
|
||||
// const assignments = await this.loadAssignmentsFromPath(modulePath);
|
||||
// const quizzes = await this.loadQuizzesFromPath(modulePath);
|
||||
// const pages = await this.loadModulePagesFromPath(modulePath);
|
||||
|
||||
return {
|
||||
name: moduleName,
|
||||
assignments,
|
||||
quizzes,
|
||||
pages,
|
||||
};
|
||||
},
|
||||
// return {
|
||||
// name: moduleName,
|
||||
// assignments,
|
||||
// quizzes,
|
||||
// pages,
|
||||
// };
|
||||
// },
|
||||
|
||||
async loadAssignmentsFromPath(
|
||||
modulePath: string
|
||||
): Promise<LocalAssignment[]> {
|
||||
const assignmentsPath = path.join(modulePath, "assignments");
|
||||
if (!(await directoryOrFileExists(assignmentsPath))) {
|
||||
console.log(
|
||||
`Error loading course by name, assignments folder does not exist in ${modulePath}`
|
||||
);
|
||||
await fs.mkdir(assignmentsPath);
|
||||
}
|
||||
// async loadAssignmentsFromPath(
|
||||
// modulePath: string
|
||||
// ): Promise<LocalAssignment[]> {
|
||||
// const assignmentsPath = path.join(modulePath, "assignments");
|
||||
// if (!(await directoryOrFileExists(assignmentsPath))) {
|
||||
// console.log(
|
||||
// `Error loading course by name, assignments folder does not exist in ${modulePath}`
|
||||
// );
|
||||
// await fs.mkdir(assignmentsPath);
|
||||
// }
|
||||
|
||||
const assignmentFiles = await fs.readdir(assignmentsPath);
|
||||
const assignmentPromises = assignmentFiles.map(async (file) => {
|
||||
const filePath = path.join(assignmentsPath, file);
|
||||
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
||||
/\r\n/g,
|
||||
"\n"
|
||||
);
|
||||
return localAssignmentMarkdown.parseMarkdown(rawFile);
|
||||
});
|
||||
// const assignmentFiles = await fs.readdir(assignmentsPath);
|
||||
// const assignmentPromises = assignmentFiles.map(async (file) => {
|
||||
// const filePath = path.join(assignmentsPath, file);
|
||||
// const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
||||
// /\r\n/g,
|
||||
// "\n"
|
||||
// );
|
||||
// return localAssignmentMarkdown.parseMarkdown(rawFile);
|
||||
// });
|
||||
|
||||
return await Promise.all(assignmentPromises);
|
||||
},
|
||||
// return await Promise.all(assignmentPromises);
|
||||
// },
|
||||
|
||||
async loadQuizzesFromPath(modulePath: string): Promise<LocalQuiz[]> {
|
||||
const quizzesPath = path.join(modulePath, "quizzes");
|
||||
if (!(await directoryOrFileExists(quizzesPath))) {
|
||||
console.log(
|
||||
`Quizzes folder does not exist in ${modulePath}, creating now`
|
||||
);
|
||||
await fs.mkdir(quizzesPath);
|
||||
}
|
||||
// async loadQuizzesFromPath(modulePath: string): Promise<LocalQuiz[]> {
|
||||
// const quizzesPath = path.join(modulePath, "quizzes");
|
||||
// if (!(await directoryOrFileExists(quizzesPath))) {
|
||||
// console.log(
|
||||
// `Quizzes folder does not exist in ${modulePath}, creating now`
|
||||
// );
|
||||
// await fs.mkdir(quizzesPath);
|
||||
// }
|
||||
|
||||
const quizFiles = await fs.readdir(quizzesPath);
|
||||
const quizPromises = quizFiles.map(async (file) => {
|
||||
const filePath = path.join(quizzesPath, file);
|
||||
const rawQuiz = (await fs.readFile(filePath, "utf-8")).replace(
|
||||
/\r\n/g,
|
||||
"\n"
|
||||
);
|
||||
return localQuizMarkdownUtils.parseMarkdown(rawQuiz);
|
||||
});
|
||||
// const quizFiles = await fs.readdir(quizzesPath);
|
||||
// const quizPromises = quizFiles.map(async (file) => {
|
||||
// const filePath = path.join(quizzesPath, file);
|
||||
// const rawQuiz = (await fs.readFile(filePath, "utf-8")).replace(
|
||||
// /\r\n/g,
|
||||
// "\n"
|
||||
// );
|
||||
// return localQuizMarkdownUtils.parseMarkdown(rawQuiz);
|
||||
// });
|
||||
|
||||
return await Promise.all(quizPromises);
|
||||
},
|
||||
// return await Promise.all(quizPromises);
|
||||
// },
|
||||
|
||||
async loadModulePagesFromPath(
|
||||
modulePath: string
|
||||
): Promise<LocalCoursePage[]> {
|
||||
const pagesPath = path.join(modulePath, "pages");
|
||||
if (!(await directoryOrFileExists(pagesPath))) {
|
||||
console.log(`Pages folder does not exist in ${modulePath}, creating now`);
|
||||
await fs.mkdir(pagesPath);
|
||||
}
|
||||
// async loadModulePagesFromPath(
|
||||
// modulePath: string
|
||||
// ): Promise<LocalCoursePage[]> {
|
||||
// const pagesPath = path.join(modulePath, "pages");
|
||||
// if (!(await directoryOrFileExists(pagesPath))) {
|
||||
// console.log(`Pages folder does not exist in ${modulePath}, creating now`);
|
||||
// await fs.mkdir(pagesPath);
|
||||
// }
|
||||
|
||||
const pageFiles = await fs.readdir(pagesPath);
|
||||
const pagePromises = pageFiles.map(async (file) => {
|
||||
const filePath = path.join(pagesPath, file);
|
||||
const rawPage = (await fs.readFile(filePath, "utf-8")).replace(
|
||||
/\r\n/g,
|
||||
"\n"
|
||||
);
|
||||
return localPageMarkdownUtils.parseMarkdown(rawPage);
|
||||
});
|
||||
// const pageFiles = await fs.readdir(pagesPath);
|
||||
// const pagePromises = pageFiles.map(async (file) => {
|
||||
// const filePath = path.join(pagesPath, file);
|
||||
// const rawPage = (await fs.readFile(filePath, "utf-8")).replace(
|
||||
// /\r\n/g,
|
||||
// "\n"
|
||||
// );
|
||||
// return localPageMarkdownUtils.parseMarkdown(rawPage);
|
||||
// });
|
||||
|
||||
return await Promise.all(pagePromises);
|
||||
},
|
||||
// return await Promise.all(pagePromises);
|
||||
// },
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user