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 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
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 { 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
) {
// I think with this new schema, that this should become a DELETE request.
if (request.method === "DELETE") {
try {
const { id } = inputQueryTest.parse(request.body);

const listToBe = await prisma.item.delete({
where: {
id: id,
},
});
response.status(200).send("Removed Items");
} catch (err) {
if (err instanceof ZodError) {
response.status(400).send(`Wrong Data Sent =>${JSON.stringify(err)}`);
} else {
response
.status(418)
.send("Something is wrong, you may or may not be a teapot");
}
}
response.status(404).send(`Invalid method, need PATCH: ${request.method}`);
}
}

const inputQueryTest = z.object({
id: z.string(),
});
10 changes: 7 additions & 3 deletions test.http
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ GET http://localhost:3000/api/autocomplete?name=Bana

###

DELETE http://localhost:3000/api/deleteList
DELETE http://localhost:3000/api/deleteList?id=123456789


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

{
"name": "listest1"
}
"id":"123456789"
}