From 772d5deb1fa15520c725255867aaa575f230f7c9 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sun, 9 Apr 2023 10:49:11 +0530 Subject: [PATCH] feat: map view apis Signed-off-by: Pranav C --- packages/nocodb-nest/src/app.module.ts | 3 +- .../src/modules/maps/maps.controller.spec.ts | 20 +++++++ .../src/modules/maps/maps.controller.ts | 53 +++++++++++++++++++ .../src/modules/maps/maps.module.ts | 9 ++++ .../src/modules/maps/maps.service.spec.ts | 18 +++++++ .../src/modules/maps/maps.service.ts | 33 ++++++++++++ 6 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 packages/nocodb-nest/src/modules/maps/maps.controller.spec.ts create mode 100644 packages/nocodb-nest/src/modules/maps/maps.controller.ts create mode 100644 packages/nocodb-nest/src/modules/maps/maps.module.ts create mode 100644 packages/nocodb-nest/src/modules/maps/maps.service.spec.ts create mode 100644 packages/nocodb-nest/src/modules/maps/maps.service.ts diff --git a/packages/nocodb-nest/src/app.module.ts b/packages/nocodb-nest/src/app.module.ts index fea279457c..c17b143549 100644 --- a/packages/nocodb-nest/src/app.module.ts +++ b/packages/nocodb-nest/src/app.module.ts @@ -24,9 +24,10 @@ import { KanbansModule } from './modules/kanbans/kanbans.module'; import { GalleriesModule } from './modules/galleries/galleries.module'; import { FormColumnsModule } from './modules/form-columns/form-columns.module'; import { GridColumnsModule } from './modules/grid-columns/grid-columns.module'; +import { MapsModule } from './modules/maps/maps.module'; @Module({ - imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule, FormsModule, GridsModule, KanbansModule, GalleriesModule, FormColumnsModule, GridColumnsModule], + imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule, FormsModule, GridsModule, KanbansModule, GalleriesModule, FormColumnsModule, GridColumnsModule, MapsModule], controllers: [], providers: [Connection, MetaService, JwtStrategy, ExtractProjectIdMiddleware], exports: [Connection, MetaService], diff --git a/packages/nocodb-nest/src/modules/maps/maps.controller.spec.ts b/packages/nocodb-nest/src/modules/maps/maps.controller.spec.ts new file mode 100644 index 0000000000..4f262bb9a5 --- /dev/null +++ b/packages/nocodb-nest/src/modules/maps/maps.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { MapsController } from './maps.controller'; +import { MapsService } from './maps.service'; + +describe('MapsController', () => { + let controller: MapsController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [MapsController], + providers: [MapsService], + }).compile(); + + controller = module.get(MapsController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/maps/maps.controller.ts b/packages/nocodb-nest/src/modules/maps/maps.controller.ts new file mode 100644 index 0000000000..b7f57c3caf --- /dev/null +++ b/packages/nocodb-nest/src/modules/maps/maps.controller.ts @@ -0,0 +1,53 @@ +import { + Body, + Controller, + Get, + Param, + Patch, + Post, + UseGuards, +} from '@nestjs/common'; +import { MapUpdateReqType, ViewCreateReqType } from 'nocodb-sdk'; +import { + Acl, + ExtractProjectIdMiddleware, +} from '../../middlewares/extract-project-id/extract-project-id.middleware'; +import { MapsService } from './maps.service'; +import { AuthGuard } from '@nestjs/passport'; + +@Controller('maps') +@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) +export class MapsController { + constructor(private readonly mapsService: MapsService) {} + + @Get('/api/v1/db/meta/maps/:mapViewId') + @Acl('mapViewGet') + async mapViewGet(@Param('mapViewId') mapViewId: string) { + return await this.mapsService.mapViewGet({ mapViewId }); + } + + @Post('/api/v1/db/meta/tables/:tableId/maps') + @Acl('mapViewCreate') + async mapViewCreate( + @Param('tableId') tableId: string, + @Body() body: ViewCreateReqType, + ) { + const view = await this.mapsService.mapViewCreate({ + tableId, + map: body, + }); + return view; + } + + @Patch('/api/v1/db/meta/maps/:mapViewId') + @Acl('mapViewUpdate') + async mapViewUpdate( + @Param('mapViewId') mapViewId: string, + @Body() body: MapUpdateReqType, + ) { + return await this.mapsService.mapViewUpdate({ + mapViewId: mapViewId, + map: body, + }); + } +} diff --git a/packages/nocodb-nest/src/modules/maps/maps.module.ts b/packages/nocodb-nest/src/modules/maps/maps.module.ts new file mode 100644 index 0000000000..4d7c7b87a3 --- /dev/null +++ b/packages/nocodb-nest/src/modules/maps/maps.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { MapsService } from './maps.service'; +import { MapsController } from './maps.controller'; + +@Module({ + controllers: [MapsController], + providers: [MapsService] +}) +export class MapsModule {} diff --git a/packages/nocodb-nest/src/modules/maps/maps.service.spec.ts b/packages/nocodb-nest/src/modules/maps/maps.service.spec.ts new file mode 100644 index 0000000000..0974a652c7 --- /dev/null +++ b/packages/nocodb-nest/src/modules/maps/maps.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { MapsService } from './maps.service'; + +describe('MapsService', () => { + let service: MapsService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [MapsService], + }).compile(); + + service = module.get(MapsService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/maps/maps.service.ts b/packages/nocodb-nest/src/modules/maps/maps.service.ts new file mode 100644 index 0000000000..a7e1f07138 --- /dev/null +++ b/packages/nocodb-nest/src/modules/maps/maps.service.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@nestjs/common'; +import { T } from 'nc-help'; +import { MapUpdateReqType, ViewCreateReqType, ViewTypes } from 'nocodb-sdk'; +import { validatePayload } from '../../helpers'; +import { MapView, View } from '../../models'; + +@Injectable() +export class MapsService { + async mapViewGet(param: { mapViewId: string }) { + return await MapView.get(param.mapViewId); + } + + async mapViewCreate(param: { tableId: string; map: ViewCreateReqType }) { + validatePayload( + 'swagger.json#/components/schemas/ViewCreateReq', + param.map, + ); + const view = await View.insert({ + ...param.map, + // todo: sanitize + fk_model_id: param.tableId, + type: ViewTypes.MAP, + }); + T.emit('evt', { evt_type: 'vtable:created', show_as: 'map' }); + return view; + } + + async mapViewUpdate(param: { mapViewId: string; map: MapUpdateReqType }) { + validatePayload('swagger.json#/components/schemas/MapUpdateReq', param.map); + T.emit('evt', { evt_type: 'view:updated', type: 'map' }); + return await MapView.update(param.mapViewId, param.map); + } +}