-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.ts
41 lines (37 loc) · 1.25 KB
/
router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import express from 'express';
import { MongoEntityManager } from "typeorm";
import { Backup } from "./entities/backup";
import { handleAuthentication } from "./authentication";
export default function getRouter(manager: MongoEntityManager) {
const router = express.Router();
router.use(handleAuthentication);
router.post("/get-backup", async (req, res) => {
const publicKey = req.body["auth"]["pubKey"];
const backup = await manager.findOne(Backup, { publicKey: publicKey });
if (backup)
res.send(backup.data);
else
res.status(404).send('Not found')
});
router.post("/store-backup", (async (req, res) => {
try {
const backup = Backup.fromData(req.body['data']);
if (await manager.findOne(Backup, { publicKey: backup.publicKey }))
await manager.findOneAndReplace(Backup, { publicKey: backup.publicKey }, backup);
else
await manager.save(backup);
res.send("Saved!")
} catch (e) {
console.log(e);
res.status(400);
res.send(e.message)
}
}));
router.post("/delete-backup", (async (req, res) => {
const publicKey = req.body['auth']['pubKey'];
await manager.delete(Backup, { publicKey: publicKey });
res.status(200);
res.send()
}));
return router;
}