tpbproxy/routes/transmission.js
2024-01-06 18:09:23 -05:00

81 lines
1.7 KiB
JavaScript

'use static';
const router = require('express').Router();
const {Torrent} = require('>/models');
router.get('/', async function(req, res, next){
try{
res.json({results: await Torrent.findAll({
where:{added_by: req.query.username || req.user.username},
// limit: req.query.limit,
// offset: req.query.offset,
// order: [[req.query.orderKey || "created_at", (req.query.asc && "ASC")]],
// from: {$between: [startDate, endDate]}
})});
}catch(error){
next(error);
}
});
router.post("/", async function(req, res, next){
try{
res.json(await Torrent.create({...req.body, added_by: req.user.username}))
}catch(error){
next(error);
}
});
router.get('/server', async function(req, res, next){
try{
res.json(await Torrent.trClient.sessionStats())
}catch(error){
next(error);
}
});
router.get("/:id", async function(req, res, next){
try{
let torrent = await Torrent.findByPk(req.params.id);
if('latest' in req.query){
torrent = await torrent.getTorrentData();
}
res.json({result: torrent});
}catch(error){
next(error);
}
});
router.delete("/:id", async function(req, res, next){
try{
let torrent = await Torrent.findByPk(req.params.id);
res.json({result: torrent, activity: await torrent.destroy()});
}catch(error){
next(error);
}
});
router.post("/:id/stop", async function(req, res, next){
try{
let torrent = await Torrent.findByPk(req.params.id);
res.json({result: torrent, activity: await torrent.stop()});
}catch(error){
next(error);
}
});
router.post("/:id/start", async function(req, res, next){
try{
let torrent = await Torrent.findByPk(req.params.id);
res.json({result: torrent, activity: await torrent.start()});
}catch(error){
next(error);
}
});
module.exports = router;