2023-02-24 11:32:28 -06:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2023-03-05 15:03:20 -06:00
|
|
|
import getLinks from "@/lib/api/controllers/links/getLinks";
|
2023-02-24 11:32:28 -06:00
|
|
|
import postLink from "@/lib/api/controllers/links/postLink";
|
2023-10-23 09:45:48 -05:00
|
|
|
import { LinkRequestQuery } from "@/types/global";
|
2023-11-06 07:25:57 -06:00
|
|
|
import verifyUser from "@/lib/api/verifyUser";
|
2024-02-10 02:29:15 -06:00
|
|
|
import deleteLinksById from "@/lib/api/controllers/links/bulk/deleteLinksById";
|
2024-02-10 16:23:59 -06:00
|
|
|
import updateLinks from "@/lib/api/controllers/links/bulk/updateLinks";
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
2023-11-06 07:25:57 -06:00
|
|
|
const user = await verifyUser({ req, res });
|
|
|
|
if (!user) return;
|
2023-02-24 11:32:28 -06:00
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
if (req.method === "GET") {
|
2023-10-23 09:45:48 -05:00
|
|
|
// Convert the type of the request query to "LinkRequestQuery"
|
|
|
|
const convertedData: LinkRequestQuery = {
|
|
|
|
sort: Number(req.query.sort as string),
|
|
|
|
cursor: req.query.cursor ? Number(req.query.cursor as string) : undefined,
|
|
|
|
collectionId: req.query.collectionId
|
|
|
|
? Number(req.query.collectionId as string)
|
|
|
|
: undefined,
|
|
|
|
tagId: req.query.tagId ? Number(req.query.tagId as string) : undefined,
|
|
|
|
pinnedOnly: req.query.pinnedOnly
|
|
|
|
? req.query.pinnedOnly === "true"
|
|
|
|
: undefined,
|
|
|
|
searchQueryString: req.query.searchQueryString
|
|
|
|
? (req.query.searchQueryString as string)
|
|
|
|
: undefined,
|
|
|
|
searchByName: req.query.searchByName === "true" ? true : undefined,
|
|
|
|
searchByUrl: req.query.searchByUrl === "true" ? true : undefined,
|
|
|
|
searchByDescription:
|
|
|
|
req.query.searchByDescription === "true" ? true : undefined,
|
2023-11-01 05:01:26 -05:00
|
|
|
searchByTextContent:
|
|
|
|
req.query.searchByTextContent === "true" ? true : undefined,
|
2023-10-23 09:45:48 -05:00
|
|
|
searchByTags: req.query.searchByTags === "true" ? true : undefined,
|
|
|
|
};
|
|
|
|
|
2023-11-02 13:59:31 -05:00
|
|
|
const links = await getLinks(user.id, convertedData);
|
2023-03-28 02:31:50 -05:00
|
|
|
return res.status(links.status).json({ response: links.response });
|
|
|
|
} else if (req.method === "POST") {
|
2023-11-02 13:59:31 -05:00
|
|
|
const newlink = await postLink(req.body, user.id);
|
2023-03-28 02:31:50 -05:00
|
|
|
return res.status(newlink.status).json({
|
|
|
|
response: newlink.response,
|
|
|
|
});
|
2024-02-10 02:29:15 -06:00
|
|
|
} else if (req.method === "PUT") {
|
2024-02-10 18:34:25 -06:00
|
|
|
const updated = await updateLinks(
|
|
|
|
user.id,
|
|
|
|
req.body.links,
|
2024-02-11 01:21:25 -06:00
|
|
|
req.body.removePreviousTags,
|
2024-02-10 18:34:25 -06:00
|
|
|
req.body.newData
|
|
|
|
);
|
2024-02-10 02:29:15 -06:00
|
|
|
return res.status(updated.status).json({
|
|
|
|
response: updated.response,
|
|
|
|
});
|
|
|
|
} else if (req.method === "DELETE") {
|
|
|
|
const deleted = await deleteLinksById(user.id, req.body.linkIds);
|
|
|
|
return res.status(deleted.status).json({
|
|
|
|
response: deleted.response,
|
|
|
|
});
|
2023-03-28 02:31:50 -05:00
|
|
|
}
|
2023-02-24 11:32:28 -06:00
|
|
|
}
|