common-cents-api/src/expenses/expense-data.service.ts
2026-02-09 13:53:42 -06:00

34 lines
1 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { Expense } from './entities/expense.entity';
import { UpdateExpenseDto } from './dto/update-expense.dto';
import { CreateExpenseDto } from './dto/create-expense.dto';
@Injectable()
export class ExpenseDataService {
private expenses: Repository<Expense>;
public constructor(private dataSource: DataSource) {
this.expenses = this.dataSource.getRepository(Expense);
}
public async getAll(): Promise<Expense[]> {
return await this.expenses.find();
}
public async getById(id: string): Promise<Expense | null> {
return await this.expenses.findOneBy({ id });
}
public async create(expense: CreateExpenseDto): Promise<Expense> {
return await this.expenses.save(expense);
}
public async update(expense: UpdateExpenseDto): Promise<Expense> {
return await this.expenses.save(expense);
}
public async delete(id: string): Promise<void> {
await this.expenses.delete({ id });
}
}