mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
6 changed files with 135 additions and 1 deletions
@ -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>(MapsController); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(controller).toBeDefined(); |
||||
}); |
||||
}); |
@ -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, |
||||
}); |
||||
} |
||||
} |
@ -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 {} |
@ -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>(MapsService); |
||||
}); |
||||
|
||||
it('should be defined', () => { |
||||
expect(service).toBeDefined(); |
||||
}); |
||||
}); |
@ -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); |
||||
} |
||||
} |
Loading…
Reference in new issue