Add TypeORM DB and Resources (#2)
Co-authored-by: Joe Arndt <jmarndt@users.noreply.github.com> Reviewed-on: #2
This commit is contained in:
parent
746adcd2fd
commit
c6434de89d
64 changed files with 2916 additions and 360 deletions
75
src/categories/categories.controller.ts
Normal file
75
src/categories/categories.controller.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Body,
|
||||
Param,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
BadRequestException,
|
||||
NotFoundException,
|
||||
InternalServerErrorException
|
||||
} from '@nestjs/common';
|
||||
import { CategoriesService } from './categories.service';
|
||||
import { CreateCategoryDto } from './dto/create-category.dto';
|
||||
import { UpdateCategoryDto } from './dto/update-category.dto';
|
||||
import { Category } from './entities/category.entity';
|
||||
|
||||
@Controller('categories')
|
||||
export class CategoriesController {
|
||||
constructor(private readonly categoriesService: CategoriesService) { }
|
||||
|
||||
@Get()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findAll(): Promise<Category[]> {
|
||||
return await this.categoriesService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findOne(@Param('id') id: string): Promise<Category> {
|
||||
if (!id) {
|
||||
throw new BadRequestException('No ID provided.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.categoriesService.findById(id);
|
||||
}
|
||||
catch (error) {
|
||||
throw new NotFoundException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
public async create(@Body() category: CreateCategoryDto): Promise<Category> {
|
||||
if (!category.name) {
|
||||
throw new BadRequestException('Category name cannot be empty.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.categoriesService.create(category);
|
||||
}
|
||||
catch (error) {
|
||||
throw new InternalServerErrorException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Put()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async update(@Body() category: UpdateCategoryDto): Promise<Category> {
|
||||
if (!category.id) {
|
||||
throw new BadRequestException('Category ID cannot be empty.');
|
||||
}
|
||||
|
||||
return await this.categoriesService.update(category);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
public async remove(@Param('id') id: string): Promise<void> {
|
||||
return await this.categoriesService.remove(id);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue