Refactor paths

This commit is contained in:
Adria Navarro 2023-07-12 18:01:46 +02:00
parent 7155d75906
commit 4bbb1b0289
3 changed files with 31 additions and 11 deletions

View File

@ -7,12 +7,17 @@ export async function fetch(ctx: Ctx) {
}
export async function find(ctx: Ctx) {
const viewId = `${DocumentType.VIEW}${SEPARATOR}${ctx.params.viewId}`
ctx.body = await sdk.views.get(viewId)
const { tableId, viewId } = ctx.params
const result = await sdk.views.get(viewId)
if (result?.tableId !== tableId) {
ctx.throw(404)
}
ctx.body = result
}
export async function findByTable(ctx: Ctx) {
const tableId = `${DocumentType.TABLE}${SEPARATOR}${ctx.params.tableId}`
const { tableId } = ctx.params
ctx.body = { views: await sdk.views.findByTable(tableId) }
}

View File

@ -1,6 +1,6 @@
import * as setup from "./utilities"
import { FieldType, Table, ViewV2 } from "@budibase/types"
import { generator } from "@budibase/backend-core/tests"
import { generator, structures } from "@budibase/backend-core/tests"
import sdk from "../../../sdk"
function priceTable(): Table {
@ -45,12 +45,17 @@ describe("/views/v2", () => {
.expect(200)
}
const getView = async (viewId: string) => {
const getView = ({
tableId,
viewId,
}: {
tableId: string
viewId: string
}) => {
return request
.get(`/api/views/v2/${viewId}`)
.get(`/api/views/v2/${tableId}/${viewId}`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
}
function createView(tableId: string): ViewV2 {
@ -114,8 +119,11 @@ describe("/views/v2", () => {
view = (await saveView(createView(table._id!))).body
})
it("persist the view when the view is successfully created", async () => {
const res = await getView(view._id)
it("can fetch the expected view", async () => {
const res = await getView({
tableId: view.tableId,
viewId: view._id,
}).expect(200)
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()
@ -127,6 +135,13 @@ describe("/views/v2", () => {
updatedAt: expect.any(String),
})
})
it("will return 404 if the wrong table id is provided", async () => {
await getView({
tableId: structures.generator.guid(),
viewId: view._id,
}).expect(404)
})
})
describe("create", () => {

View File

@ -14,12 +14,12 @@ router
viewController.v2.fetch
)
.get(
`/api/views/v2/${DocumentType.TABLE}${SEPARATOR}:tableId`,
`/api/views/v2/:tableId`,
authorized(permissions.BUILDER),
viewController.v2.findByTable
)
.get(
`/api/views/v2/${DocumentType.VIEW}${SEPARATOR}:viewId`,
`/api/views/v2/:tableId/:viewId`,
authorized(permissions.BUILDER),
viewController.v2.find
)