mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
workign on api errors
This commit is contained in:
@@ -1,28 +1,33 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function GET(
|
||||
export const GET = async (
|
||||
_request: Request,
|
||||
{
|
||||
params: { courseName, moduleName, assignmentName },
|
||||
}: {
|
||||
params: { courseName: string; moduleName: string; assignmentName: string };
|
||||
}
|
||||
) {
|
||||
) =>
|
||||
await withErrorHandling(async () => {
|
||||
const settings = await fileStorageService.getAssignment(
|
||||
courseName,
|
||||
moduleName,
|
||||
assignmentName
|
||||
);
|
||||
return Response.json(settings);
|
||||
}
|
||||
});
|
||||
|
||||
export async function PUT(
|
||||
export const PUT = async (
|
||||
request: Request,
|
||||
{
|
||||
params: { courseName, moduleName, assignmentName },
|
||||
}: { params: { courseName: string; moduleName: string; assignmentName: string } }
|
||||
) {
|
||||
const assignment = await request.json()
|
||||
}: {
|
||||
params: { courseName: string; moduleName: string; assignmentName: string };
|
||||
}
|
||||
) =>
|
||||
await withErrorHandling(async () => {
|
||||
const assignment = await request.json();
|
||||
await fileStorageService.updateAssignment(
|
||||
courseName,
|
||||
moduleName,
|
||||
@@ -30,4 +35,4 @@ export async function PUT(
|
||||
assignment
|
||||
);
|
||||
return Response.json({});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function GET(
|
||||
export const GET = async (
|
||||
_request: Request,
|
||||
{
|
||||
params: { courseName, moduleName },
|
||||
}: { params: { courseName: string; moduleName: string } }
|
||||
) {
|
||||
) => await withErrorHandling(async () => {
|
||||
const settings = await fileStorageService.getAssignmentNames(
|
||||
courseName,
|
||||
moduleName
|
||||
);
|
||||
return Response.json(settings);
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,31 +1,29 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function GET(
|
||||
export const GET = async (
|
||||
_request: Request,
|
||||
{
|
||||
params: { courseName, moduleName, pageName },
|
||||
}: { params: { courseName: string; moduleName: string; pageName: string } }
|
||||
) {
|
||||
) =>
|
||||
await withErrorHandling(async () => {
|
||||
const settings = await fileStorageService.getPage(
|
||||
courseName,
|
||||
moduleName,
|
||||
pageName
|
||||
);
|
||||
return Response.json(settings);
|
||||
}
|
||||
});
|
||||
|
||||
export async function PUT(
|
||||
export const PUT = async (
|
||||
request: Request,
|
||||
{
|
||||
params: { courseName, moduleName, pageName },
|
||||
}: { params: { courseName: string; moduleName: string; pageName: string } }
|
||||
) {
|
||||
const page = await request.json()
|
||||
await fileStorageService.updatePage(
|
||||
courseName,
|
||||
moduleName,
|
||||
pageName,
|
||||
page
|
||||
);
|
||||
) =>
|
||||
await withErrorHandling(async () => {
|
||||
const page = await request.json();
|
||||
await fileStorageService.updatePage(courseName, moduleName, pageName, page);
|
||||
return Response.json({});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function GET(
|
||||
export const GET = async (
|
||||
_request: Request,
|
||||
{
|
||||
params: { courseName, moduleName },
|
||||
}: { params: { courseName: string; moduleName: string } }
|
||||
) {
|
||||
) =>
|
||||
await withErrorHandling(async () => {
|
||||
const settings = await fileStorageService.getPageNames(
|
||||
courseName,
|
||||
moduleName
|
||||
);
|
||||
return Response.json(settings);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function GET(
|
||||
export const GET = async (
|
||||
_request: Request,
|
||||
{
|
||||
params: { courseName, moduleName, quizName },
|
||||
}: { params: { courseName: string; moduleName: string; quizName: string } }
|
||||
) {
|
||||
) => await withErrorHandling(async () => {
|
||||
const quiz = await fileStorageService.getQuiz(
|
||||
courseName,
|
||||
moduleName,
|
||||
quizName
|
||||
);
|
||||
return Response.json(quiz);
|
||||
}
|
||||
})
|
||||
|
||||
export async function PUT(
|
||||
export const PUT = async (
|
||||
request: Request,
|
||||
{
|
||||
params: { courseName, moduleName, quizName },
|
||||
}: { params: { courseName: string; moduleName: string; quizName: string } }
|
||||
) {
|
||||
) => await withErrorHandling(async () => {
|
||||
const quiz = await request.json()
|
||||
await fileStorageService.updateQuiz(
|
||||
courseName,
|
||||
@@ -28,4 +29,4 @@ export async function PUT(
|
||||
quiz
|
||||
);
|
||||
return Response.json({});
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function GET(
|
||||
export const GET = async (
|
||||
_request: Request,
|
||||
{
|
||||
params: { courseName, moduleName },
|
||||
}: { params: { courseName: string; moduleName: string } }
|
||||
) {
|
||||
) =>
|
||||
await withErrorHandling(async () => {
|
||||
const settings = await fileStorageService.getQuizNames(
|
||||
courseName,
|
||||
moduleName
|
||||
);
|
||||
return Response.json(settings);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function GET(
|
||||
export const GET = async (
|
||||
_request: Request,
|
||||
{ params: { courseName } }: { params: { courseName: string } }
|
||||
) {
|
||||
const settings = await fileStorageService.getModuleNames(courseName)
|
||||
) =>
|
||||
await withErrorHandling(async () => {
|
||||
const settings = await fileStorageService.getModuleNames(courseName);
|
||||
return Response.json(settings);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function PUT(
|
||||
export const PUT = async (
|
||||
request: Request,
|
||||
{ params: { courseName } }: { params: { courseName: string } }
|
||||
) {
|
||||
) =>
|
||||
await withErrorHandling(async () => {
|
||||
const { updatedCourse, previousCourse } = await request.json();
|
||||
|
||||
console.log(updatedCourse);
|
||||
@@ -11,4 +13,4 @@ export async function PUT(
|
||||
|
||||
// await fileStorageService.saveCourseAsync(updatedCourse, previousCourse);
|
||||
return Response.json({});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function GET(
|
||||
export const GET = async (
|
||||
_request: Request,
|
||||
{ params: { courseName } }: { params: { courseName: string } }
|
||||
) {
|
||||
) =>
|
||||
await withErrorHandling(async () => {
|
||||
if (courseName.includes(".js.map")) {
|
||||
return Response.json({});
|
||||
}
|
||||
const settings = await fileStorageService.getCourseSettings(courseName);
|
||||
return Response.json(settings);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
import { withErrorHandling } from "@/services/withErrorHandling";
|
||||
|
||||
export async function GET() {
|
||||
export const GET = async () =>
|
||||
await withErrorHandling(async () => {
|
||||
const courses = await fileStorageService.getCourseNames();
|
||||
|
||||
return Response.json(courses);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@ import { useDraggingContext } from "../context/draggingContext";
|
||||
import { useCalendarItemsContext } from "../context/calendarItemsContext";
|
||||
import { useCourseContext } from "../context/courseContext";
|
||||
import Link from "next/link";
|
||||
import { IModuleItem } from "@/models/local/IModuleItem";
|
||||
|
||||
export default function Day({ day, month }: { day: string; month: number }) {
|
||||
const dayAsDate = getDateFromStringOrThrow(
|
||||
@@ -18,27 +19,88 @@ export default function Day({ day, month }: { day: string; month: number }) {
|
||||
|
||||
const itemsContext = useCalendarItemsContext();
|
||||
const { itemDrop } = useDraggingContext();
|
||||
const { courseName } = useCourseContext();
|
||||
|
||||
const dateKey = getDateOnlyMarkdownString(dayAsDate);
|
||||
const todaysModules = itemsContext[dateKey];
|
||||
|
||||
const todaysAssignments = todaysModules
|
||||
? Object.keys(todaysModules).flatMap((moduleName) =>
|
||||
todaysModules[moduleName].assignments.map((assignment) => ({
|
||||
moduleName,
|
||||
assignment,
|
||||
}))
|
||||
)
|
||||
: [];
|
||||
|
||||
const todaysQuizzes = todaysModules
|
||||
? Object.keys(todaysModules).flatMap((moduleName) =>
|
||||
todaysModules[moduleName].quizzes.map((quiz) => ({
|
||||
moduleName,
|
||||
quiz,
|
||||
}))
|
||||
)
|
||||
: [];
|
||||
|
||||
const todaysPages = todaysModules
|
||||
? Object.keys(todaysModules).flatMap((moduleName) =>
|
||||
todaysModules[moduleName].pages.map((page) => ({
|
||||
moduleName,
|
||||
page,
|
||||
}))
|
||||
)
|
||||
: [];
|
||||
|
||||
return (
|
||||
<div
|
||||
className={
|
||||
"border border-slate-600 rounded-lg p-2 pb-4 m-1 " + backgroundClass
|
||||
}
|
||||
onDrop={(e) => {
|
||||
itemDrop(e, day);
|
||||
}}
|
||||
onDrop={(e) => itemDrop(e, day)}
|
||||
onDragOver={(e) => e.preventDefault()}
|
||||
>
|
||||
{dayAsDate.getDate()}
|
||||
{todaysModules &&
|
||||
Object.keys(todaysModules).flatMap((moduleName) =>
|
||||
todaysModules[moduleName].assignments.map((a) => (
|
||||
<ul className="list-disc">
|
||||
{todaysAssignments.map(({ assignment, moduleName }) => (
|
||||
<DraggableListItem
|
||||
key={assignment.name}
|
||||
type={"assignment"}
|
||||
moduleName={moduleName}
|
||||
item={assignment}
|
||||
/>
|
||||
))}
|
||||
{todaysQuizzes.map(({ quiz, moduleName }) => (
|
||||
<DraggableListItem
|
||||
key={quiz.name}
|
||||
type={"quiz"}
|
||||
moduleName={moduleName}
|
||||
item={quiz}
|
||||
/>
|
||||
))}
|
||||
{todaysPages.map(({ page, moduleName }) => (
|
||||
<DraggableListItem
|
||||
key={page.name}
|
||||
type={"page"}
|
||||
moduleName={moduleName}
|
||||
item={page}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function DraggableListItem({
|
||||
type,
|
||||
moduleName,
|
||||
item,
|
||||
}: {
|
||||
type: "assignment" | "page" | "quiz";
|
||||
moduleName: string;
|
||||
item: IModuleItem;
|
||||
}) {
|
||||
const { courseName } = useCourseContext();
|
||||
return (
|
||||
<li
|
||||
key={a.name}
|
||||
role="button"
|
||||
draggable="true"
|
||||
onDragStart={(e) => {
|
||||
@@ -46,7 +108,7 @@ export default function Day({ day, month }: { day: string; month: number }) {
|
||||
"draggableItem",
|
||||
JSON.stringify({
|
||||
type: "assignment",
|
||||
item: a,
|
||||
item,
|
||||
sourceModuleName: moduleName,
|
||||
})
|
||||
);
|
||||
@@ -58,117 +120,13 @@ export default function Day({ day, month }: { day: string; month: number }) {
|
||||
encodeURIComponent(courseName) +
|
||||
"/modules/" +
|
||||
encodeURIComponent(moduleName) +
|
||||
"/assignment/" +
|
||||
encodeURIComponent(a.name)
|
||||
`/${type}/` +
|
||||
encodeURIComponent(item.name)
|
||||
}
|
||||
shallow={true}
|
||||
>
|
||||
{a.name}
|
||||
{item.name}
|
||||
</Link>
|
||||
</li>
|
||||
))
|
||||
)}
|
||||
{todaysModules &&
|
||||
Object.keys(todaysModules).flatMap((moduleName) =>
|
||||
todaysModules[moduleName].quizzes.map((q) => (
|
||||
<li
|
||||
key={q.name}
|
||||
role="button"
|
||||
draggable="true"
|
||||
onDragStart={(e) => {
|
||||
e.dataTransfer.setData(
|
||||
"draggableItem",
|
||||
JSON.stringify({
|
||||
type: "quiz",
|
||||
item: q,
|
||||
sourceModuleName: moduleName,
|
||||
})
|
||||
);
|
||||
}}
|
||||
>
|
||||
<Link
|
||||
href={
|
||||
"/course/" +
|
||||
encodeURIComponent(courseName) +
|
||||
"/modules/" +
|
||||
encodeURIComponent(moduleName) +
|
||||
"/quiz/" +
|
||||
encodeURIComponent(q.name)
|
||||
}
|
||||
shallow={true}
|
||||
>
|
||||
{q.name}
|
||||
</Link>
|
||||
</li>
|
||||
))
|
||||
)}
|
||||
{todaysModules &&
|
||||
Object.keys(todaysModules).flatMap((moduleName) =>
|
||||
todaysModules[moduleName].pages.map((p) => (
|
||||
<li
|
||||
key={p.name}
|
||||
role="button"
|
||||
draggable="true"
|
||||
onDragStart={(e) => {
|
||||
e.dataTransfer.setData(
|
||||
"draggableItem",
|
||||
JSON.stringify({
|
||||
type: "page",
|
||||
item: p,
|
||||
sourceModuleName: moduleName,
|
||||
})
|
||||
);
|
||||
}}
|
||||
>
|
||||
<Link
|
||||
href={
|
||||
"/course/" +
|
||||
encodeURIComponent(courseName) +
|
||||
"/modules/" +
|
||||
encodeURIComponent(moduleName) +
|
||||
"/page/" +
|
||||
encodeURIComponent(p.name)
|
||||
}
|
||||
shallow={true}
|
||||
>
|
||||
{p.name}
|
||||
</Link>
|
||||
</li>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// export default function Day({ day, month }: { day: string; month: number }) {
|
||||
// const { data: moduleNames } = useModuleNamesQuery();
|
||||
|
||||
// const dayAsDate = getDateFromStringOrThrow(
|
||||
// day,
|
||||
// "calculating same month in day"
|
||||
// );
|
||||
// const isInSameMonth = dayAsDate.getMonth() + 1 != month;
|
||||
// const backgroundClass = isInSameMonth ? "" : "bg-slate-900";
|
||||
// const { itemDrop } = useDraggingContext();
|
||||
|
||||
// return (
|
||||
// <div
|
||||
// className={
|
||||
// "border border-slate-600 rounded-lg p-2 pb-4 m-1 " + backgroundClass
|
||||
// }
|
||||
// onDrop={(e) => {
|
||||
// itemDrop(e, day);
|
||||
// }}
|
||||
// onDragOver={(e) => e.preventDefault()}
|
||||
// >
|
||||
// {dayAsDate.getDate()}
|
||||
// {moduleNames.map((moduleName) => (
|
||||
// <DayItemsInModule
|
||||
// key={"" + day + month + moduleName}
|
||||
// moduleName={moduleName}
|
||||
// day={day}
|
||||
// />
|
||||
// ))}
|
||||
// </div>
|
||||
// );
|
||||
// }
|
||||
|
||||
13
nextjs/src/services/withErrorHandling.ts
Normal file
13
nextjs/src/services/withErrorHandling.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function withErrorHandling(handler: () => Promise<Response | NextResponse>) {
|
||||
try {
|
||||
return await handler();
|
||||
} catch (error) {
|
||||
console.error("Error caught in centralized handler:", error);
|
||||
return NextResponse.json(
|
||||
{ error: (error as Error).message || "Internal Server Error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user