Created the links.ts router & created a route to insert a new link
This commit is contained in:
parent
adee79675c
commit
c7a88c133b
21
api/routes/links.ts
Normal file
21
api/routes/links.ts
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user