From f9dec8c74b95ac78de9cbb90769ce6eceb0ad9eb Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sun, 9 Apr 2023 10:30:17 +0530 Subject: [PATCH] feat: gallery & grid view apis Signed-off-by: Pranav C --- .../galleries/galleries.controller.spec.ts | 20 +++++++ .../modules/galleries/galleries.controller.ts | 55 +++++++++++++++++++ .../src/modules/galleries/galleries.module.ts | 9 +++ .../galleries/galleries.service.spec.ts | 18 ++++++ .../modules/galleries/galleries.service.ts | 44 +++++++++++++++ .../modules/grids/grids.controller.spec.ts | 20 +++++++ .../src/modules/grids/grids.controller.ts | 44 +++++++++++++++ .../src/modules/grids/grids.module.ts | 9 +++ .../src/modules/grids/grids.service.spec.ts | 18 ++++++ .../src/modules/grids/grids.service.ts | 35 ++++++++++++ 10 files changed, 272 insertions(+) create mode 100644 packages/nocodb-nest/src/modules/galleries/galleries.controller.spec.ts create mode 100644 packages/nocodb-nest/src/modules/galleries/galleries.controller.ts create mode 100644 packages/nocodb-nest/src/modules/galleries/galleries.module.ts create mode 100644 packages/nocodb-nest/src/modules/galleries/galleries.service.spec.ts create mode 100644 packages/nocodb-nest/src/modules/galleries/galleries.service.ts create mode 100644 packages/nocodb-nest/src/modules/grids/grids.controller.spec.ts create mode 100644 packages/nocodb-nest/src/modules/grids/grids.controller.ts create mode 100644 packages/nocodb-nest/src/modules/grids/grids.module.ts create mode 100644 packages/nocodb-nest/src/modules/grids/grids.service.spec.ts create mode 100644 packages/nocodb-nest/src/modules/grids/grids.service.ts diff --git a/packages/nocodb-nest/src/modules/galleries/galleries.controller.spec.ts b/packages/nocodb-nest/src/modules/galleries/galleries.controller.spec.ts new file mode 100644 index 0000000000..fe45be2aa0 --- /dev/null +++ b/packages/nocodb-nest/src/modules/galleries/galleries.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { GalleriesController } from './galleries.controller'; +import { GalleriesService } from './galleries.service'; + +describe('GalleriesController', () => { + let controller: GalleriesController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [GalleriesController], + providers: [GalleriesService], + }).compile(); + + controller = module.get(GalleriesController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/galleries/galleries.controller.ts b/packages/nocodb-nest/src/modules/galleries/galleries.controller.ts new file mode 100644 index 0000000000..d73dbfbc42 --- /dev/null +++ b/packages/nocodb-nest/src/modules/galleries/galleries.controller.ts @@ -0,0 +1,55 @@ +import { + Body, + Controller, + Get, + Param, + Patch, + Post, + UseGuards, +} from '@nestjs/common'; +import { GalleryUpdateReqType, ViewCreateReqType } from 'nocodb-sdk'; +import { + Acl, + ExtractProjectIdMiddleware, +} from '../../middlewares/extract-project-id/extract-project-id.middleware'; +import { GalleriesService } from './galleries.service'; +import { AuthGuard } from '@nestjs/passport'; + +@Controller('galleries') +@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) +export class GalleriesController { + constructor(private readonly galleriesService: GalleriesService) {} + + @Get('/api/v1/db/meta/galleries/:galleryViewId') + @Acl('galleryViewGet') + async galleryViewGet(@Param('galleryViewId') galleryViewId: string) { + return await this.galleriesService.galleryViewGet({ + galleryViewId, + }); + } + + @Post('/api/v1/db/meta/tables/:tableId/galleries') + @Acl('galleryViewCreate') + async galleryViewCreate( + @Param('tableId') tableId: string, + @Body() body: ViewCreateReqType, + ) { + return await this.galleriesService.galleryViewCreate({ + gallery: body, + // todo: sanitize + tableId, + }); + } + + @Patch('/api/v1/db/meta/galleries/:galleryViewId') + @Acl('galleryViewUpdate') + async galleryViewUpdate( + @Param('galleryViewId') galleryViewId: string, + @Body() body: GalleryUpdateReqType, + ) { + return await this.galleriesService.galleryViewUpdate({ + galleryViewId, + gallery: body, + }); + } +} diff --git a/packages/nocodb-nest/src/modules/galleries/galleries.module.ts b/packages/nocodb-nest/src/modules/galleries/galleries.module.ts new file mode 100644 index 0000000000..c5fcbcb684 --- /dev/null +++ b/packages/nocodb-nest/src/modules/galleries/galleries.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { GalleriesService } from './galleries.service'; +import { GalleriesController } from './galleries.controller'; + +@Module({ + controllers: [GalleriesController], + providers: [GalleriesService] +}) +export class GalleriesModule {} diff --git a/packages/nocodb-nest/src/modules/galleries/galleries.service.spec.ts b/packages/nocodb-nest/src/modules/galleries/galleries.service.spec.ts new file mode 100644 index 0000000000..4294d0cf21 --- /dev/null +++ b/packages/nocodb-nest/src/modules/galleries/galleries.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { GalleriesService } from './galleries.service'; + +describe('GalleriesService', () => { + let service: GalleriesService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [GalleriesService], + }).compile(); + + service = module.get(GalleriesService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/galleries/galleries.service.ts b/packages/nocodb-nest/src/modules/galleries/galleries.service.ts new file mode 100644 index 0000000000..d7f648c216 --- /dev/null +++ b/packages/nocodb-nest/src/modules/galleries/galleries.service.ts @@ -0,0 +1,44 @@ +import { Injectable } from '@nestjs/common'; +import { GalleryUpdateReqType, ViewCreateReqType, ViewTypes } from 'nocodb-sdk'; +import { validatePayload } from '../../helpers'; +import { GalleryView, View } from '../../models'; +import { T } from 'nc-help'; + +@Injectable() +export class GalleriesService { + async galleryViewGet(param: { galleryViewId: string }) { + return await GalleryView.get(param.galleryViewId); + } + + async galleryViewCreate(param: { + tableId: string; + gallery: ViewCreateReqType; + }) { + validatePayload( + 'swagger.json#/components/schemas/ViewCreateReq', + param.gallery, + ); + + T.emit('evt', { evt_type: 'vtable:created', show_as: 'gallery' }); + const view = await View.insert({ + ...param.gallery, + // todo: sanitize + fk_model_id: param.tableId, + type: ViewTypes.GALLERY, + }); + return view; + } + + async galleryViewUpdate(param: { + galleryViewId: string; + gallery: GalleryUpdateReqType; + }) { + validatePayload( + 'swagger.json#/components/schemas/GalleryUpdateReq', + param.gallery, + ); + + T.emit('evt', { evt_type: 'view:updated', type: 'gallery' }); + return await GalleryView.update(param.galleryViewId, param.gallery); + } +} diff --git a/packages/nocodb-nest/src/modules/grids/grids.controller.spec.ts b/packages/nocodb-nest/src/modules/grids/grids.controller.spec.ts new file mode 100644 index 0000000000..e3e35d21e3 --- /dev/null +++ b/packages/nocodb-nest/src/modules/grids/grids.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { GridsController } from './grids.controller'; +import { GridsService } from './grids.service'; + +describe('GridsController', () => { + let controller: GridsController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [GridsController], + providers: [GridsService], + }).compile(); + + controller = module.get(GridsController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/grids/grids.controller.ts b/packages/nocodb-nest/src/modules/grids/grids.controller.ts new file mode 100644 index 0000000000..f487805368 --- /dev/null +++ b/packages/nocodb-nest/src/modules/grids/grids.controller.ts @@ -0,0 +1,44 @@ +import { + Body, + Controller, + 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 { GridsService } from './grids.service'; +import { AuthGuard } from '@nestjs/passport'; + +@Controller('grids') +@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) +export class GridsController { + get '/api/v1/db/meta/tables/:tableId/grids/'() { + return this['_/api/v1/db/meta/tables/:tableId/grids/']; + } + constructor(private readonly gridsService: GridsService) {} + + @Post('/api/v1/db/meta/tables/:tableId/grids/') + @Acl('gridViewCreate') + async gridViewCreate( + @Param('tableId') tableId: string, + @Body() body: ViewCreateReqType, + ) { + const view = await this.gridsService.gridViewCreate({ + grid: body, + tableId, + }); + return view; + } + @Patch('/api/v1/db/meta/grids/:viewId') + async gridViewUpdate(req, res) { + return await this.gridsService.gridViewUpdate({ + viewId: req.params.viewId, + grid: req.body, + }); + } +} diff --git a/packages/nocodb-nest/src/modules/grids/grids.module.ts b/packages/nocodb-nest/src/modules/grids/grids.module.ts new file mode 100644 index 0000000000..d8f0e12019 --- /dev/null +++ b/packages/nocodb-nest/src/modules/grids/grids.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { GridsService } from './grids.service'; +import { GridsController } from './grids.controller'; + +@Module({ + controllers: [GridsController], + providers: [GridsService] +}) +export class GridsModule {} diff --git a/packages/nocodb-nest/src/modules/grids/grids.service.spec.ts b/packages/nocodb-nest/src/modules/grids/grids.service.spec.ts new file mode 100644 index 0000000000..dbf57dec20 --- /dev/null +++ b/packages/nocodb-nest/src/modules/grids/grids.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { GridsService } from './grids.service'; + +describe('GridsService', () => { + let service: GridsService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [GridsService], + }).compile(); + + service = module.get(GridsService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/packages/nocodb-nest/src/modules/grids/grids.service.ts b/packages/nocodb-nest/src/modules/grids/grids.service.ts new file mode 100644 index 0000000000..373a812076 --- /dev/null +++ b/packages/nocodb-nest/src/modules/grids/grids.service.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@nestjs/common'; +import { GridUpdateReqType, ViewCreateReqType, ViewTypes } from 'nocodb-sdk'; +import { validatePayload } from '../../helpers'; +import { GridView, View } from '../../models'; +import { T } from 'nc-help'; + +@Injectable() +export class GridsService { + async gridViewCreate(param: { tableId: string; grid: ViewCreateReqType }) { + validatePayload( + 'swagger.json#/components/schemas/ViewCreateReq', + param.grid, + ); + + const view = await View.insert({ + ...param.grid, + // todo: sanitize + fk_model_id: param.tableId, + type: ViewTypes.GRID, + }); + + T.emit('evt', { evt_type: 'vtable:created', show_as: 'grid' }); + + return view; + } + + async gridViewUpdate(param: { viewId: string; grid: GridUpdateReqType }) { + validatePayload( + 'swagger.json#/components/schemas/GridUpdateReq', + param.grid, + ); + T.emit('evt', { evt_type: 'view:updated', type: 'grid' }); + return await GridView.update(param.viewId, param.grid); + } +}