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