Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add tag to log api #92

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions front/src/app/api/logs/[id]/connectTag/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { prismaErrorHandler } from "@/features/tags/utils";
import { LogsData } from "@/services/LogsData";
import { Prisma } from "@prisma/client";
import { NextResponse } from "next/server";

type QueryParams = {
params: { id: string };
};

export async function POST(req: Request, { params }: QueryParams) {
let json;
let tagId;

try {
json = await req.json();
({ tagId } = json);
} catch (error) {
return NextResponse.json(
{ error: "No tagId found in the body of the post request" },
{ status: 400 },
);
}

try {
await LogsData.connectTagToLog(params.id, tagId);
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
return prismaErrorHandler(error);
}
return NextResponse.json({ error: error }, { status: 500 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't catch it, would this not be the default behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it only return a response with status 500, but no error message. It doesn't seem like an error message shows in the console either.

}
return NextResponse.json({}, { status: 201 });
}
29 changes: 29 additions & 0 deletions front/src/features/tags/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Prisma } from "@prisma/client";
import { NextResponse } from "next/server";

export const prismaErrorHandler = (
error: Prisma.PrismaClientKnownRequestError,
) => {
if (error instanceof Prisma.PrismaClientKnownRequestError) {
switch (error.code) {
case "P2016": {
return NextResponse.json(
{ error: "No log exists with the given ID" },
{ status: 404 },
);
}
case "P2025": {
return NextResponse.json(
{ error: "No tag exists with the provided ID" },
{ status: 400 },
);
}
case "P5007": {
return NextResponse.json(
{ error: "You are not authenticated" },
{ status: 401 },
);
}
}
}
};
18 changes: 18 additions & 0 deletions front/src/services/LogsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,22 @@ export const LogsData = {

return log;
},
connectTagToLog: async (logId: string, tagId: string) => {
const result = await prisma.llmLogs.update({
where: {
id: logId,
},
data: {
tags: {
connect: {
id: tagId,
},
},
},
include: {
tags: true,
},
});
return result;
},
};