/* * @author gs * @date 2020/07/29 10:22 */ import { Body, Delete, Get, Param, Post, Put, Query } from '@nestjs/common'; import { CreateQuery } from 'mongoose'; import { BaseService } from './base.service'; import { BaseModel } from './base.model'; export abstract class BaseController { protected readonly service: BaseService; protected constructor(service: BaseService) { this.service = service; } @Post() public async create(@Body() doc: Partial): Promise { return this.service.create(doc as CreateQuery); } @Delete(':id') public async deleteById(@Param('id') id: string): Promise { return this.service.deleteById(id); } @Get(':id') public async findById(@Param('id') id: string): Promise { return this.service.findById(id); } @Get() public async find(@Query('filter') filter: string): Promise { const findFilter = filter ? JSON.parse(filter) : {}; return this.service.find(findFilter); } @Put(':id') public async update( @Param('id') id: string, @Body() doc: Partial ): Promise { const existed = await this.service.findById(id); const updatedDoc = { ...(existed as any), ...(doc as any) } as any; return this.service.update(id, updatedDoc); } }