extracting out schemaQuery validation to lib, extracting out delete/edit to it's own files for clarity
This commit is contained in:
1 parent
e4d9f7bc7d
commit
737a8897ba
7 files changed
+135
-33
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import schema from "lib/validations/queryIdTransformParseInt";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { withValidation } from "next-validations";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
type ResponseData = {
|
||||
message?: string;
|
||||
error?: any;
|
||||
};
|
||||
|
||||
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
const safe = await schema.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "DELETE") {
|
||||
// DELETE WILL DELETE THE EVENT TYPE
|
||||
prisma.eventType
|
||||
.delete({ where: { id: safe.data.id } })
|
||||
.then(() => {
|
||||
// We only remove the event type from the database if there's an existing resource.
|
||||
res.status(200).json({ message: `event-type with id: ${safe.data.id} deleted successfully` });
|
||||
})
|
||||
.catch((error) => {
|
||||
// This catches the error thrown by prisma.eventType.delete() if the resource is not found.
|
||||
res.status(400).json({ error: error });
|
||||
});
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ error: "Only DELETE Method allowed in /event-types/[id]/delete endpoint" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
mode: "query",
|
||||
});
|
||||
|
||||
export default validate(eventType);
|
||||
@@ -0,0 +1,49 @@
|
||||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import schemaQuery from "lib/validations/queryIdTransformParseInt";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { withValidation } from "next-validations";
|
||||
import { z } from "zod";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const schema = z
|
||||
.object({
|
||||
title: z.string().min(3),
|
||||
slug: z.string().min(3),
|
||||
length: z.number().min(1).max(1440), // max is a full day.
|
||||
description: z.string().min(3).optional(),
|
||||
})
|
||||
.strict(); // Adding strict so that we can disallow passing in extra fields
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
error?: any;
|
||||
};
|
||||
|
||||
export async function editEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, body, method } = req;
|
||||
const safeQuery = await schemaQuery.safeParse(query);
|
||||
const safeBody = await schema.safeParse(body);
|
||||
|
||||
if (safeQuery.success && safeBody.success) {
|
||||
if (method === "PATCH") {
|
||||
const event = await prisma.eventType.update({
|
||||
where: { id: safeQuery.data.id },
|
||||
data: { ...safeBody.data },
|
||||
});
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ error: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ error: "Only GET Method allowed" });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
mode: "body",
|
||||
});
|
||||
|
||||
export default validate(editEventType);
|
||||
@@ -1,27 +1,10 @@
|
||||
import { PrismaClient, EventType } from "@prisma/client";
|
||||
import schema from "lib/validations/queryIdTransformParseInt";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { withValidation } from "next-validations";
|
||||
import { z } from "zod";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const schema = z
|
||||
.object({
|
||||
// since nextjs parses query params as strings,
|
||||
// we need to cast them to numbers using z.transform() and parseInt()
|
||||
id: z
|
||||
.string()
|
||||
.regex(/^\d+$/)
|
||||
.transform((id) => parseInt(id)),
|
||||
})
|
||||
.strict();
|
||||
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
mode: "query",
|
||||
});
|
||||
|
||||
type ResponseData = {
|
||||
data?: EventType;
|
||||
error?: any;
|
||||
@@ -29,18 +12,31 @@ type ResponseData = {
|
||||
|
||||
export async function eventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||
const { query, method } = req;
|
||||
if (method === "GET") {
|
||||
const safe = await schema.safeParse(query);
|
||||
if (safe.success) {
|
||||
const safe = await schema.safeParse(query);
|
||||
if (safe.success) {
|
||||
if (method === "GET") {
|
||||
const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } });
|
||||
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ error: "Event type not found" });
|
||||
} else if (method === "PATCH") {
|
||||
const event = await prisma.eventType.update({
|
||||
where: { id: safe.data.id },
|
||||
data: { title: "Updated title" },
|
||||
});
|
||||
if (event) res.status(200).json({ data: event });
|
||||
if (!event) res.status(404).json({ error: "Event type not found" });
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ error: "Only GET Method allowed" });
|
||||
}
|
||||
} else {
|
||||
// Reject any other HTTP method than POST
|
||||
res.status(405).json({ error: "Only GET Method allowed" });
|
||||
}
|
||||
}
|
||||
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
mode: "query",
|
||||
});
|
||||
|
||||
export default validate(eventType);
|
||||
@@ -14,7 +14,7 @@ const schema = z
|
||||
})
|
||||
.strict(); // Adding strict so that we can disallow passing in extra fields
|
||||
|
||||
type schema = z.infer<typeof schema>;
|
||||
// type schema = z.infer<typeof schema>;
|
||||
const validate = withValidation({
|
||||
schema,
|
||||
type: "Zod",
|
||||
|
||||
Reference in new issue
Block a user