From cc0122d513dc790c6647b579a8a676f497467097 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sat, 8 Apr 2023 17:45:31 +0530 Subject: [PATCH] feat: view column apis - module Signed-off-by: Pranav C --- .../view-columns.controller.spec.ts | 20 +++++++ .../view-columns/view-columns.controller.ts | 58 +++++++++++++++++++ .../view-columns/view-columns.module.ts | 9 +++ .../view-columns/view-columns.service.spec.ts | 18 ++++++ .../view-columns/view-columns.service.ts | 50 ++++++++++++++++ 5 files changed, 155 insertions(+) create mode 100644 packages/nocodb-nest/src/modules/view-columns/view-columns.controller.spec.ts create mode 100644 packages/nocodb-nest/src/modules/view-columns/view-columns.controller.ts create mode 100644 packages/nocodb-nest/src/modules/view-columns/view-columns.module.ts create mode 100644 packages/nocodb-nest/src/modules/view-columns/view-columns.service.spec.ts create mode 100644 packages/nocodb-nest/src/modules/view-columns/view-columns.service.ts diff --git a/packages/nocodb-nest/src/modules/view-columns/view-columns.controller.spec.ts b/packages/nocodb-nest/src/modules/view-columns/view-columns.controller.spec.ts new file mode 100644 index 0000000000..71182e27f1 --- /dev/null +++ b/packages/nocodb-nest/src/modules/view-columns/view-columns.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { ViewColumnsController } from './view-columns.controller'; +import { ViewColumnsService } from './view-columns.service'; + +describe('ViewColumnsController', () => { + let controller: ViewColumnsController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [ViewColumnsController], + providers: [ViewColumnsService], + }).compile(); + + controller = module.get(ViewColumnsController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/view-columns/view-columns.controller.ts b/packages/nocodb-nest/src/modules/view-columns/view-columns.controller.ts new file mode 100644 index 0000000000..97d4bc4699 --- /dev/null +++ b/packages/nocodb-nest/src/modules/view-columns/view-columns.controller.ts @@ -0,0 +1,58 @@ +import { + Body, + Controller, + Get, + Param, + Patch, + Post, + UseGuards, +} from '@nestjs/common'; +import { ColumnReqType, ViewColumnReqType } from 'nocodb-sdk'; +import { PagedResponseImpl } from '../../helpers/PagedResponse'; +import { + Acl, + ExtractProjectIdMiddleware, +} from '../../middlewares/extract-project-id/extract-project-id.middleware'; +import { ViewColumnsService } from './view-columns.service'; +import { AuthGuard } from '@nestjs/passport'; + +@Controller('view-columns') +@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) +export class ViewColumnsController { + constructor(private readonly viewColumnsService: ViewColumnsService) {} + + @Get('/api/v1/db/meta/views/:viewId/columns/') + @Acl('columnList') + async columnList(@Param('viewId') viewId: string) { + return; + new PagedResponseImpl(await this.viewColumnsService.columnList({ viewId })); + } + + @Post('/api/v1/db/meta/views/:viewId/columns/') + @Acl('columnAdd') + async columnAdd( + @Param('viewId') viewId: string, + @Body() body: ViewColumnReqType, + ) { + const viewColumn = await this.viewColumnsService.columnAdd({ + viewId, + column: body, + }); + return viewColumn; + } + + @Patch('/api/v1/db/meta/views/:viewId/columns/:columnId') + @Acl('columnUpdate') + async columnUpdate( + @Param('viewId') viewId: string, + @Param('columnId') columnId: string, + @Body() body: ViewColumnReqType, + ) { + const result = await this.viewColumnsService.columnUpdate({ + viewId, + columnId, + column: body, + }); + return result; + } +} diff --git a/packages/nocodb-nest/src/modules/view-columns/view-columns.module.ts b/packages/nocodb-nest/src/modules/view-columns/view-columns.module.ts new file mode 100644 index 0000000000..7417a98442 --- /dev/null +++ b/packages/nocodb-nest/src/modules/view-columns/view-columns.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { ViewColumnsService } from './view-columns.service'; +import { ViewColumnsController } from './view-columns.controller'; + +@Module({ + controllers: [ViewColumnsController], + providers: [ViewColumnsService] +}) +export class ViewColumnsModule {} diff --git a/packages/nocodb-nest/src/modules/view-columns/view-columns.service.spec.ts b/packages/nocodb-nest/src/modules/view-columns/view-columns.service.spec.ts new file mode 100644 index 0000000000..7d7aaecebe --- /dev/null +++ b/packages/nocodb-nest/src/modules/view-columns/view-columns.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { ViewColumnsService } from './view-columns.service'; + +describe('ViewColumnsService', () => { + let service: ViewColumnsService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ViewColumnsService], + }).compile(); + + service = module.get(ViewColumnsService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/view-columns/view-columns.service.ts b/packages/nocodb-nest/src/modules/view-columns/view-columns.service.ts new file mode 100644 index 0000000000..50c245cdd9 --- /dev/null +++ b/packages/nocodb-nest/src/modules/view-columns/view-columns.service.ts @@ -0,0 +1,50 @@ +import { Injectable } from '@nestjs/common'; +import { ViewColumnReqType, ViewColumnUpdateReqType } from 'nocodb-sdk'; +import { validatePayload } from '../../helpers'; +import { View } from '../../models'; + +import { T } from 'nc-help'; + +@Injectable() +export class ViewColumnsService { + async columnList(param: { viewId: string }) { + return await View.getColumns(param.viewId); + } + async columnAdd(param: { viewId: string; column: ViewColumnReqType }) { + validatePayload( + 'swagger.json#/components/schemas/ViewColumnReq', + param.column, + ); + + const viewColumn = await View.insertOrUpdateColumn( + param.viewId, + param.column.fk_column_id, + { + order: param.column.order, + show: param.column.show, + }, + ); + T.emit('evt', { evt_type: 'viewColumn:inserted' }); + + return viewColumn; + } + + async columnUpdate(param: { + viewId: string; + columnId: string; + column: ViewColumnUpdateReqType; + }) { + validatePayload( + 'swagger.json#/components/schemas/ViewColumnUpdateReq', + param.column, + ); + + const result = await View.updateColumn( + param.viewId, + param.columnId, + param.column, + ); + T.emit('evt', { evt_type: 'viewColumn:updated' }); + return result; + } +}