You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.0 KiB
41 lines
1.0 KiB
const router = require("express").Router(); |
|
const Str = require("@supercharge/strings"); |
|
const Redirect = require("../model/Redirect"); |
|
|
|
router.get("/", async (req, res) => { |
|
const redirects = await Redirect.query().where("userId", req.user.id); |
|
return res.json(redirects); |
|
}); |
|
|
|
router.post("/", async (req, res) => { |
|
console.log("Creating redirect for user req.user"); |
|
const { forwardTo } = req.body; |
|
|
|
if (!forwardTo) { |
|
return res.status(400).send("field 'forwardTo' was not provided"); |
|
} |
|
const alias = `${Str.random(20)}@proxymail.com`; |
|
const result = await Redirect.query().insert({ |
|
user_id: req.user.id, |
|
forward_to: forwardTo, |
|
alias, |
|
}); |
|
|
|
res.json(result); |
|
}); |
|
|
|
router.delete("/:id", async (req, res) => { |
|
const id = req.params.id; |
|
|
|
const result = await Redirect.query() |
|
.where("id", id) |
|
.andWhere("userId", req.user.id) |
|
.delete(); |
|
|
|
res.send({ |
|
status: "Success", |
|
result, |
|
}); |
|
}); |
|
|
|
module.exports = router;
|
|
|