starting lecture

This commit is contained in:
2024-11-01 14:01:19 -06:00
parent b7dd0ccab6
commit 859c9033f0
6 changed files with 91 additions and 1 deletions

View File

@@ -0,0 +1,62 @@
"use server";
import path from "path";
import { basePath } from "./utils/fileSystemUtils";
import fs from "fs/promises";
import { Lecture } from "@/models/local/lecture";
import { extractLabelValue } from "@/models/local/assignment/utils/markdownUtils";
export async function getLectures(courseName: string) {
const courseLectureRoot = path.join(basePath, courseName, "lectures");
if (!(await directoryExists(courseLectureRoot))) {
return [];
}
const entries = await fs.readdir(courseLectureRoot, { withFileTypes: true });
const lectureWeekFolders = entries
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);
const lecturesByWeek = await Promise.all(
lectureWeekFolders.map(async (weekName) => {
const weekBasePath = path.join(courseLectureRoot, weekName);
const fileNames = await fs.readdir(weekBasePath);
const lectures = await Promise.all(
fileNames.map(async (fileName) => {
const filePath = path.join(weekBasePath, fileName);
const fileContent = await fs.readFile(filePath, "utf-8");
const lecture = parseLecture(fileContent);
return lecture;
})
);
return {
weekName,
lectures,
};
})
);
return lecturesByWeek;
}
function parseLecture(fileContent: string): Lecture {
const settings = fileContent.split("---\n")[0];
const name = extractLabelValue(settings, "Name");
const date = extractLabelValue(settings, "Date");
const content = fileContent.split("---\n")[1].trim();
return {
name,
date,
content,
};
}
const directoryExists = async (path: string): Promise<boolean> => {
try {
const stat = await fs.stat(path);
return stat.isDirectory();
} catch {
return false;
}
};

View File

@@ -14,7 +14,9 @@ export const moduleFileStorageService = {
.map((dirent) => dirent.name);
const modules = await Promise.all(modulePromises);
return modules.sort((a, b) => a.localeCompare(b));
return modules
.filter((m) => m !== "lectures")
.sort((a, b) => a.localeCompare(b));
},
async createModule(courseName: string, moduleName: string) {
const courseDirectory = path.join(basePath, courseName);