Fix search

This commit is contained in:
Adria Navarro 2023-07-19 16:04:53 +02:00
parent 9bf22213b2
commit ac0ae34808
5 changed files with 48 additions and 18 deletions

View File

@ -147,12 +147,12 @@ export async function search(ctx: any) {
}
export async function searchView(ctx: Ctx<void, SearchResponse>) {
const { viewId } = ctx.params
const view = await sdk.views.get(viewId)
const { tableId, viewId } = ctx.params
const view = await sdk.views.get(tableId, viewId)
if (!view) {
ctx.throw(404)
ctx.throw(404, `View ${viewId} not found in table ${tableId}`)
}
const tableId = view.tableId
ctx.status = 200
ctx.body = await quotas.addQuery(

View File

@ -147,7 +147,7 @@ router
rowController.search
)
.get(
"/api/v2/views/:viewId/search",
"/api/v2/views/:tableId/:viewId/search",
authorized(PermissionType.VIEW, PermissionLevel.READ),
rowController.searchView
)

View File

@ -716,8 +716,10 @@ describe("/rows", () => {
}
const createViewResponse = await config.api.viewV2.create()
const response = await config.api.viewV2.search(createViewResponse._id!)
const response = await config.api.viewV2.search(
createViewResponse.tableId,
createViewResponse.id
)
expect(response.body.rows).toHaveLength(10)
expect(response.body).toEqual({
@ -744,11 +746,17 @@ describe("/rows", () => {
})
)
const createViewResponse = await config.api.viewV2.create({
const createViewResponse = await config.api.viewV2.create(
config.table?._id!,
{
query: { equal: { age: 40 } },
})
}
)
const response = await config.api.viewV2.search(createViewResponse._id!)
const response = await config.api.viewV2.search(
createViewResponse.tableId,
createViewResponse.id
)
expect(response.body.rows).toHaveLength(5)
expect(response.body).toEqual({
@ -831,11 +839,17 @@ describe("/rows", () => {
})
}
const createViewResponse = await config.api.viewV2.create({
const createViewResponse = await config.api.viewV2.create(
config.table?._id!,
{
sort: sortParams,
})
}
)
const response = await config.api.viewV2.search(createViewResponse._id!)
const response = await config.api.viewV2.search(
createViewResponse.tableId,
createViewResponse.id
)
expect(response.body.rows).toHaveLength(4)
expect(response.body).toEqual({

View File

@ -4,6 +4,17 @@ import { ViewV2 } from "@budibase/types"
import sdk from "../../../sdk"
import { utils as coreUtils } from "@budibase/backend-core"
export async function get(
tableId: string,
viewId: string
): Promise<ViewV2 | undefined> {
const table = await sdk.tables.getTable(tableId)
const view = Object.values(table.views!).find(v => isV2(v) && v.id === viewId)
// @ts-ignore TODO
return view
}
export async function create(
tableId: string,
viewRequest: Omit<ViewV2, "id" | "version">
@ -32,10 +43,11 @@ export async function remove(tableId: string, viewId: string): Promise<void> {
const db = context.getAppDB()
const table = await sdk.tables.getTable(tableId)
const view = Object.values(table.views!).find(v => isV2(v) && v.id === viewId)
const view = await get(tableId, viewId)
if (!view) {
throw new HTTPError(`View ${viewId} not found in table ${tableId}`, 404)
}
delete table.views![view?.name]
await db.put(table)
}

View File

@ -42,9 +42,13 @@ export class ViewV2API extends TestAPI {
.expect(expectStatus)
}
search = async (viewId: string, { expectStatus } = { expectStatus: 200 }) => {
search = async (
tableId: string,
viewId: string,
{ expectStatus } = { expectStatus: 200 }
) => {
return this.request
.get(`/api/v2/views/${viewId}/search`)
.get(`/api/v2/views/${tableId}/${viewId}/search`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(expectStatus)