2022-02-22 18:38:27 +01:00
|
|
|
const rowController = require("../row")
|
2022-02-23 19:31:32 +01:00
|
|
|
const { addRev } = require("./utils")
|
2022-02-22 18:38:27 +01:00
|
|
|
|
2022-02-18 18:44:08 +01:00
|
|
|
// makes sure that the user doesn't need to pass in the type, tableId or _id params for
|
|
|
|
// the call to be correct
|
|
|
|
function fixRow(row, params) {
|
|
|
|
if (!params || !row) {
|
|
|
|
return row
|
|
|
|
}
|
|
|
|
if (params.rowId) {
|
|
|
|
row._id = params.rowId
|
|
|
|
}
|
|
|
|
if (params.tableId) {
|
|
|
|
row.tableId = params.tableId
|
|
|
|
}
|
|
|
|
if (!row.type) {
|
|
|
|
row.type = "row"
|
|
|
|
}
|
|
|
|
return row
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:38:27 +01:00
|
|
|
exports.search = async ctx => {
|
|
|
|
await rowController.search(ctx)
|
2022-02-17 19:58:09 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 19:31:32 +01:00
|
|
|
exports.create = async ctx => {
|
2022-02-18 18:44:08 +01:00
|
|
|
ctx.request.body = fixRow(ctx.request.body, ctx.params)
|
2022-02-23 19:31:32 +01:00
|
|
|
await rowController.save(ctx)
|
|
|
|
ctx.body = { row: ctx.body }
|
2022-02-17 19:58:09 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 19:31:32 +01:00
|
|
|
exports.read = async ctx => {
|
|
|
|
await rowController.find(ctx)
|
|
|
|
ctx.body = { row: ctx.body }
|
|
|
|
}
|
2022-02-17 19:58:09 +01:00
|
|
|
|
2022-02-18 18:44:08 +01:00
|
|
|
exports.update = async ctx => {
|
2022-02-23 19:31:32 +01:00
|
|
|
ctx.request.body = await addRev(fixRow(ctx.request.body, ctx.params))
|
|
|
|
ctx.body = { row: ctx.body }
|
2022-02-17 19:58:09 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 19:31:32 +01:00
|
|
|
exports.delete = async ctx => {
|
|
|
|
// set the body as expected, with the _id and _rev fields
|
|
|
|
ctx.request.body = await addRev({ _id: ctx.params.rowId })
|
|
|
|
await rowController.destroy(ctx)
|
|
|
|
// destroy controller doesn't currently return the row as the body, need to adjust this
|
|
|
|
// in the public API to be correct
|
|
|
|
ctx.body = { row: ctx.row }
|
|
|
|
}
|