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
|
|
@ -9,12 +9,16 @@ import { CategoriesModule } from './categories/categories.module';
|
|||
import { Category } from './categories/entities/category.entity';
|
||||
import { SubCategoriesModule } from './sub-categories/sub-categories.module';
|
||||
import { SubCategory } from './sub-categories/entities/sub-category.entity';
|
||||
import { ExpensesModule } from './expenses/expenses.module';
|
||||
import { Expense } from './expenses/entities/expense.entity';
|
||||
|
||||
const entities = [Merchant, Tag, Category, SubCategory, Expense];
|
||||
|
||||
const sqliteConfig: TypeOrmModuleOptions = {
|
||||
synchronize: true, // typeorm -h (schema:sync)
|
||||
synchronize: true,
|
||||
type: 'sqlite',
|
||||
database: 'common-cents.db',
|
||||
entities: [Merchant, Tag, Category, SubCategory]
|
||||
entities
|
||||
}
|
||||
|
||||
@Module({
|
||||
|
|
@ -23,9 +27,9 @@ const sqliteConfig: TypeOrmModuleOptions = {
|
|||
MerchantsModule,
|
||||
TagsModule,
|
||||
CategoriesModule,
|
||||
SubCategoriesModule
|
||||
SubCategoriesModule,
|
||||
ExpensesModule
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: []
|
||||
controllers: [AppController]
|
||||
})
|
||||
export class AppModule { }
|
||||
|
|
|
|||
|
|
@ -5,6 +5,6 @@ export class Category {
|
|||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
}
|
||||
|
|
|
|||
10
src/expenses/dto/create-expense.dto.ts
Normal file
10
src/expenses/dto/create-expense.dto.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Category } from '../../categories/entities/category.entity';
|
||||
|
||||
export class CreateExpenseDto {
|
||||
year: string;
|
||||
month: string;
|
||||
day: string;
|
||||
cents: number;
|
||||
description: string;
|
||||
category: Category
|
||||
}
|
||||
6
src/expenses/dto/update-expense.dto.ts
Normal file
6
src/expenses/dto/update-expense.dto.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateExpenseDto } from './create-expense.dto';
|
||||
|
||||
export class UpdateExpenseDto extends PartialType(CreateExpenseDto) {
|
||||
id: string;
|
||||
}
|
||||
39
src/expenses/entities/expense.entity.ts
Normal file
39
src/expenses/entities/expense.entity.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { Column, Entity, JoinTable, ManyToMany, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import { Tag } from '../../tags/entities/tag.entity';
|
||||
import { SubCategory } from '../../sub-categories/entities/sub-category.entity';
|
||||
import { Category } from '../../categories/entities/category.entity';
|
||||
import { Merchant } from '../../merchants/entities/merchant.entity';
|
||||
|
||||
@Entity()
|
||||
export class Expense {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
year: string;
|
||||
|
||||
@Column()
|
||||
month: string;
|
||||
|
||||
@Column()
|
||||
day: string;
|
||||
|
||||
@Column()
|
||||
cents: number;
|
||||
|
||||
@Column({ nullable: true })
|
||||
description: string;
|
||||
|
||||
@ManyToOne(() => Category, { eager: true })
|
||||
category: Category;
|
||||
|
||||
@ManyToOne(() => SubCategory, { nullable: true, eager: true })
|
||||
subCategory: SubCategory;
|
||||
|
||||
@ManyToOne(() => Merchant, { nullable: true, eager: true })
|
||||
merchant: Merchant;
|
||||
|
||||
@ManyToMany(() => Tag, { nullable: true, eager: true })
|
||||
@JoinTable()
|
||||
tags: Tag[];
|
||||
}
|
||||
34
src/expenses/expense-data.service.ts
Normal file
34
src/expenses/expense-data.service.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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 });
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
10
src/expenses/expenses.module.ts
Normal file
10
src/expenses/expenses.module.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { ExpensesService } from './expenses.service';
|
||||
import { ExpensesController } from './expenses.controller';
|
||||
import { ExpenseDataService } from './expense-data.service';
|
||||
|
||||
@Module({
|
||||
controllers: [ExpensesController],
|
||||
providers: [ExpensesService, ExpenseDataService],
|
||||
})
|
||||
export class ExpensesModule { }
|
||||
35
src/expenses/expenses.service.ts
Normal file
35
src/expenses/expenses.service.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateExpenseDto } from './dto/create-expense.dto';
|
||||
import { UpdateExpenseDto } from './dto/update-expense.dto';
|
||||
import { ExpenseDataService } from './expense-data.service';
|
||||
import { Expense } from './entities/expense.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ExpensesService {
|
||||
public constructor(private expenseDataService: ExpenseDataService) { }
|
||||
|
||||
public async findAll(): Promise<Expense[]> {
|
||||
return await this.expenseDataService.getAll();
|
||||
}
|
||||
|
||||
public async findById(id: string): Promise<Expense> {
|
||||
const expense = await this.expenseDataService.getById(id);
|
||||
if (!expense) {
|
||||
throw new Error('No expense found');
|
||||
}
|
||||
|
||||
return expense;
|
||||
}
|
||||
|
||||
public async create(expense: CreateExpenseDto): Promise<Expense> {
|
||||
return await this.expenseDataService.create(expense);
|
||||
}
|
||||
|
||||
public async update(expense: UpdateExpenseDto): Promise<Expense> {
|
||||
return await this.expenseDataService.update(expense);
|
||||
}
|
||||
|
||||
public async remove(id: string): Promise<void> {
|
||||
await this.expenseDataService.delete(id);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,6 @@ export class Merchant {
|
|||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,6 @@ export class SubCategory {
|
|||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,6 @@ export class Tag {
|
|||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue