mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 07:38:33 -06:00
more code refactor to colocate feature code
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user