|
| 1 | +const express = require("express"); |
| 2 | +const router = express.Router(); |
| 3 | +const validUrl = require("valid-url"); |
| 4 | +const shortId = require("shortid"); |
| 5 | + |
| 6 | +const middleAuth = require("../../middleware/auth"); |
| 7 | + |
| 8 | +const Url = require("../../models/url"); |
| 9 | +const User = require("../../models/user"); |
| 10 | + |
| 11 | +router.get("/", middleAuth, async (req, res) => { |
| 12 | + try { |
| 13 | + const user = await User.findById(req.user.id) |
| 14 | + .populate("shortenedUrl") |
| 15 | + .exec(); |
| 16 | + if (!user) { |
| 17 | + return res.status(400).json({ msg: "User does not exist" }); |
| 18 | + } else { |
| 19 | + return res |
| 20 | + .status(200) |
| 21 | + .json({ msg: "User found", url: user.shortenedUrl }); |
| 22 | + } |
| 23 | + } catch (err) { |
| 24 | + console.log(err); |
| 25 | + res.status(500).send({ msg: "Internal Server Error" }); |
| 26 | + } |
| 27 | +}); |
| 28 | + |
| 29 | +router.post("/compress", middleAuth, async (req, res) => { |
| 30 | + const { longUrl, lastDate } = req.body; |
| 31 | + |
| 32 | + // const baseUrl = "http://localhost:5000"; |
| 33 | + const baseUrl = process.env.BASE_URL; |
| 34 | + if (!validUrl.isUri(baseUrl)) { |
| 35 | + return res.status(400).send({ msg: "Invalid Base Url" }); |
| 36 | + } |
| 37 | + |
| 38 | + const urlCode = shortId.generate(); |
| 39 | + |
| 40 | + if (validUrl.isUri(longUrl)) { |
| 41 | + try { |
| 42 | + let url = await Url.findOne({ longUrl: longUrl }); |
| 43 | + |
| 44 | + if (url) { |
| 45 | + res |
| 46 | + .status(400) |
| 47 | + .send({ msg: "Compressed URL already exists", url: url.shortUrl }); |
| 48 | + } else { |
| 49 | + const shortUrl = baseUrl + "/" + urlCode; |
| 50 | + |
| 51 | + url = new Url({ |
| 52 | + longUrl, |
| 53 | + shortUrl, |
| 54 | + urlCode, |
| 55 | + lastDate: lastDate, |
| 56 | + code: urlCode, |
| 57 | + creator: req.user.id, |
| 58 | + }); |
| 59 | + const newUrl = await url.save(); |
| 60 | + |
| 61 | + const user = await User.findById(req.user.id); |
| 62 | + await user.shortenedUrl.push(newUrl._id); |
| 63 | + await user.save(); |
| 64 | + |
| 65 | + res |
| 66 | + .status(200) |
| 67 | + .json({ msg: "URL Compressed Successfully", url: newUrl }); |
| 68 | + } |
| 69 | + } catch (err) { |
| 70 | + console.log(err); |
| 71 | + res.status(500).send({ msg: "Internal Server Error" }); |
| 72 | + } |
| 73 | + } else { |
| 74 | + res.status(401).send({ msg: "Invalid Long URL" }); |
| 75 | + } |
| 76 | +}); |
| 77 | + |
| 78 | +router.delete("/:id", middleAuth, async (req, res) => { |
| 79 | + const id = req.params.id; |
| 80 | + |
| 81 | + try { |
| 82 | + const url = await Url.findById(id); |
| 83 | + |
| 84 | + if (req.user.id !== url.creator.toString()) { |
| 85 | + return res.status(401).json({ msg: "Unauthorized User" }); |
| 86 | + } |
| 87 | + |
| 88 | + const user = await User.findById(req.user.id); |
| 89 | + |
| 90 | + for (let i = 0; i < user.shortenedUrl.length; i++) { |
| 91 | + if (user.shortenedUrl[i].toString() === id) { |
| 92 | + user.shortenedUrl.splice(i, 1); |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + await user.save(); |
| 97 | + await url.deleteOne(); |
| 98 | + |
| 99 | + res.status(200).json({ msg: "Short Url Deleted Successfully" }); |
| 100 | + } catch (err) { |
| 101 | + console.log(err); |
| 102 | + res.status(500).json({ msg: "Internal Server Error" }); |
| 103 | + } |
| 104 | +}); |
| 105 | + |
| 106 | +router.patch("/:id", middleAuth, async (req, res) => { |
| 107 | + const id = req.params.id; |
| 108 | + |
| 109 | + try { |
| 110 | + const url = await Url.findById(id); |
| 111 | + |
| 112 | + if (req.user.id !== url.creator.toString()) { |
| 113 | + return res.status(401).json({ msg: "Unauthorized User" }); |
| 114 | + } |
| 115 | + |
| 116 | + const { lastDate, status } = req.body; |
| 117 | + |
| 118 | + url.lastDate = lastDate; |
| 119 | + url.status = status; |
| 120 | + const newUrl = await url.save(); |
| 121 | + res.status(200).json({ msg: "URL updated successfully", url: newUrl }); |
| 122 | + } catch (err) { |
| 123 | + console.log(err); |
| 124 | + res.status(500).json({ msg: "Internal Server Error" }); |
| 125 | + } |
| 126 | +}); |
| 127 | + |
| 128 | +module.exports = router; |
0 commit comments