1
0
Fork 0
mirror of synced 2024-10-03 19:43:32 +13:00

Update view endpoint

This commit is contained in:
Adria Navarro 2023-07-25 15:33:49 +02:00
parent 5bd491b0a8
commit 29bc87a47f
4 changed files with 46 additions and 1 deletions

View file

@ -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

View file

@ -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),

View file

@ -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
}

View file

@ -5,3 +5,5 @@ export interface ViewResponse {
}
export type CreateViewRequest = Omit<ViewV2, "version" | "id">
export type UpdateViewRequest = ViewV2