mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-09 15:05:35 +00:00
refactor: get/create categories endpoints
This commit is contained in:
11
src/lib/category/getUserCategoryByName.ts
Normal file
11
src/lib/category/getUserCategoryByName.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Category, User } from "@prisma/client";
|
||||
import prisma from "utils/prisma";
|
||||
|
||||
export default async function getUserCategoryByName(
|
||||
user: User,
|
||||
name: Category["name"]
|
||||
) {
|
||||
return await prisma.category.findFirst({
|
||||
where: { name, authorId: user.id },
|
||||
});
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { checkMethodAllowedOrThrow } from "utils/api";
|
||||
import prisma from "utils/prisma";
|
||||
import { getSessionOrThrow } from "utils/session";
|
||||
|
||||
export default async function POST(req: NextApiRequest, res: NextApiResponse) {
|
||||
await checkMethodAllowedOrThrow(req, ["post"]);
|
||||
const session = await getSessionOrThrow(req, res);
|
||||
|
||||
const name = req.body?.name as string;
|
||||
if (!name) {
|
||||
throw new Error("Categorie name missing");
|
||||
}
|
||||
|
||||
const category = await prisma.category.findFirst({
|
||||
where: { name },
|
||||
});
|
||||
if (category) {
|
||||
throw new Error("Category name already used");
|
||||
}
|
||||
|
||||
const { id: authorId } = await prisma.user.findFirstOrThrow({
|
||||
where: {
|
||||
email: session.user.email,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
const categoryCreated = await prisma.category.create({
|
||||
data: { name, authorId },
|
||||
});
|
||||
console.log("là");
|
||||
return res.status(200).send({
|
||||
success: "Category successfully created",
|
||||
categoryId: categoryCreated.id,
|
||||
});
|
||||
}
|
||||
@@ -1,20 +1,41 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { apiHandler } from "lib/api/handler";
|
||||
import getUserCategories from "lib/category/getUserCategories";
|
||||
import getUserCategoryByName from "lib/category/getUserCategoryByName";
|
||||
import prisma from "utils/prisma";
|
||||
import { object, string } from "yup";
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
try {
|
||||
const categories = await prisma.category.findMany();
|
||||
console.log("request");
|
||||
return res.status(200).send({
|
||||
categories,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return res.status(400).send({
|
||||
error: "Une erreur est survenue lors de la récupération des catégories",
|
||||
});
|
||||
}
|
||||
export default apiHandler({
|
||||
get: getCatgories,
|
||||
post: createCategory,
|
||||
});
|
||||
|
||||
async function getCatgories({ res, user }) {
|
||||
const categories = await getUserCategories(user);
|
||||
return res.status(200).send({
|
||||
categories,
|
||||
});
|
||||
}
|
||||
|
||||
const bodySchema = object({
|
||||
name: string()
|
||||
.trim()
|
||||
.required("name is required")
|
||||
.max(32, "name is too long"),
|
||||
}).typeError("Missing request Body");
|
||||
|
||||
async function createCategory({ req, res, user }) {
|
||||
const { name } = await bodySchema.validate(req.body);
|
||||
|
||||
const category = await getUserCategoryByName(user, name);
|
||||
if (category) {
|
||||
throw new Error("Category name already used");
|
||||
}
|
||||
|
||||
const categoryCreated = await prisma.category.create({
|
||||
data: { name, authorId: user.id },
|
||||
});
|
||||
return res.status(200).send({
|
||||
success: "Category successfully created",
|
||||
categoryId: categoryCreated.id,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { NextApiRequest } from "next";
|
||||
|
||||
export function checkMethodAllowedOrThrow(
|
||||
req: NextApiRequest,
|
||||
methods: Array<RequestInit["method"]>
|
||||
) {
|
||||
const isMethodAllowed = methods.includes(req.method.toLowerCase());
|
||||
if (!isMethodAllowed) {
|
||||
throw new Error(`Method ${req.method} not allowed`);
|
||||
}
|
||||
return isMethodAllowed;
|
||||
}
|
||||
Reference in New Issue
Block a user