2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 20:11:15 +00:00
books/server/restAPI.js

118 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-01-16 06:09:17 +00:00
const frappe = require('frappejs');
const path = require('path');
const multer = require('multer');
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
}));
const upload = multer({
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, frappe.conf.staticPath)
},
filename: (req, file, cb) => {
const filename = file.originalname.split('.')[0];
const extension = path.extname(file.originalname);
const now = Date.now();
cb(null, filename + '-' + now + extension);
}
})
});
app.post('/api/upload/:doctype/:name/:fieldname', upload.array('files', 10), frappe.asyncHandler(async function(request, response) {
const files = request.files;
const { doctype, name, fieldname } = request.params;
let fileDocs = [];
for (let file of files) {
const doc = frappe.newDoc({
doctype: 'File',
name: path.join('/', file.path),
filename: file.originalname,
mimetype: file.mimetype,
size: file.size,
referenceDoctype: doctype,
referenceName: name,
referenceFieldname: fieldname
});
await doc.insert();
await frappe.db.setValue(doctype, name, fieldname, doc.name);
fileDocs.push(doc.getValidDict());
}
return response.json(fileDocs);
}));
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
}
};