mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
6 changed files with 145 additions and 1 deletions
@ -0,0 +1,20 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { FormsController } from './forms.controller'; |
||||
import { FormsService } from './forms.service'; |
||||
|
||||
describe('FormsController', () => { |
||||
let controller: FormsController; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
controllers: [FormsController], |
||||
providers: [FormsService], |
||||
}).compile(); |
||||
|
||||
controller = module.get<FormsController>(FormsController); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(controller).toBeDefined(); |
||||
}); |
||||
}); |
@ -0,0 +1,54 @@
|
||||
import { |
||||
Body, |
||||
Controller, |
||||
Get, |
||||
Param, |
||||
Patch, |
||||
Post, |
||||
UseGuards, |
||||
} from '@nestjs/common'; |
||||
import { ViewCreateReqType } from 'nocodb-sdk'; |
||||
import { |
||||
Acl, |
||||
ExtractProjectIdMiddleware, |
||||
} from '../../middlewares/extract-project-id/extract-project-id.middleware'; |
||||
import { FormsService } from './forms.service'; |
||||
import { AuthGuard } from '@nestjs/passport'; |
||||
|
||||
@Controller('forms') |
||||
@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) |
||||
export class FormsController { |
||||
constructor(private readonly formsService: FormsService) {} |
||||
|
||||
@Get('/api/v1/db/meta/forms/:formViewId') |
||||
@Acl('formViewGet') |
||||
async formViewGet(@Param('formViewId') formViewId: string) { |
||||
const formViewData = await this.formsService.formViewGet({ |
||||
formViewId, |
||||
}); |
||||
return formViewData; |
||||
} |
||||
|
||||
@Post('/api/v1/db/meta/tables/:tableId/forms') |
||||
@Acl('formViewCreate') |
||||
async formViewCreate( |
||||
@Param('tableId') tableId: string, |
||||
@Body() body: ViewCreateReqType, |
||||
) { |
||||
const view = await this.formsService.formViewCreate({ |
||||
body, |
||||
tableId, |
||||
}); |
||||
return view; |
||||
} |
||||
@Patch('/api/v1/db/meta/forms/:formViewId') |
||||
@Acl('formViewUpdate') |
||||
async formViewUpdate(req, res) { |
||||
res.json( |
||||
await this.formsService.formViewUpdate({ |
||||
formViewId: req.params.formViewId, |
||||
form: req.body, |
||||
}), |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'; |
||||
import { FormsService } from './forms.service'; |
||||
import { FormsController } from './forms.controller'; |
||||
|
||||
@Module({ |
||||
controllers: [FormsController], |
||||
providers: [FormsService] |
||||
}) |
||||
export class FormsModule {} |
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'; |
||||
import { FormsService } from './forms.service'; |
||||
|
||||
describe('FormsService', () => { |
||||
let service: FormsService; |
||||
|
||||
beforeEach(async () => { |
||||
const module: TestingModule = await Test.createTestingModule({ |
||||
providers: [FormsService], |
||||
}).compile(); |
||||
|
||||
service = module.get<FormsService>(FormsService); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(service).toBeDefined(); |
||||
}); |
||||
}); |
@ -0,0 +1,42 @@
|
||||
import { Injectable } from '@nestjs/common'; |
||||
|
||||
import { T } from 'nc-help'; |
||||
import { FormUpdateReqType, ViewCreateReqType, ViewTypes } from 'nocodb-sdk'; |
||||
import { validatePayload } from '../../helpers'; |
||||
import { FormView, View } from '../../models'; |
||||
|
||||
@Injectable() |
||||
export class FormsService { |
||||
async formViewGet(param: { formViewId: string }) { |
||||
const formViewData = await FormView.getWithInfo(param.formViewId); |
||||
return formViewData; |
||||
} |
||||
|
||||
async formViewCreate(param: { tableId: string; body: ViewCreateReqType }) { |
||||
validatePayload( |
||||
'swagger.json#/components/schemas/ViewCreateReq', |
||||
param.body, |
||||
); |
||||
|
||||
const view = await View.insert({ |
||||
...param.body, |
||||
// todo: sanitize
|
||||
fk_model_id: param.tableId, |
||||
type: ViewTypes.FORM, |
||||
}); |
||||
|
||||
T.emit('evt', { evt_type: 'vtable:created', show_as: 'form' }); |
||||
|
||||
return view; |
||||
} |
||||
|
||||
async formViewUpdate(param: { formViewId: string; form: FormUpdateReqType }) { |
||||
validatePayload( |
||||
'swagger.json#/components/schemas/FormUpdateReq', |
||||
param.form, |
||||
); |
||||
|
||||
T.emit('evt', { evt_type: 'view:updated', type: 'form' }); |
||||
return await FormView.update(param.formViewId, param.form); |
||||
} |
||||
} |
Loading…
Reference in new issue