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

Delete item #52

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
43 changes: 43 additions & 0 deletions src/pages/api/deleteItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { NextApiRequest, NextApiResponse } from "next";
import { z, ZodError } from "zod";
import { Items, List, Prisma, PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

/* The point of this endpoint is to delete an item to a list.
First check the name given against the database.
Since there are multiple ways to spell something we are trying to remove
a couple of letters at a time and see if there is a match
When shorter than 3 letters we just put it in the Other list
*/
export default async function handler(
request: NextApiRequest,
response: NextApiResponse
) {
if (request.method === "PATCH") {
try {
const { id } = inputQueryTest.parse(request.body);

const listToBe = await prisma.items.update({
where: {
id: id,
},
data: {
ListID: null,
Copy link
Collaborator

Choose a reason for hiding this comment

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

please use disconnect: true
instead of nulling the reference to the foreign key here.
https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#disconnect

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

working, thanks.

},
});
response.status(201).send("Removed Items");
Copy link
Collaborator

Choose a reason for hiding this comment

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

200 is more fitting here

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks going to change it to 200.

} catch (err) {
if (err instanceof ZodError) {
response.status(400).send(`Wrong Data Sent =>${JSON.stringify(err)}`);
} else {
response.status(418).send("Something is wrong");
Copy link
Collaborator

Choose a reason for hiding this comment

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

so you're a teapot? 😄

}
}
response.status(404).send(`Invalid method, need PATCH: ${request.method}`);
}
}

const inputQueryTest = z.object({
id: z.string(),
});
8 changes: 8 additions & 0 deletions test.http
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ Content-Type: application/json
{
"query":"pear",
"inputList":"Hello"
}

###
PATCH http://localhost:3000/api/deleteItem/
Content-Type: application/json

{
"id":"a169f9ec-78de-48ed-9e4a-7ca3e42a52be"
}