common-cents-api/src/expenses/expenses.controller.ts
2026-02-25 14:50:30 -06:00

77 lines
1.9 KiB
TypeScript

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 { GetExpenseDto } from './dto/get-expense.dto';
@Controller('expenses')
export class ExpensesController {
constructor(private readonly expensesService: ExpensesService) { }
@Get()
@HttpCode(HttpStatus.OK)
public async findAll(): Promise<GetExpenseDto[]> {
return await this.expensesService.findAll();
}
@Get(':id')
@HttpCode(HttpStatus.OK)
public async findOne(@Param('id') id: string): Promise<GetExpenseDto> {
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<GetExpenseDto> {
if (!expense) {
throw new BadRequestException('Expense name cannot be empty.');
}
// TODO: Validate date/cents/category exist...
try {
return await this.expensesService.create(expense);
}
catch (error) {
throw new InternalServerErrorException(error);
}
}
@Put()
@HttpCode(HttpStatus.OK)
public async update(@Body() expense: UpdateExpenseDto): Promise<GetExpenseDto> {
if (!expense.id) {
throw new BadRequestException('Expense ID cannot be empty.');
}
// TODO: Validate date/cents/category exist...
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);
}
}