Browse Source

refactor: path correction and error status code update

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5901/head
Pranav C 2 years ago
parent
commit
e7ff641b61
  1. 22
      packages/nocodb/src/controllers/data-table.controller.ts
  2. 2069
      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) {} constructor(private readonly dataTableService: DataTableService) {}
// todo: Handle the error case where view doesnt belong to model // 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') @Acl('dataList')
async dataList( async dataList(
@Request() req, @Request() req,
@Response() res, @Response() res,
@Param('projectId') projectId: string,
@Param('modelId') modelId: string, @Param('modelId') modelId: string,
@Query('viewId') viewId: string, @Query('viewId') viewId: string,
) { ) {
const startTime = process.hrtime(); const startTime = process.hrtime();
const responseData = await this.dataTableService.dataList({ const responseData = await this.dataTableService.dataList({
query: req.query, query: req.query,
projectId: projectId,
modelId: modelId, modelId: modelId,
viewId: viewId, viewId: viewId,
}); });
@ -47,12 +45,11 @@ export class DataTableController {
res.json(responseData); res.json(responseData);
} }
@Get(['/api/v1/base/:projectId/tables/:modelId/count']) @Get(['/api/v1/tables/:modelId/count'])
@Acl('dataCount') @Acl('dataCount')
async dataCount( async dataCount(
@Request() req, @Request() req,
@Response() res, @Response() res,
@Param('projectId') projectId: string,
@Param('modelId') modelId: string, @Param('modelId') modelId: string,
@Query('viewId') viewId: string, @Query('viewId') viewId: string,
) { ) {
@ -60,24 +57,21 @@ export class DataTableController {
query: req.query, query: req.query,
modelId, modelId,
viewId, viewId,
projectId,
}); });
res.json(countResult); res.json(countResult);
} }
@Post(['/api/v1/base/:projectId/tables/:modelId']) @Post(['/api/v1/tables/:modelId'])
@HttpCode(200) @HttpCode(200)
@Acl('dataInsert') @Acl('dataInsert')
async dataInsert( async dataInsert(
@Request() req, @Request() req,
@Param('projectId') projectId: string,
@Param('modelId') modelId: string, @Param('modelId') modelId: string,
@Query('viewId') viewId: string, @Query('viewId') viewId: string,
@Body() body: any, @Body() body: any,
) { ) {
return await this.dataTableService.dataInsert({ return await this.dataTableService.dataInsert({
projectId: projectId,
modelId: modelId, modelId: modelId,
body: body, body: body,
viewId, viewId,
@ -85,41 +79,35 @@ export class DataTableController {
}); });
} }
@Patch(['/api/v1/base/:projectId/tables/:modelId']) @Patch(['/api/v1/tables/:modelId'])
@Acl('dataUpdate') @Acl('dataUpdate')
async dataUpdate( async dataUpdate(
@Request() req, @Request() req,
@Param('modelId') modelId: string, @Param('modelId') modelId: string,
@Query('viewId') viewId: string, @Query('viewId') viewId: string,
@Param('rowId') rowId: string, @Param('rowId') rowId: string,
@Param('projectId') projectId: string,
) { ) {
return await this.dataTableService.dataUpdate({ return await this.dataTableService.dataUpdate({
modelId: modelId, modelId: modelId,
body: req.body, body: req.body,
cookie: req, cookie: req,
viewId, viewId,
// rowId: rowId,
projectId,
}); });
} }
@Delete(['/api/v1/base/:projectId/tables/:modelId']) @Delete(['/api/v1/tables/:modelId'])
@Acl('dataDelete') @Acl('dataDelete')
async dataDelete( async dataDelete(
@Request() req, @Request() req,
@Param('modelId') modelId: string, @Param('modelId') modelId: string,
@Query('viewId') viewId: string, @Query('viewId') viewId: string,
@Param('rowId') rowId: string, @Param('rowId') rowId: string,
@Param('projectId') projectId: string,
) { ) {
return await this.dataTableService.dataDelete({ return await this.dataTableService.dataDelete({
modelId: modelId, modelId: modelId,
cookie: req, cookie: req,
viewId, viewId,
body: req.body, body: req.body,
// rowId: rowId,
projectId,
}); });
} }

2069
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 }); return res.status(501).json({ msg: e.message });
} else if (e instanceof AjvError) { } else if (e instanceof AjvError) {
return res.status(400).json({ msg: e.message, errors: e.errors }); 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); next(e);
} }
@ -431,6 +433,8 @@ export class InternalServerError extends Error {}
export class NotImplemented extends Error {} export class NotImplemented extends Error {}
export class UnprocessableEntity extends Error {}
export class AjvError extends Error { export class AjvError extends Error {
constructor(param: { message: string; errors: ErrorObject[] }) { constructor(param: { message: string; errors: ErrorObject[] }) {
super(param.message); super(param.message);
@ -468,4 +472,8 @@ export class NcError {
static ajvValidationError(param: { message: string; errors: ErrorObject[] }) { static ajvValidationError(param: { message: string; errors: ErrorObject[] }) {
throw new AjvError(param); 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 { NcError } from '../helpers/catchError';
import { Base, Model, View } from '../models'; import { Base, Model, View } from '../models';
import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2'; import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2';
import projectAcl from '../utils/projectAcl';
import { DatasService } from './datas.service'; import { DatasService } from './datas.service';
@Injectable() @Injectable()
@ -173,7 +172,7 @@ export class DataTableService {
const model = await Model.get(param.modelId); const model = await Model.get(param.modelId);
if (!model) { if (!model) {
throw new Error('Model not found'); NcError.notFound('Model not found');
} }
if (param.projectId && model.project_id !== param.projectId) { if (param.projectId && model.project_id !== param.projectId) {
@ -184,8 +183,8 @@ export class DataTableService {
if (param.viewId) { if (param.viewId) {
view = await View.get(param.viewId); view = await View.get(param.viewId);
if (view.fk_model_id && view.fk_model_id !== param.modelId) { if (!view || (view.fk_model_id && view.fk_model_id !== param.modelId)) {
throw new Error('View not belong to model'); NcError.unprocessableEntity('View not belong to model');
} }
} }

Loading…
Cancel
Save