Created the links.ts router & created a route to insert a new link

This commit is contained in:
Mathias Wagner 2022-09-09 01:10:44 +02:00
parent adee79675c
commit c7a88c133b
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

21
api/routes/links.ts Normal file
View File

@ -0,0 +1,21 @@
import Router, {Response} from 'express';
import {AuthenticatedRequest, hasRank} from "../middlewares/authenticate";
import {Rank} from "../../models/User";
import {validateSchema} from "../../util/validate";
import {shortUrl} from "../validations/linkValidation";
import {ShortenedLink} from "../../models/ShortenedLink";
const app = Router();
app.put("/", hasRank(Rank.TEAM_MEMBER), async (req: AuthenticatedRequest, res: Response) => {
const validationError = validateSchema(shortUrl, req.body);
if (validationError) return res.status(400).json({message: validationError});
if (await ShortenedLink.findOne({shortenedId: req.body.shortenedId}) != null)
return res.status(409).json({message: "The provided id has already been taken"});
const link = await ShortenedLink.create({originalUrl: req.body.originalUrl, shortenedId: req.body.shortenedId, clientId: req.user.clientId});
res.json({message: "Link successfully shortened", "shorten_url": link.shortenedId});
});
module.exports = app;