2
0
mirror of https://github.com/frappe/books.git synced 2025-01-23 23:28:24 +00:00
books/server/restAPI.js

78 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-01-16 11:39:17 +05:30
const frappe = require('frappejs');
2018-01-12 17:55:07 +05:30
module.exports = {
setup(app) {
// get list
2018-03-26 17:48:07 +05:30
app.get('/api/resource/:doctype', frappe.asyncHandler(async function(request, response) {
2018-02-12 17:31:31 +05:30
for (let key of ['fields', 'filters']) {
2018-01-12 17:55:07 +05:30
if (request.query[key]) {
request.query[key] = JSON.parse(request.query[key]);
}
}
let data = await frappe.db.getAll({
2018-01-12 17:55:07 +05:30
doctype: request.params.doctype,
2018-01-23 17:56:40 +05:30
fields: request.query.fields,
2018-01-12 17:55:07 +05:30
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 17:48:07 +05:30
app.post('/api/resource/:doctype', frappe.asyncHandler(async function(request, response) {
2018-02-08 15:08:47 +05:30
let data = request.body;
2018-01-12 17:55:07 +05:30
data.doctype = request.params.doctype;
2018-02-08 17:58:51 +05:30
let doc = frappe.newDoc(data);
2018-01-12 17:55:07 +05:30
await doc.insert();
await frappe.db.commit();
2018-02-08 17:15:32 +05:30
return response.json(doc.getValidDict());
2018-01-12 17:55:07 +05:30
}));
// update
2018-03-26 17:48:07 +05:30
app.put('/api/resource/:doctype/:name', frappe.asyncHandler(async function(request, response) {
2018-02-08 15:08:47 +05:30
let data = request.body;
2018-02-08 17:58:51 +05:30
let doc = await frappe.getDoc(request.params.doctype, request.params.name);
2018-01-12 17:55:07 +05:30
Object.assign(doc, data);
await doc.update();
await frappe.db.commit();
2018-02-08 17:15:32 +05:30
return response.json(doc.getValidDict());
2018-01-12 17:55:07 +05:30
}));
// get document
2018-03-26 17:48:07 +05:30
app.get('/api/resource/:doctype/:name', frappe.asyncHandler(async function(request, response) {
2018-02-08 17:58:51 +05:30
let doc = await frappe.getDoc(request.params.doctype, request.params.name);
2018-02-08 17:15:32 +05:30
return response.json(doc.getValidDict());
2018-01-12 17:55:07 +05:30
}));
// get value
2018-03-26 17:48:07 +05:30
app.get('/api/resource/:doctype/:name/:fieldname', frappe.asyncHandler(async function(request, response) {
2018-02-08 15:08:47 +05:30
let value = await frappe.db.getValue(request.params.doctype, request.params.name, request.params.fieldname);
return response.json({value: value});
}));
2018-01-12 17:55:07 +05:30
// delete
2018-03-26 17:48:07 +05:30
app.delete('/api/resource/:doctype/:name', frappe.asyncHandler(async function(request, response) {
2018-02-08 17:58:51 +05:30
let doc = await frappe.getDoc(request.params.doctype, request.params.name)
2018-01-12 17:55:07 +05:30
await doc.delete();
return response.json({});
}));
2018-02-08 15:08:47 +05:30
// delete many
2018-03-26 17:48:07 +05:30
app.delete('/api/resource/:doctype', frappe.asyncHandler(async function(request, response) {
2018-02-08 15:08:47 +05:30
let names = request.body;
for (let name of names) {
2018-02-08 17:58:51 +05:30
let doc = await frappe.getDoc(request.params.doctype, name);
2018-02-08 15:08:47 +05:30
await doc.delete();
}
return response.json({});
}));
2018-01-12 17:55:07 +05:30
}
};