From f018392067f6f67feadcdc421bad475752faedc0 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Fri, 9 Sep 2022 15:06:17 +0200 Subject: [PATCH] Created the link controller --- controller/links.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 controller/links.ts diff --git a/controller/links.ts b/controller/links.ts new file mode 100644 index 0000000..20f7f4b --- /dev/null +++ b/controller/links.ts @@ -0,0 +1,25 @@ +import {ShortenedLink} from "../models/ShortenedLink"; + +/** + * Gets a link object from an id + * @param shortenedId The link id (shortened) + */ +export const getLinkById = (shortenedId: string) => ShortenedLink.findOne({shortenedId: shortenedId}, {_id: 0, __v: 0}); + +/** + * Shorts a new link + * @param originalUrl The url to short + * @param shortenedId The custom id of the link (optional) + * @param clientId The client id of the user that created the link + */ +export const shortLink = (originalUrl: string, shortenedId: string | undefined, clientId: Number) => + ShortenedLink.create({originalUrl, shortenedId, clientId}); + +/** + * Lists all links created by a specific user + * @param clientId The user to get the links from + */ +export const listLinksByUser = (clientId: Number) => ShortenedLink.find({clientId}, {_id: 0, __v: 0}); + +/** Lists all created links from the database */ +export const listAllLinks = () => ShortenedLink.find({}, {_id: 0, __v: 0}); \ No newline at end of file