2
0
mirror of https://github.com/frappe/books.git synced 2024-12-24 11:55:46 +00:00
books/server/restAPI.js

78 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-01-16 06:09:17 +00:00
const frappe = require('frappejs');
2018-01-12 12:25:07 +00:00
module.exports = {
setup(app) {
// get list
2018-03-26 12:18:07 +00:00
app.get('/api/resource/:doctype', frappe.asyncHandler(async function(request, response) {
2018-02-12 12:01:31 +00:00
for (let key of ['fields', 'filters']) {
2018-01-12 12:25:07 +00:00
if (request.query[key]) {
request.query[key] = JSON.parse(request.query[key]);
}
}
let data = await frappe.db.getAll({
2018-01-12 12:25:07 +00:00
doctype: request.params.doctype,
2018-01-23 12:26:40 +00:00
fields: request.query.fields,
2018-01-12 12:25:07 +00:00
filters: request.query.filters,
start: request.query.start || 0,
limit: request.query.limit || 20,
order_by: request.query.order_by,
order: request.query.order
});
return response.json(data);
}));
// create
2018-03-26 12:18:07 +00:00
app.post('/api/resource/:doctype', frappe.asyncHandler(async function(request, response) {
2018-02-08 09:38:47 +00:00
let data = request.body;
2018-01-12 12:25:07 +00:00
data.doctype = request.params.doctype;
2018-02-08 12:28:51 +00:00
let doc = frappe.newDoc(data);
2018-01-12 12:25:07 +00:00
await doc.insert();
await frappe.db.commit();
2018-02-08 11:45:32 +00:00
return response.json(doc.getValidDict());
2018-01-12 12:25:07 +00:00
}));
// update
2018-03-26 12:18:07 +00:00
app.put('/api/resource/:doctype/:name', frappe.asyncHandler(async function(request, response) {
2018-02-08 09:38:47 +00:00
let data = request.body;
2018-02-08 12:28:51 +00:00
let doc = await frappe.getDoc(request.params.doctype, request.params.name);
2018-01-12 12:25:07 +00:00
Object.assign(doc, data);
await doc.update();
await frappe.db.commit();
2018-02-08 11:45:32 +00:00
return response.json(doc.getValidDict());
2018-01-12 12:25:07 +00:00
}));
// get document
2018-03-26 12:18:07 +00:00
app.get('/api/resource/:doctype/:name', frappe.asyncHandler(async function(request, response) {
2018-02-08 12:28:51 +00:00
let doc = await frappe.getDoc(request.params.doctype, request.params.name);
2018-02-08 11:45:32 +00:00
return response.json(doc.getValidDict());
2018-01-12 12:25:07 +00:00
}));
// get value
2018-03-26 12:18:07 +00:00
app.get('/api/resource/:doctype/:name/:fieldname', frappe.asyncHandler(async function(request, response) {
2018-02-08 09:38:47 +00:00
let value = await frappe.db.getValue(request.params.doctype, request.params.name, request.params.fieldname);
return response.json({value: value});
}));
2018-01-12 12:25:07 +00:00
// delete
2018-03-26 12:18:07 +00:00
app.delete('/api/resource/:doctype/:name', frappe.asyncHandler(async function(request, response) {
2018-02-08 12:28:51 +00:00
let doc = await frappe.getDoc(request.params.doctype, request.params.name)
2018-01-12 12:25:07 +00:00
await doc.delete();
return response.json({});
}));
2018-02-08 09:38:47 +00:00
// delete many
2018-03-26 12:18:07 +00:00
app.delete('/api/resource/:doctype', frappe.asyncHandler(async function(request, response) {
2018-02-08 09:38:47 +00:00
let names = request.body;
for (let name of names) {
2018-02-08 12:28:51 +00:00
let doc = await frappe.getDoc(request.params.doctype, name);
2018-02-08 09:38:47 +00:00
await doc.delete();
}
return response.json({});
}));
2018-01-12 12:25:07 +00:00
}
};