more code refactor to colocate feature code

This commit is contained in:
2025-07-23 11:25:12 -06:00
parent c37ad0708e
commit 815f929c2d
30 changed files with 535 additions and 646 deletions

View File

@@ -1,8 +1,15 @@
import publicProcedure from "../../../services/serverFunctions/publicProcedure";
import { z } from "zod";
import { router } from "../../../services/serverFunctions/trpcSetup";
import { fileStorageService } from "@/features/local/utils/fileStorageService";
import { zodLocalAssignment } from "@/features/local/assignments/models/localAssignment";
import {
LocalAssignment,
zodLocalAssignment,
} from "@/features/local/assignments/models/localAssignment";
import path from "path";
import { getCoursePathByName } from "../globalSettings/globalSettingsFileStorageService";
import { promises as fs } from "fs";
import { courseItemFileStorageService } from "../course/courseItemFileStorageService";
import { assignmentMarkdownSerializer } from "./models/utils/assignmentMarkdownSerializer";
export const assignmentRouter = router({
getAssignment: publicProcedure
@@ -14,13 +21,12 @@ export const assignmentRouter = router({
})
)
.query(async ({ input: { courseName, moduleName, assignmentName } }) => {
const assignment = await fileStorageService.assignments.getAssignment(
return await courseItemFileStorageService.getItem({
courseName,
moduleName,
assignmentName
);
// console.log(assignment);
return assignment;
name: assignmentName,
type: "Assignment",
});
}),
getAllAssignments: publicProcedure
.input(
@@ -30,10 +36,11 @@ export const assignmentRouter = router({
})
)
.query(async ({ input: { courseName, moduleName } }) => {
const assignments = await fileStorageService.assignments.getAssignments(
const assignments = await courseItemFileStorageService.getItems({
courseName,
moduleName
);
moduleName,
type: "Assignment",
});
return assignments;
}),
createAssignment: publicProcedure
@@ -49,7 +56,7 @@ export const assignmentRouter = router({
async ({
input: { courseName, moduleName, assignmentName, assignment },
}) => {
await fileStorageService.assignments.updateOrCreateAssignment({
await updateOrCreateAssignmentFile({
courseName,
moduleName,
assignmentName,
@@ -79,7 +86,7 @@ export const assignmentRouter = router({
previousAssignmentName,
},
}) => {
await fileStorageService.assignments.updateOrCreateAssignment({
await updateOrCreateAssignmentFile({
courseName,
moduleName,
assignmentName,
@@ -90,7 +97,7 @@ export const assignmentRouter = router({
assignmentName !== previousAssignmentName ||
moduleName !== previousModuleName
) {
await fileStorageService.assignments.delete({
await deleteAssignment({
courseName,
moduleName: previousModuleName,
assignmentName: previousAssignmentName,
@@ -107,10 +114,59 @@ export const assignmentRouter = router({
})
)
.mutation(async ({ input: { courseName, moduleName, assignmentName } }) => {
await fileStorageService.assignments.delete({
await deleteAssignment({
courseName,
moduleName,
assignmentName,
});
}),
});
export async function updateOrCreateAssignmentFile({
courseName,
moduleName,
assignmentName,
assignment,
}: {
courseName: string;
moduleName: string;
assignmentName: string;
assignment: LocalAssignment;
}) {
const courseDirectory = await getCoursePathByName(courseName);
const folder = path.join(courseDirectory, moduleName, "assignments");
await fs.mkdir(folder, { recursive: true });
const filePath = path.join(
courseDirectory,
moduleName,
"assignments",
assignmentName + ".md"
);
const assignmentMarkdown =
assignmentMarkdownSerializer.toMarkdown(assignment);
console.log(`Saving assignment ${filePath}`);
await fs.writeFile(filePath, assignmentMarkdown);
}
async function deleteAssignment({
courseName,
moduleName,
assignmentName,
}: {
courseName: string;
moduleName: string;
assignmentName: string;
}) {
const courseDirectory = await getCoursePathByName(courseName);
const filePath = path.join(
courseDirectory,
moduleName,
"assignments",
assignmentName + ".md"
);
console.log("removing assignment", filePath);
await fs.unlink(filePath);
}

View File

@@ -1,100 +0,0 @@
import {
localAssignmentMarkdown,
LocalAssignment,
} from "@/features/local/assignments/models/localAssignment";
import { assignmentMarkdownSerializer } from "@/features/local/assignments/models/utils/assignmentMarkdownSerializer";
import path from "path";
import { promises as fs } from "fs";
import { courseItemFileStorageService } from "@/features/local/course/courseItemFileStorageService";
import { getCoursePathByName } from "@/features/local/globalSettings/globalSettingsFileStorageService";
import { directoryOrFileExists } from "@/features/local/utils/fileSystemUtils";
const getAssignmentNames = async (courseName: string, moduleName: string) => {
const courseDirectory = await getCoursePathByName(courseName);
const filePath = path.join(courseDirectory, moduleName, "assignments");
if (!(await directoryOrFileExists(filePath))) {
console.log(
`Error loading course by name, assignments folder does not exist in ${filePath}`
);
// await fs.mkdir(filePath);
return [];
}
const assignmentFiles = await fs.readdir(filePath);
return assignmentFiles.map((f) => f.replace(/\.md$/, ""));
};
const getAssignment = async (
courseName: string,
moduleName: string,
assignmentName: string
) => {
const courseDirectory = await getCoursePathByName(courseName);
const filePath = path.join(
courseDirectory,
moduleName,
"assignments",
assignmentName + ".md"
);
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(/\r\n/g, "\n");
return localAssignmentMarkdown.parseMarkdown(rawFile, assignmentName);
};
export const assignmentsFileStorageService = {
getAssignmentNames,
getAssignment,
async getAssignments(courseName: string, moduleName: string) {
return await courseItemFileStorageService.getItems(
courseName,
moduleName,
"Assignment"
);
},
async updateOrCreateAssignment({
courseName,
moduleName,
assignmentName,
assignment,
}: {
courseName: string;
moduleName: string;
assignmentName: string;
assignment: LocalAssignment;
}) {
const courseDirectory = await getCoursePathByName(courseName);
const folder = path.join(courseDirectory, moduleName, "assignments");
await fs.mkdir(folder, { recursive: true });
const filePath = path.join(
courseDirectory,
moduleName,
"assignments",
assignmentName + ".md"
);
const assignmentMarkdown =
assignmentMarkdownSerializer.toMarkdown(assignment);
console.log(`Saving assignment ${filePath}`);
await fs.writeFile(filePath, assignmentMarkdown);
},
async delete({
courseName,
moduleName,
assignmentName,
}: {
courseName: string;
moduleName: string;
assignmentName: string;
}) {
const courseDirectory = await getCoursePathByName(courseName);
const filePath = path.join(
courseDirectory,
moduleName,
"assignments",
assignmentName + ".md"
);
console.log("removing assignment", filePath);
await fs.unlink(filePath);
},
};