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 { return await this.expensesService.findAll(); } @Get(':id') @HttpCode(HttpStatus.OK) public async findOne(@Param('id') id: string): Promise { 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 { 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 { 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 { return await this.expensesService.remove(id); } }