2023-10-22 23:28:39 -05:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import deleteLinkById from "@/lib/api/controllers/links/linkId/deleteLinkById";
|
|
|
|
import updateLinkById from "@/lib/api/controllers/links/linkId/updateLinkById";
|
2023-10-28 23:57:24 -05:00
|
|
|
import getLinkById from "@/lib/api/controllers/links/linkId/getLinkById";
|
2023-11-02 13:59:31 -05:00
|
|
|
import authenticateUser from "@/lib/api/authenticateUser";
|
2023-10-22 23:28:39 -05:00
|
|
|
|
|
|
|
export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
2023-11-02 13:59:31 -05:00
|
|
|
const user = await authenticateUser({ req, res });
|
|
|
|
if (!user) return res.status(404).json({ response: "User not found." });
|
2023-10-22 23:28:39 -05:00
|
|
|
|
2023-10-28 23:57:24 -05:00
|
|
|
if (req.method === "GET") {
|
2023-11-02 13:59:31 -05:00
|
|
|
const updated = await getLinkById(user.id, Number(req.query.id));
|
2023-10-28 23:57:24 -05:00
|
|
|
return res.status(updated.status).json({
|
|
|
|
response: updated.response,
|
|
|
|
});
|
|
|
|
} else if (req.method === "PUT") {
|
2023-10-22 23:28:39 -05:00
|
|
|
const updated = await updateLinkById(
|
2023-11-02 13:59:31 -05:00
|
|
|
user.id,
|
2023-10-22 23:28:39 -05:00
|
|
|
Number(req.query.id),
|
|
|
|
req.body
|
|
|
|
);
|
|
|
|
return res.status(updated.status).json({
|
|
|
|
response: updated.response,
|
|
|
|
});
|
|
|
|
} else if (req.method === "DELETE") {
|
2023-11-02 13:59:31 -05:00
|
|
|
const deleted = await deleteLinkById(user.id, Number(req.query.id));
|
2023-10-22 23:28:39 -05:00
|
|
|
return res.status(deleted.status).json({
|
|
|
|
response: deleted.response,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|