mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-11 16:53:05 +00:00
refactor: api link endpoint - edit & delete
This commit is contained in:
82
src/pages/api/link/[lid].ts
Normal file
82
src/pages/api/link/[lid].ts
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import { boolean, number, object, string } from "yup";
|
||||||
|
|
||||||
|
import { VALID_URL_REGEX } from "constants/url";
|
||||||
|
import { apiHandler } from "lib/api/handler";
|
||||||
|
import getUserLink from "lib/link/getUserLink";
|
||||||
|
import prisma from "utils/prisma";
|
||||||
|
|
||||||
|
export default apiHandler({
|
||||||
|
put: editLink,
|
||||||
|
delete: deleteLink,
|
||||||
|
});
|
||||||
|
|
||||||
|
const querySchema = object({
|
||||||
|
lid: number().required(),
|
||||||
|
});
|
||||||
|
|
||||||
|
// FIXME: code duplicated from api/link/create
|
||||||
|
const bodySchema = object({
|
||||||
|
name: string()
|
||||||
|
.trim()
|
||||||
|
.required("name is required")
|
||||||
|
.max(32, "name is too long"),
|
||||||
|
url: string()
|
||||||
|
.trim()
|
||||||
|
.required("url is required")
|
||||||
|
.matches(VALID_URL_REGEX, "invalid url format"),
|
||||||
|
categoryId: number().required("categoryId must be a number"),
|
||||||
|
favorite: boolean().default(() => false),
|
||||||
|
}).typeError("Missing request Body");
|
||||||
|
|
||||||
|
async function editLink({ req, res, user }) {
|
||||||
|
const { lid } = await querySchema.validate(req.query);
|
||||||
|
const { name, url, favorite, categoryId } = await bodySchema.validate(
|
||||||
|
req.body
|
||||||
|
);
|
||||||
|
|
||||||
|
const link = await getUserLink(user, lid);
|
||||||
|
if (!link) {
|
||||||
|
throw new Error("Unable to find link " + lid);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
link.name === name &&
|
||||||
|
link.url === url &&
|
||||||
|
link.favorite === favorite &&
|
||||||
|
link.categoryId === categoryId
|
||||||
|
) {
|
||||||
|
throw new Error("You must update at least one field");
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.link.update({
|
||||||
|
where: { id: Number(lid) },
|
||||||
|
data: {
|
||||||
|
name,
|
||||||
|
url,
|
||||||
|
favorite,
|
||||||
|
categoryId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.send({ success: "Link successfully updated", categoryId });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteLink({ req, res, user }) {
|
||||||
|
const { lid } = await querySchema.validate(req.query);
|
||||||
|
|
||||||
|
const link = await getUserLink(user, lid);
|
||||||
|
if (!link) {
|
||||||
|
throw new Error("Unable to find link " + lid);
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.link.delete({
|
||||||
|
where: { id: Number(lid) },
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.send({
|
||||||
|
success: "Link successfully deleted",
|
||||||
|
categoryId: link.categoryId,
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
|
||||||
import prisma from "utils/prisma";
|
|
||||||
|
|
||||||
// TODO: Ajouter vérification -> l'utilisateur doit changer au moins un champ
|
|
||||||
export default async function handler(
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse
|
|
||||||
) {
|
|
||||||
const { lid } = req.query;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const link = await prisma.link.findFirst({
|
|
||||||
where: { id: Number(lid) },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!link) {
|
|
||||||
return res.status(400).send({ error: "Lien introuvable" });
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
return res.status(400).send({
|
|
||||||
error:
|
|
||||||
"Une erreur est survenue lors de l'édition du lien (link/edit->findLink)",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const name = req.body?.name as string;
|
|
||||||
const url = req.body?.url as string;
|
|
||||||
const favorite = Boolean(req.body?.favorite) || false;
|
|
||||||
const categoryId = Number(req.body?.categoryId);
|
|
||||||
|
|
||||||
if (!name) {
|
|
||||||
return res.status(400).send({ error: "Nom du lien manquant" });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!url) {
|
|
||||||
return res.status(400).send({ error: "URL du lien manquant" });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!categoryId) {
|
|
||||||
return res.status(400).send({ error: "Catégorie du lien manquante" });
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await prisma.link.update({
|
|
||||||
where: { id: Number(lid) },
|
|
||||||
data: {
|
|
||||||
name,
|
|
||||||
url,
|
|
||||||
favorite,
|
|
||||||
categoryId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return res
|
|
||||||
.status(200)
|
|
||||||
.send({ success: "Lien mis à jour avec succès", categoryId });
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
return res.status(400).send({
|
|
||||||
error:
|
|
||||||
"Une erreur est survenue lors de l'édition du lien (link/remove->updateLink)",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
|
||||||
import prisma from "utils/prisma";
|
|
||||||
|
|
||||||
export default async function handler(
|
|
||||||
req: NextApiRequest,
|
|
||||||
res: NextApiResponse
|
|
||||||
) {
|
|
||||||
const { lid } = req.query;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const link = await prisma.link.findFirst({
|
|
||||||
where: { id: Number(lid) },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!link) {
|
|
||||||
return res.status(400).send({ error: "Lien introuvable" });
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
return res.status(400).send({
|
|
||||||
error:
|
|
||||||
"Une erreur est survenue lors de la suppression du lien (link/remove->findLink)",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const link = await prisma.link.delete({
|
|
||||||
where: { id: Number(lid) },
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(200).send({
|
|
||||||
success: "Le lien a été supprimé avec succès",
|
|
||||||
categoryId: link.categoryId,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
return res.status(400).send({
|
|
||||||
error:
|
|
||||||
"Une erreur est survenue lors de la suppression du lien (link/remove->deleteLink)",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user