Simplifying endpoints
This commit is contained in:
parent
4ca25ee065
commit
02bc1d2cdc
|
@ -2,24 +2,26 @@ import sdk from "../../../sdk"
|
|||
import { Ctx, ViewV2 } from "@budibase/types"
|
||||
|
||||
export async function fetch(ctx: Ctx) {
|
||||
ctx.body = { views: await sdk.views.fetch() }
|
||||
const { tableId } = ctx.query
|
||||
|
||||
if (tableId && typeof tableId !== "string") {
|
||||
ctx.throw(400, "tableId type is not valid")
|
||||
}
|
||||
|
||||
const views = tableId
|
||||
? await sdk.views.findByTable(tableId)
|
||||
: await sdk.views.fetch()
|
||||
|
||||
ctx.body = { views }
|
||||
}
|
||||
|
||||
export async function find(ctx: Ctx) {
|
||||
const { tableId, viewId } = ctx.params
|
||||
const { 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 } = ctx.params
|
||||
ctx.body = { views: await sdk.views.findByTable(tableId) }
|
||||
}
|
||||
|
||||
export async function save(ctx: Ctx<ViewV2>) {
|
||||
const view = ctx.request.body
|
||||
const result = await sdk.views.save(view)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import * as setup from "./utilities"
|
||||
import { FieldType, Table, ViewV2 } from "@budibase/types"
|
||||
import { generator, structures } from "@budibase/backend-core/tests"
|
||||
import sdk from "../../../sdk"
|
||||
|
||||
function priceTable(): Table {
|
||||
return {
|
||||
|
@ -45,15 +44,9 @@ describe("/views/v2", () => {
|
|||
.expect(200)
|
||||
}
|
||||
|
||||
const getView = ({
|
||||
tableId,
|
||||
viewId,
|
||||
}: {
|
||||
tableId: string
|
||||
viewId: string
|
||||
}) => {
|
||||
const getView = (viewId: string) => {
|
||||
return request
|
||||
.get(`/api/views/v2/${tableId}/${viewId}`)
|
||||
.get(`/api/views/v2/${viewId}`)
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
}
|
||||
|
@ -88,28 +81,36 @@ describe("/views/v2", () => {
|
|||
expect.arrayContaining(views.map(v => expect.objectContaining(v)))
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("findByTable", () => {
|
||||
const views: any[] = []
|
||||
|
||||
beforeAll(async () => {
|
||||
table = await config.createTable(priceTable())
|
||||
it("can filter by table id", async () => {
|
||||
const newTable = await config.createTable(priceTable())
|
||||
const newViews = []
|
||||
for (let id = 0; id < 5; id++) {
|
||||
const res = await saveView(createView(table._id!))
|
||||
views.push(res.body)
|
||||
const res = await saveView(createView(newTable._id!))
|
||||
newViews.push(res.body)
|
||||
}
|
||||
})
|
||||
|
||||
it("returns all views", async () => {
|
||||
const res = await request
|
||||
.get(`/api/views/v2/${table._id}`)
|
||||
.get(`/api/views/v2?tableId=${newTable._id}`)
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(200)
|
||||
|
||||
expect(res.body.views.length).toBe(5)
|
||||
expect(res.body.views).toEqual(expect.arrayContaining([]))
|
||||
expect(res.body.views).toEqual(
|
||||
expect.arrayContaining(newViews.map(v => expect.objectContaining(v)))
|
||||
)
|
||||
})
|
||||
|
||||
it("can not filter by multiple table ids", async () => {
|
||||
const res = await request
|
||||
.get(
|
||||
`/api/views/v2?tableId=${structures.generator.guid()}&tableId=${structures.generator.guid()}`
|
||||
)
|
||||
.set(config.defaultHeaders())
|
||||
.expect("Content-Type", /json/)
|
||||
.expect(400)
|
||||
|
||||
expect(res.body.message).toBe("tableId type is not valid")
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -120,10 +121,7 @@ describe("/views/v2", () => {
|
|||
})
|
||||
|
||||
it("can fetch the expected view", async () => {
|
||||
const res = await getView({
|
||||
tableId: view.tableId,
|
||||
viewId: view._id,
|
||||
}).expect(200)
|
||||
const res = await getView(view._id).expect(200)
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.body._id).toBeDefined()
|
||||
|
||||
|
@ -136,11 +134,8 @@ describe("/views/v2", () => {
|
|||
})
|
||||
})
|
||||
|
||||
it("will return 404 if the wrong table id is provided", async () => {
|
||||
await getView({
|
||||
tableId: structures.generator.guid(),
|
||||
viewId: view._id,
|
||||
}).expect(404)
|
||||
it("will return 404 if the unnexisting id is provided", async () => {
|
||||
await getView(structures.generator.guid()).expect(404)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -168,20 +163,14 @@ describe("/views/v2", () => {
|
|||
})
|
||||
|
||||
it("can delete an existing view", async () => {
|
||||
await getView({
|
||||
tableId: view.tableId,
|
||||
viewId: view._id,
|
||||
}).expect(200)
|
||||
await getView(view._id).expect(200)
|
||||
|
||||
await request
|
||||
.delete(`/api/views/v2/${view.tableId}/${view._id}`)
|
||||
.delete(`/api/views/v2/${view._id}`)
|
||||
.set(config.defaultHeaders())
|
||||
.expect(204)
|
||||
|
||||
await getView({
|
||||
tableId: view.tableId,
|
||||
viewId: view._id,
|
||||
}).expect(404)
|
||||
await getView(view._id).expect(404)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -14,12 +14,7 @@ router
|
|||
viewController.v2.fetch
|
||||
)
|
||||
.get(
|
||||
`/api/views/v2/:tableId`,
|
||||
authorized(permissions.BUILDER),
|
||||
viewController.v2.findByTable
|
||||
)
|
||||
.get(
|
||||
`/api/views/v2/:tableId/:viewId`,
|
||||
`/api/views/v2/:viewId`,
|
||||
authorized(permissions.BUILDER),
|
||||
viewController.v2.find
|
||||
)
|
||||
|
@ -29,7 +24,7 @@ router
|
|||
viewController.v2.save
|
||||
)
|
||||
.delete(
|
||||
`/api/views/v2/:tableId/:viewId`,
|
||||
`/api/views/v2/:viewId`,
|
||||
authorized(permissions.BUILDER),
|
||||
viewController.v2.remove
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue