2022-02-24 16:13:14 +01:00
|
|
|
import { default as rowController } from "../row"
|
2022-03-01 15:37:35 +01:00
|
|
|
import { addRev, wrapResponse } from "./utils"
|
2022-02-24 16:13:14 +01:00
|
|
|
import { Row } from "../../../definitions/common"
|
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
|
2022-02-24 16:13:14 +01:00
|
|
|
function fixRow(row: Row, params: any) {
|
2022-02-18 18:44:08 +01:00
|
|
|
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-24 16:13:14 +01:00
|
|
|
export async function search(ctx: any) {
|
|
|
|
let { sort, paginate, bookmark, limit, query } = ctx.request.body
|
|
|
|
// update the body to the correct format of the internal search
|
|
|
|
if (!sort) {
|
|
|
|
sort = {}
|
|
|
|
}
|
|
|
|
ctx.request.body = {
|
|
|
|
sort: sort.column,
|
|
|
|
sortType: sort.type,
|
|
|
|
sortOrder: sort.order,
|
|
|
|
bookmark,
|
|
|
|
paginate,
|
|
|
|
limit,
|
|
|
|
query,
|
|
|
|
}
|
2022-02-22 18:38:27 +01:00
|
|
|
await rowController.search(ctx)
|
2022-03-01 15:37:35 +01:00
|
|
|
ctx.body.data = ctx.body.rows
|
|
|
|
delete ctx.body.rows
|
2022-02-17 19:58:09 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 16:13:14 +01:00
|
|
|
export async function create(ctx: any) {
|
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)
|
2022-03-01 15:37:35 +01:00
|
|
|
wrapResponse(ctx)
|
2022-02-17 19:58:09 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 16:13:14 +01:00
|
|
|
export async function read(ctx: any) {
|
2022-02-28 13:54:32 +01:00
|
|
|
await rowController.fetchEnrichedRow(ctx)
|
2022-03-01 15:37:35 +01:00
|
|
|
wrapResponse(ctx)
|
2022-02-23 19:31:32 +01:00
|
|
|
}
|
2022-02-17 19:58:09 +01:00
|
|
|
|
2022-02-24 16:13:14 +01:00
|
|
|
export async function update(ctx: any) {
|
2022-02-23 23:13:16 +01:00
|
|
|
ctx.request.body = await addRev(fixRow(ctx.request.body, ctx.params.tableId))
|
2022-02-25 19:02:08 +01:00
|
|
|
await rowController.save(ctx)
|
2022-03-01 15:37:35 +01:00
|
|
|
wrapResponse(ctx)
|
2022-02-17 19:58:09 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 16:13:14 +01:00
|
|
|
export async function destroy(ctx: any) {
|
2022-02-23 19:31:32 +01:00
|
|
|
// set the body as expected, with the _id and _rev fields
|
2022-02-25 19:02:08 +01:00
|
|
|
ctx.request.body = await addRev(
|
|
|
|
fixRow({ _id: ctx.params.rowId }, ctx.params.tableId)
|
|
|
|
)
|
2022-02-23 19:31:32 +01:00
|
|
|
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
|
2022-03-01 15:37:35 +01:00
|
|
|
wrapResponse(ctx)
|
2022-02-23 19:31:32 +01:00
|
|
|
}
|
2022-02-24 16:13:14 +01:00
|
|
|
|
|
|
|
export default {
|
|
|
|
create,
|
|
|
|
read,
|
|
|
|
update,
|
|
|
|
destroy,
|
|
|
|
search,
|
|
|
|
}
|