Rework Expenses resource (#3)
Co-authored-by: Joe Arndt <jmarndt@users.noreply.github.com> Reviewed-on: #3
This commit is contained in:
parent
c6434de89d
commit
6600745072
28 changed files with 317 additions and 89 deletions
75
src/expenses/expenses.controller.ts
Normal file
75
src/expenses/expenses.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 { ExpensesService } from './expenses.service';
|
||||
import { CreateExpenseDto } from './dto/create-expense.dto';
|
||||
import { UpdateExpenseDto } from './dto/update-expense.dto';
|
||||
import { Expense } from './entities/expense.entity';
|
||||
|
||||
@Controller('expenses')
|
||||
export class ExpensesController {
|
||||
constructor(private readonly expensesService: ExpensesService) { }
|
||||
|
||||
@Get()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findAll(): Promise<Expense[]> {
|
||||
return await this.expensesService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findOne(@Param('id') id: string): Promise<Expense> {
|
||||
if (!id) {
|
||||
throw new BadRequestException('No ID provided.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.expensesService.findById(id);
|
||||
}
|
||||
catch (error) {
|
||||
throw new NotFoundException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
public async create(@Body() expense: CreateExpenseDto): Promise<Expense> {
|
||||
if (!expense) {
|
||||
throw new BadRequestException('Expense name cannot be empty.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.expensesService.create(expense);
|
||||
}
|
||||
catch (error) {
|
||||
throw new InternalServerErrorException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Put()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async update(@Body() expense: UpdateExpenseDto): Promise<Expense> {
|
||||
if (!expense.id) {
|
||||
throw new BadRequestException('Expense ID cannot be empty.');
|
||||
}
|
||||
|
||||
return await this.expensesService.update(expense);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
public async remove(@Param('id') id: string): Promise<void> {
|
||||
return await this.expensesService.remove(id);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue