url-minify/backend/controllers/minify.js

117 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-02-13 06:01:55 +00:00
const Minfy = require('../models/minifed_urls')
2022-01-31 21:34:11 +00:00
const base_url = 'https://minfy.xyz/'
2022-02-13 06:01:55 +00:00
const { nanoid } = require('nanoid')
2022-01-31 19:44:34 +00:00
2022-02-01 18:44:58 +00:00
module.exports.getAllData = async (req, res) => {
Minfy.find({})
2022-02-13 06:01:55 +00:00
.then((data) => {
res.json(data)
})
.catch((err) => {
console.error(err)
res.sendStatus(500)
})
2022-02-01 18:44:58 +00:00
}
2022-01-31 21:34:11 +00:00
module.exports.getURLData = async (req, res) => {
try {
2022-02-01 18:36:47 +00:00
const data = await Minfy.findOne({
2022-02-13 08:28:33 +00:00
alias: req.params.alias,
2022-02-01 18:36:47 +00:00
})
2022-02-13 08:28:33 +00:00
// data.minifiedUrl = base_url + data.alias
if (!data || data == null) {
return res.json({ success: false, message: 'No data found' })
} else {
res.json({ success: true, data })
}
2022-01-31 21:34:11 +00:00
} catch (err) {
console.error(err)
res.sendStatus(500)
}
2022-02-01 05:39:00 +00:00
}
2022-02-01 18:36:47 +00:00
module.exports.findUrlById = async (req, res) => {
2022-02-13 06:01:55 +00:00
Minfy.findById(req.params.id)
.then((data) => {
2022-02-01 18:36:47 +00:00
res.send(data)
})
.catch((err) => {
console.error(err)
res.sendStatus(500)
})
}
2022-02-01 05:39:00 +00:00
module.exports.addURL = async (req, res) => {
2022-02-13 06:01:55 +00:00
req.body.alias = nanoid(5)
req.body.minifiedUrl = base_url + req.body.alias
2022-02-01 05:39:00 +00:00
Minfy.create(req.body)
2022-02-13 06:01:55 +00:00
.then((data) => {
res.json(data)
})
.catch((err) => {
console.error(err)
res.sendStatus(500)
})
2022-02-01 15:10:46 +00:00
}
2022-02-01 18:36:47 +00:00
module.exports.deleteUrlData = async (req, res) => {
2022-02-01 15:10:46 +00:00
Minfy.findByIdAndRemove(req.params.id)
2022-02-01 18:36:47 +00:00
.then((data) => {
2022-02-13 06:01:55 +00:00
res.send('Successfully Deleted')
2022-02-01 18:36:47 +00:00
})
.catch((err) => {
console.error(err)
res.sendStaus(500)
})
2022-02-01 20:04:46 +00:00
}
2022-02-12 14:35:15 +00:00
2022-02-13 06:01:55 +00:00
module.exports.updateUrlData = async (req, res) => {
//find a data object with url's id and update the alias
Minfy.findByIdAndUpdate(req.params.id, { alias: req.body.alias })
.then((data) => {
//send back the updated data object
res.send(data)
})
.catch((err) => {
//found error
console.error(err)
res.sendStatus(500)
})
}
module.exports.addURLAuthed = async (req, res) => {
2022-02-13 06:01:55 +00:00
const { alias, originalUrl } = req.body
var createdBy = req.user.data.email
// console.log(req.user);
const minifiedUrl = base_url + alias
const data = {
alias,
originalUrl,
2022-02-13 06:01:55 +00:00
minifiedUrl,
createdBy,
}
Minfy.create(data)
.then((data) => {
res.send(data)
})
.catch((err) => {
console.error(err)
res.sendStatus(500)
})
2022-02-13 06:01:55 +00:00
}
module.exports.visitor = async (req, res) => {
Minfy.findOneAndUpdate(
{ alias: req.params.alias },
{ $inc: { views: 1 } },
{ new: true }
)
.then((data) => {
res.send(`views increased`)
})
.catch((err) => {
console.error(err)
res.sendStatus(500)
})
}