File

src/models/category/category.controller.ts

Prefix

category

Description

Exposes category CRUD endpoints

Index

Methods

Methods

Async create
create(createCategoryDto: CreateCategoryDto)
Decorators :
@ApiOperation({summary: 'Admin creates a new category'})
@IsAdmin()
@Post()

Creates a new category, only for admins

Parameters :
Name Type Optional
createCategoryDto CreateCategoryDto No
Returns : Promise<Category>
Async findAll
findAll(findCategoriesDto: FindCategoriesDto)
Decorators :
@ApiOperation({summary: 'Returns all categories'})
@Public()
@Get()

Returns all categories with pagination

Default is starting on page 1 showing 10 results per page, searching and ordering by name

Parameters :
Name Type Optional
findCategoriesDto FindCategoriesDto No
Async findOneById
findOneById(id: string, findProductsDto: FindProductsDto)
Decorators :
@ApiOperation({summary: 'Admin gets category by ID and its products'})
@IsAdmin()
@Get('/id/:id')

Find category by ID, only for admins

Parameters :
Name Type Optional
id string No
findProductsDto FindProductsDto No
Returns : Promise<Category>
Async findOneByName
findOneByName(name: string, findProductsDto: FindProductsDto)
Decorators :
@ApiOperation({summary: 'Returns category by name and its products'})
@Public()
@Get(':name')

Find category by name

Parameters :
Name Type Optional
name string No
findProductsDto FindProductsDto No
Returns : Promise<Category>
Async remove
remove(id: string)
Decorators :
@ApiOperation({summary: 'Admin deletes category'})
@IsAdmin()
@Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT)

Deletes category from database, only for admins

Parameters :
Name Type Optional
id string No
Returns : Promise<void>
Async update
update(id: string, updateCategoryDto: UpdateCategoryDto)
Decorators :
@ApiOperation({summary: 'Admin updates category'})
@IsAdmin()
@Patch(':id')

Updates category information, only for admins

Parameters :
Name Type Optional
id string No
updateCategoryDto UpdateCategoryDto No
Returns : Promise<Category>
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { Public } from 'src/auth/public.decorator';
import { IsAdmin } from 'src/common/decorators/is-admin.decorator';
import { FindProductsDto } from '../product/dto/find-products.dto';
import { CategoryService } from './category.service';
import { CreateCategoryDto } from './dto/create-category.dto';
import { FindCategoriesDto } from './dto/find-categories.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';
import { Category } from './entities/category.entity';

/** Exposes category CRUD endpoints */
@ApiTags('category')
@Controller('category')
export class CategoryController {
  /** Exposes category CRUD endpoints
   *
   * Instantiate class and CategoryService dependency
   */
  constructor(private readonly categoryService: CategoryService) {}

  /** Creates a new category, only for admins */
  @ApiOperation({ summary: 'Admin creates a new category' })
  @IsAdmin()
  @Post()
  async create(
    @Body() createCategoryDto: CreateCategoryDto,
  ): Promise<Category> {
    return this.categoryService.create(createCategoryDto);
  }

  /** Returns all categories with pagination
   *
   * Default is starting on page 1 showing 10 results per page,
   * searching and ordering by name
   */
  @ApiOperation({ summary: 'Returns all categories' })
  @Public()
  @Get()
  async findAll(
    @Query() findCategoriesDto: FindCategoriesDto,
  ): Promise<Category[]> {
    return this.categoryService.findAll(findCategoriesDto);
  }

  /** Find category by ID, only for admins */
  @ApiOperation({ summary: 'Admin gets category by ID and its products' })
  @IsAdmin()
  @Get('/id/:id')
  async findOneById(
    @Param('id') id: string,
    @Query() findProductsDto: FindProductsDto,
  ): Promise<Category> {
    return this.categoryService.findOneById(id, findProductsDto);
  }

  /** Find category by name */
  @ApiOperation({ summary: 'Returns category by name and its products' })
  @Public()
  @Get(':name')
  async findOneByName(
    @Param('name') name: string,
    @Query() findProductsDto: FindProductsDto,
  ): Promise<Category> {
    return this.categoryService.findOneByName(name, findProductsDto);
  }

  /** Updates category information, only for admins */
  @ApiOperation({ summary: 'Admin updates category' })
  @IsAdmin()
  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Body() updateCategoryDto: UpdateCategoryDto,
  ): Promise<Category> {
    return this.categoryService.update(id, updateCategoryDto);
  }

  /** Deletes category from database, only for admins */
  @ApiOperation({ summary: 'Admin deletes category' })
  @IsAdmin()
  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  async remove(@Param('id') id: string): Promise<void> {
    return this.categoryService.remove(id);
  }
}

results matching ""

    No results matching ""