mashafrancis/butternut-user

View on GitHub
src/base/rest.controller.ts

Summary

Maintainability
A
2 hrs
Test Coverage
import {
    Body,
    Delete,
    Get,
    Param,
    Patch,
    Post,
    Put,
    Req,
} from '@nestjs/common';
import { DeepPartial } from 'typeorm';
import { ExtendedEntity } from '../app/_helpers';
import { CrudService } from './crud.service';

export class RestController<T extends ExtendedEntity> {
    protected service: CrudService<T>;

    @Get('/')
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    public findAll(@Req() req): Promise<T[]> {
        return this.service.findAll();
    }

    @Get('/:id')
    public async findOne(@Param('id') id: string) {
        return this.service.findOneById(id);
    }

    @Post('/')
    public async create(@Body() data: DeepPartial<T>): Promise<T> {
        return this.service.create(data);
    }

    @Put('/:id')
    public async update(@Body() data: DeepPartial<T>): Promise<T> {
        return this.service.update(data);
    }

    @Patch('/:id')
    public async patch(
        @Param('id') id: string,
        @Body() data: DeepPartial<T>,
    ): Promise<T> {
        return this.service.patch(id, data);
    }

    @Delete('/:id')
    public async delete(@Param('id') id: string): Promise<T> {
        return this.service.delete(id);
    }
}