Browse Source

refactor: path correction and error status code update

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5901/head
Pranav C 1 year ago
parent
commit
e7ff641b61
  1. 22
      packages/nocodb/src/controllers/data-table.controller.ts
  2. 2529
      packages/nocodb/src/db/BaseModelSqlv2.ts
  3. 8
      packages/nocodb/src/helpers/catchError.ts
  4. 7
      packages/nocodb/src/services/data-table.service.ts

22
packages/nocodb/src/controllers/data-table.controller.ts

@ -26,19 +26,17 @@ export class DataTableController {
constructor(private readonly dataTableService: DataTableService) {}
// todo: Handle the error case where view doesnt belong to model
@Get('/api/v1/base/:projectId/tables/:modelId')
@Get('/api/v1/tables/:modelId')
@Acl('dataList')
async dataList(
@Request() req,
@Response() res,
@Param('projectId') projectId: string,
@Param('modelId') modelId: string,
@Query('viewId') viewId: string,
) {
const startTime = process.hrtime();
const responseData = await this.dataTableService.dataList({
query: req.query,
projectId: projectId,
modelId: modelId,
viewId: viewId,
});
@ -47,12 +45,11 @@ export class DataTableController {
res.json(responseData);
}
@Get(['/api/v1/base/:projectId/tables/:modelId/count'])
@Get(['/api/v1/tables/:modelId/count'])
@Acl('dataCount')
async dataCount(
@Request() req,
@Response() res,
@Param('projectId') projectId: string,
@Param('modelId') modelId: string,
@Query('viewId') viewId: string,
) {
@ -60,24 +57,21 @@ export class DataTableController {
query: req.query,
modelId,
viewId,
projectId,
});
res.json(countResult);
}
@Post(['/api/v1/base/:projectId/tables/:modelId'])
@Post(['/api/v1/tables/:modelId'])
@HttpCode(200)
@Acl('dataInsert')
async dataInsert(
@Request() req,
@Param('projectId') projectId: string,
@Param('modelId') modelId: string,
@Query('viewId') viewId: string,
@Body() body: any,
) {
return await this.dataTableService.dataInsert({
projectId: projectId,
modelId: modelId,
body: body,
viewId,
@ -85,41 +79,35 @@ export class DataTableController {
});
}
@Patch(['/api/v1/base/:projectId/tables/:modelId'])
@Patch(['/api/v1/tables/:modelId'])
@Acl('dataUpdate')
async dataUpdate(
@Request() req,
@Param('modelId') modelId: string,
@Query('viewId') viewId: string,
@Param('rowId') rowId: string,
@Param('projectId') projectId: string,
) {
return await this.dataTableService.dataUpdate({
modelId: modelId,
body: req.body,
cookie: req,
viewId,
// rowId: rowId,
projectId,
});
}
@Delete(['/api/v1/base/:projectId/tables/:modelId'])
@Delete(['/api/v1/tables/:modelId'])
@Acl('dataDelete')
async dataDelete(
@Request() req,
@Param('modelId') modelId: string,
@Query('viewId') viewId: string,
@Param('rowId') rowId: string,
@Param('projectId') projectId: string,
) {
return await this.dataTableService.dataDelete({
modelId: modelId,
cookie: req,
viewId,
body: req.body,
// rowId: rowId,
projectId,
});
}

2529
packages/nocodb/src/db/BaseModelSqlv2.ts

File diff suppressed because it is too large Load Diff

8
packages/nocodb/src/helpers/catchError.ts

@ -413,6 +413,8 @@ export default function (
return res.status(501).json({ msg: e.message });
} else if (e instanceof AjvError) {
return res.status(400).json({ msg: e.message, errors: e.errors });
} else if (e instanceof UnprocessableEntity) {
return res.status(422).json({ msg: e.message });
}
next(e);
}
@ -431,6 +433,8 @@ export class InternalServerError extends Error {}
export class NotImplemented extends Error {}
export class UnprocessableEntity extends Error {}
export class AjvError extends Error {
constructor(param: { message: string; errors: ErrorObject[] }) {
super(param.message);
@ -468,4 +472,8 @@ export class NcError {
static ajvValidationError(param: { message: string; errors: ErrorObject[] }) {
throw new AjvError(param);
}
static unprocessableEntity(message = 'Unprocessable entity') {
throw new UnprocessableEntity(message);
}
}

7
packages/nocodb/src/services/data-table.service.ts

@ -2,7 +2,6 @@ import { Injectable } from '@nestjs/common';
import { NcError } from '../helpers/catchError';
import { Base, Model, View } from '../models';
import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2';
import projectAcl from '../utils/projectAcl';
import { DatasService } from './datas.service';
@Injectable()
@ -173,7 +172,7 @@ export class DataTableService {
const model = await Model.get(param.modelId);
if (!model) {
throw new Error('Model not found');
NcError.notFound('Model not found');
}
if (param.projectId && model.project_id !== param.projectId) {
@ -184,8 +183,8 @@ export class DataTableService {
if (param.viewId) {
view = await View.get(param.viewId);
if (view.fk_model_id && view.fk_model_id !== param.modelId) {
throw new Error('View not belong to model');
if (!view || (view.fk_model_id && view.fk_model_id !== param.modelId)) {
NcError.unprocessableEntity('View not belong to model');
}
}

Loading…
Cancel
Save