-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost-pouch-delete.js
62 lines (55 loc) · 1.53 KB
/
post-pouch-delete.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*\
title: $:/plugins/OokTech/PouchDBAdaptor/get-pouch-delete.js
type: application/javascript
module-type: route
GET /^\/pouch/delete
delete a tiddler
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
const tiddlerDB = require('$:/plugins/OokTech/Bob/UsePouchDB.js')
const thePath = new RegExp('^\/pouch\/delete')
module.exports = {
method: "POST",
path: thePath,
handler: function(request,response,state) {
try {
let title = state.data
// We don't return the whole doc, just the fields part, so we need to
// make a function here instead of just putting in callback as the
// second argument.
tiddlerDB.get(title, function (err, doc) {
if (err) {
if (err.name === 'not_found') {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("");
} else {
console.log(err)
response.writeHead(403)
response.end()
}
} else {
doc._deleted = true
tiddlerDB.put(doc, function(err) {
if (err) {
console.log(err)
response.writeHead(403)
response.end()
} else {
// Just reply with a 200, nothing else is needed here.
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("");
}
})
}
})
} catch (e) {
console.log(e)
response.writeHead(403)
response.end()
}
}
}
}());