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

Change item quantities #69

Draft
wants to merge 2 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
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ model Items {
ListID String?
Linked_to_Category Categories? @relation(fields: [CategoryID], references: [id])
CategoryID String?
quantity Int?
unit String?
}
54 changes: 54 additions & 0 deletions src/pages/api/changeQuantity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { NextApiRequest, NextApiResponse } from "next";
import { PrismaClient } from "@prisma/client";
import { z, ZodError } from "zod";

const prisma = new PrismaClient();

const Quantity = z.object({
quantity: z.number(),
});

const AllowedUnits = z.union([
z.literal("kg"),
z.literal("L"),
z.literal("g"),
z.literal("pieces"),
z.literal(""),
]);

const QuantityAndUnit = Quantity.extend({
unit: AllowedUnits,
});

export default async function handler(
request: NextApiRequest,
response: NextApiResponse
) {
if (request.method === "POST") {
const { quantity, unit } = QuantityAndUnit.parse(request.body);

try {
const updatedList = await prisma.items.update({
where: { id: "0df43603-a0d9-4940-8775-1ede0ceebc6b" }, // Replace with actual ID of the list item
data: {
unit: unit,
quantity: quantity,
},
});

response.status(200).json(updatedList);
} catch (error) {
if (error instanceof ZodError) {
response
.status(400)
.send(`Invalid quantity parameter: ${error.message}`);
} else {
response.status(500).send("Internal server error");
}
}

return;
}

response.status(404).send(`Invalid method: ${request.method}`);
}
20 changes: 20 additions & 0 deletions src/pages/api/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NextApiRequest, NextApiResponse } from "next";
import { PrismaClient } from "@prisma/client";

// endpoint for general http request testing. Please don't make a comment to delete it.
// It is really useful for checking if the http request reaches the server.

const prisma = new PrismaClient();

export default async function handler(
request: NextApiRequest,
response: NextApiResponse
) {
if (request.method == "GET") {
const list = await prisma.items.findMany({});

response.status(200).json(list);
return;
}
response.status(404).send(`Invalid method: ${request.method}`);
}
10 changes: 8 additions & 2 deletions test.http
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
GET http://localhost:3000/api/user
GET http://localhost:3000/api/test

###

POST http://localhost:3000/api/user
POST http://localhost:3000/api/changeQuantity
Content-Type: application/json

{
"quantity": 4,
"unit": "L"
}


###
Expand Down