Update view endpoint
This commit is contained in:
parent
5bd491b0a8
commit
29bc87a47f
|
@ -1,5 +1,10 @@
|
|||
import sdk from "../../../sdk"
|
||||
import { CreateViewRequest, Ctx, ViewResponse } from "@budibase/types"
|
||||
import {
|
||||
CreateViewRequest,
|
||||
Ctx,
|
||||
UpdateViewRequest,
|
||||
ViewResponse,
|
||||
} from "@budibase/types"
|
||||
|
||||
export async function create(ctx: Ctx<CreateViewRequest, ViewResponse>) {
|
||||
const view = ctx.request.body
|
||||
|
@ -12,6 +17,21 @@ export async function create(ctx: Ctx<CreateViewRequest, ViewResponse>) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function update(ctx: Ctx<UpdateViewRequest, ViewResponse>) {
|
||||
const view = ctx.request.body
|
||||
|
||||
if (view.version !== 2) {
|
||||
ctx.throw(400, "Only views V2 can be updated")
|
||||
}
|
||||
|
||||
const { tableId } = view
|
||||
|
||||
const result = await sdk.views.update(tableId, view)
|
||||
ctx.body = {
|
||||
data: result,
|
||||
}
|
||||
}
|
||||
|
||||
export async function remove(ctx: Ctx) {
|
||||
const { viewId } = ctx.params
|
||||
|
||||
|
|
|
@ -13,6 +13,11 @@ router
|
|||
authorized(permissions.BUILDER),
|
||||
viewController.v2.create
|
||||
)
|
||||
.put(
|
||||
`/api/v2/views`,
|
||||
authorized(permissions.BUILDER),
|
||||
viewController.v2.update
|
||||
)
|
||||
.delete(
|
||||
`/api/v2/views/:viewId`,
|
||||
authorized(permissions.BUILDER),
|
||||
|
|
|
@ -33,6 +33,24 @@ export async function create(
|
|||
return view
|
||||
}
|
||||
|
||||
export async function update(tableId: string, view: ViewV2): Promise<ViewV2> {
|
||||
const db = context.getAppDB()
|
||||
const table = await sdk.tables.getTable(tableId)
|
||||
table.views ??= {}
|
||||
|
||||
const existingView = Object.values(table.views).find(
|
||||
v => isV2(v) && v.id === view.id
|
||||
)
|
||||
if (!existingView) {
|
||||
throw new HTTPError(`View ${view.id} not found in table ${tableId}`, 404)
|
||||
}
|
||||
|
||||
delete table.views[existingView.name]
|
||||
table.views[view.name] = view
|
||||
await db.put(table)
|
||||
return view
|
||||
}
|
||||
|
||||
export function isV2(view: View | ViewV2): view is ViewV2 {
|
||||
return (view as ViewV2).version === 2
|
||||
}
|
||||
|
|
|
@ -5,3 +5,5 @@ export interface ViewResponse {
|
|||
}
|
||||
|
||||
export type CreateViewRequest = Omit<ViewV2, "version" | "id">
|
||||
|
||||
export type UpdateViewRequest = ViewV2
|
||||
|
|
Loading…
Reference in New Issue