Add TypeORM DB and Resources (#2)
Co-authored-by: Joe Arndt <jmarndt@users.noreply.github.com> Reviewed-on: #2
This commit is contained in:
parent
746adcd2fd
commit
c6434de89d
64 changed files with 2916 additions and 360 deletions
|
|
@ -1,22 +0,0 @@
|
|||
// import { Test, TestingModule } from '@nestjs/testing';
|
||||
// import { AppController } from './app.controller';
|
||||
// import { AppService } from './app.service';
|
||||
//
|
||||
// describe('AppController', () => {
|
||||
// let appController: AppController;
|
||||
//
|
||||
// beforeEach(async () => {
|
||||
// const app: TestingModule = await Test.createTestingModule({
|
||||
// controllers: [AppController],
|
||||
// providers: [AppService],
|
||||
// }).compile();
|
||||
//
|
||||
// appController = app.get<AppController>(AppController);
|
||||
// });
|
||||
//
|
||||
// describe('root', () => {
|
||||
// it('should return "Hello World!"', () => {
|
||||
// expect(appController.getHello()).toBe('Hello World!');
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
|
@ -1,11 +1,31 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { ExpensesService } from './services/expenses.service';
|
||||
import { ExpensesController } from './controllers/expenses/expenses.controller';
|
||||
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
|
||||
import { MerchantsModule } from './merchants/merchants.module';
|
||||
import { Merchant } from './merchants/entities/merchant.entity';
|
||||
import { TagsModule } from './tags/tags.module';
|
||||
import { Tag } from './tags/entities/tag.entity';
|
||||
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';
|
||||
|
||||
const sqliteConfig: TypeOrmModuleOptions = {
|
||||
synchronize: true, // typeorm -h (schema:sync)
|
||||
type: 'sqlite',
|
||||
database: 'common-cents.db',
|
||||
entities: [Merchant, Tag, Category, SubCategory]
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [AppController, ExpensesController],
|
||||
providers: [ExpensesService]
|
||||
imports: [
|
||||
TypeOrmModule.forRoot(sqliteConfig),
|
||||
MerchantsModule,
|
||||
TagsModule,
|
||||
CategoriesModule,
|
||||
SubCategoriesModule
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: []
|
||||
})
|
||||
export class AppModule {}
|
||||
export class AppModule { }
|
||||
|
|
|
|||
75
src/categories/categories.controller.ts
Normal file
75
src/categories/categories.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 { CategoriesService } from './categories.service';
|
||||
import { CreateCategoryDto } from './dto/create-category.dto';
|
||||
import { UpdateCategoryDto } from './dto/update-category.dto';
|
||||
import { Category } from './entities/category.entity';
|
||||
|
||||
@Controller('categories')
|
||||
export class CategoriesController {
|
||||
constructor(private readonly categoriesService: CategoriesService) { }
|
||||
|
||||
@Get()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findAll(): Promise<Category[]> {
|
||||
return await this.categoriesService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findOne(@Param('id') id: string): Promise<Category> {
|
||||
if (!id) {
|
||||
throw new BadRequestException('No ID provided.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.categoriesService.findById(id);
|
||||
}
|
||||
catch (error) {
|
||||
throw new NotFoundException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
public async create(@Body() category: CreateCategoryDto): Promise<Category> {
|
||||
if (!category.name) {
|
||||
throw new BadRequestException('Category name cannot be empty.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.categoriesService.create(category);
|
||||
}
|
||||
catch (error) {
|
||||
throw new InternalServerErrorException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Put()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async update(@Body() category: UpdateCategoryDto): Promise<Category> {
|
||||
if (!category.id) {
|
||||
throw new BadRequestException('Category ID cannot be empty.');
|
||||
}
|
||||
|
||||
return await this.categoriesService.update(category);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
public async remove(@Param('id') id: string): Promise<void> {
|
||||
return await this.categoriesService.remove(id);
|
||||
}
|
||||
}
|
||||
10
src/categories/categories.module.ts
Normal file
10
src/categories/categories.module.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { CategoriesService } from './categories.service';
|
||||
import { CategoriesController } from './categories.controller';
|
||||
import { CategoryDataService } from './category-data.service';
|
||||
|
||||
@Module({
|
||||
controllers: [CategoriesController],
|
||||
providers: [CategoriesService, CategoryDataService]
|
||||
})
|
||||
export class CategoriesModule { }
|
||||
35
src/categories/categories.service.ts
Normal file
35
src/categories/categories.service.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateCategoryDto } from './dto/create-category.dto';
|
||||
import { UpdateCategoryDto } from './dto/update-category.dto';
|
||||
import { CategoryDataService } from './category-data.service';
|
||||
import { Category } from './entities/category.entity';
|
||||
|
||||
@Injectable()
|
||||
export class CategoriesService {
|
||||
public constructor(private categoryDataService: CategoryDataService) { }
|
||||
|
||||
public async findAll(): Promise<Category[]> {
|
||||
return await this.categoryDataService.getAll();
|
||||
}
|
||||
|
||||
public async findById(id: string): Promise<Category> {
|
||||
const category = await this.categoryDataService.getById(id);
|
||||
if (!category) {
|
||||
throw new Error('No category found');
|
||||
}
|
||||
|
||||
return category;
|
||||
}
|
||||
|
||||
public async create(category: CreateCategoryDto): Promise<Category> {
|
||||
return await this.categoryDataService.create(category.name);
|
||||
}
|
||||
|
||||
public async update(category: UpdateCategoryDto): Promise<Category> {
|
||||
return await this.categoryDataService.update(category);
|
||||
}
|
||||
|
||||
public async remove(id: string): Promise<void> {
|
||||
await this.categoryDataService.delete(id);
|
||||
}
|
||||
}
|
||||
33
src/categories/category-data.service.ts
Normal file
33
src/categories/category-data.service.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Category } from './entities/category.entity';
|
||||
import { UpdateCategoryDto } from './dto/update-category.dto';
|
||||
|
||||
@Injectable()
|
||||
export class CategoryDataService {
|
||||
private categories: Repository<Category>;
|
||||
|
||||
public constructor(private dataSource: DataSource) {
|
||||
this.categories = this.dataSource.getRepository(Category);
|
||||
}
|
||||
|
||||
public async getAll(): Promise<Category[]> {
|
||||
return await this.categories.find();
|
||||
}
|
||||
|
||||
public async getById(id: string): Promise<Category | null> {
|
||||
return await this.categories.findOneBy({ id });
|
||||
}
|
||||
|
||||
public async create(name: string): Promise<Category> {
|
||||
return await this.categories.save({ name });
|
||||
}
|
||||
|
||||
public async update(category: UpdateCategoryDto): Promise<Category> {
|
||||
return await this.categories.save(category);
|
||||
}
|
||||
|
||||
public async delete(id: string): Promise<void> {
|
||||
await this.categories.delete({ id });
|
||||
}
|
||||
}
|
||||
3
src/categories/dto/create-category.dto.ts
Normal file
3
src/categories/dto/create-category.dto.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export class CreateCategoryDto {
|
||||
name: string;
|
||||
}
|
||||
4
src/categories/dto/update-category.dto.ts
Normal file
4
src/categories/dto/update-category.dto.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export class UpdateCategoryDto {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
10
src/categories/entities/category.entity.ts
Normal file
10
src/categories/entities/category.entity.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Category {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
import { PartialType } from '@nestjs/mapped-types';
|
||||
|
||||
export class CreateExpenseDto {
|
||||
date: Date;
|
||||
cents: number;
|
||||
categoryId: string;
|
||||
merchantId?: string;
|
||||
subcategoryIds?: string[];
|
||||
tagIds?: string[];
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export class UpdateExpenseDto extends PartialType(CreateExpenseDto) {}
|
||||
|
||||
export class GetExpenseDto {
|
||||
id: string;
|
||||
date: Date;
|
||||
cents: number;
|
||||
category: string;
|
||||
merchant?: string;
|
||||
subcategories?: string[];
|
||||
tags?: string[];
|
||||
description?: string;
|
||||
}
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
export class Expense {
|
||||
id: string;
|
||||
date: Date;
|
||||
cents: number;
|
||||
categoryId: string;
|
||||
merchantId?: string;
|
||||
subcategoryIds: string[];
|
||||
tagIds: string[];
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export class Category {
|
||||
id: string;
|
||||
category: string;
|
||||
}
|
||||
|
||||
export class SubCategory {
|
||||
id: string;
|
||||
subcategory: string;
|
||||
}
|
||||
|
||||
export class Merchant {
|
||||
id: string;
|
||||
merchant: string;
|
||||
}
|
||||
|
||||
export class Tag {
|
||||
id: string;
|
||||
tag: string;
|
||||
}
|
||||
|
|
@ -1,44 +0,0 @@
|
|||
import { Controller, Get, Post, Body, Patch, Param, Delete, NotFoundException, HttpCode } from '@nestjs/common';
|
||||
import { ExpensesService } from '../../services/expenses.service';
|
||||
import { CreateExpenseDto, UpdateExpenseDto } from './expense.dto';
|
||||
|
||||
@Controller('expenses')
|
||||
export class ExpensesController {
|
||||
constructor(private readonly expensesService: ExpensesService) {}
|
||||
|
||||
@Post()
|
||||
create(@Body() createExpenseDto: CreateExpenseDto) {
|
||||
return this.expensesService.create(createExpenseDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.expensesService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
const expense = this.expensesService.findOne(id);
|
||||
if (!expense) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return expense;
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateExpenseDto: UpdateExpenseDto) {
|
||||
const expense = this.expensesService.update(id, updateExpenseDto);
|
||||
if (!expense) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return expense;
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(204)
|
||||
remove(@Param('id') id: string) {
|
||||
return this.expensesService.remove(id);
|
||||
}
|
||||
}
|
||||
3
src/merchants/dto/create-merchant.dto.ts
Normal file
3
src/merchants/dto/create-merchant.dto.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export class CreateMerchantDto {
|
||||
name: string;
|
||||
}
|
||||
4
src/merchants/dto/update-merchant.dto.ts
Normal file
4
src/merchants/dto/update-merchant.dto.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export class UpdateMerchantDto {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
10
src/merchants/entities/merchant.entity.ts
Normal file
10
src/merchants/entities/merchant.entity.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Merchant {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
}
|
||||
33
src/merchants/merchant-data.service.ts
Normal file
33
src/merchants/merchant-data.service.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Merchant } from './entities/merchant.entity';
|
||||
import { UpdateMerchantDto } from './dto/update-merchant.dto';
|
||||
|
||||
@Injectable()
|
||||
export class MerchantDataService {
|
||||
private merchants: Repository<Merchant>;
|
||||
|
||||
public constructor(private dataSource: DataSource) {
|
||||
this.merchants = this.dataSource.getRepository(Merchant);
|
||||
}
|
||||
|
||||
public async getAll(): Promise<Merchant[]> {
|
||||
return await this.merchants.find();
|
||||
}
|
||||
|
||||
public async getById(id: string): Promise<Merchant | null> {
|
||||
return await this.merchants.findOneBy({ id });
|
||||
}
|
||||
|
||||
public async create(name: string): Promise<Merchant> {
|
||||
return await this.merchants.save({ name });
|
||||
}
|
||||
|
||||
public async update(merchant: UpdateMerchantDto): Promise<Merchant> {
|
||||
return await this.merchants.save(merchant);
|
||||
}
|
||||
|
||||
public async delete(id: string): Promise<void> {
|
||||
await this.merchants.delete({ id });
|
||||
}
|
||||
}
|
||||
75
src/merchants/merchants.controller.ts
Normal file
75
src/merchants/merchants.controller.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Delete,
|
||||
Body,
|
||||
Param,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
NotFoundException,
|
||||
BadRequestException,
|
||||
InternalServerErrorException
|
||||
} from '@nestjs/common';
|
||||
import { MerchantsService } from './merchants.service';
|
||||
import { CreateMerchantDto } from './dto/create-merchant.dto';
|
||||
import { UpdateMerchantDto } from './dto/update-merchant.dto';
|
||||
import { Merchant } from './entities/merchant.entity';
|
||||
|
||||
@Controller('merchants')
|
||||
export class MerchantsController {
|
||||
public constructor(private readonly merchantsService: MerchantsService) { }
|
||||
|
||||
@Get()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findAll(): Promise<Merchant[]> {
|
||||
return await this.merchantsService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findOne(@Param('id') id: string): Promise<Merchant> {
|
||||
if (!id) {
|
||||
throw new BadRequestException('No ID provided.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.merchantsService.findById(id);
|
||||
}
|
||||
catch (error) {
|
||||
throw new NotFoundException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
public async create(@Body() merchant: CreateMerchantDto): Promise<Merchant> {
|
||||
if (!merchant.name) {
|
||||
throw new BadRequestException('Merchant name cannot be empty.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.merchantsService.create(merchant);
|
||||
}
|
||||
catch (error) {
|
||||
throw new InternalServerErrorException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Put()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async update(@Body() merchant: UpdateMerchantDto): Promise<Merchant> {
|
||||
if (!merchant.id) {
|
||||
throw new BadRequestException('Merchant ID cannot be empty.');
|
||||
}
|
||||
|
||||
return await this.merchantsService.update(merchant);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
public async remove(@Param('id') id: string): Promise<void> {
|
||||
return await this.merchantsService.remove(id);
|
||||
}
|
||||
}
|
||||
10
src/merchants/merchants.module.ts
Normal file
10
src/merchants/merchants.module.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { MerchantsService } from './merchants.service';
|
||||
import { MerchantsController } from './merchants.controller';
|
||||
import { MerchantDataService } from './merchant-data.service';
|
||||
|
||||
@Module({
|
||||
controllers: [MerchantsController],
|
||||
providers: [MerchantsService, MerchantDataService]
|
||||
})
|
||||
export class MerchantsModule { }
|
||||
35
src/merchants/merchants.service.ts
Normal file
35
src/merchants/merchants.service.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateMerchantDto } from './dto/create-merchant.dto';
|
||||
import { UpdateMerchantDto } from './dto/update-merchant.dto';
|
||||
import { MerchantDataService } from './merchant-data.service';
|
||||
import { Merchant } from './entities/merchant.entity';
|
||||
|
||||
@Injectable()
|
||||
export class MerchantsService {
|
||||
public constructor(private merchantDataService: MerchantDataService) { }
|
||||
|
||||
public async findAll(): Promise<Merchant[]> {
|
||||
return await this.merchantDataService.getAll();
|
||||
}
|
||||
|
||||
public async findById(id: string): Promise<Merchant> {
|
||||
const merchant = await this.merchantDataService.getById(id);
|
||||
if (!merchant) {
|
||||
throw new Error('Merchant not found.');
|
||||
}
|
||||
|
||||
return merchant;
|
||||
}
|
||||
|
||||
public async create(merchant: CreateMerchantDto): Promise<Merchant> {
|
||||
return await this.merchantDataService.create(merchant.name);
|
||||
}
|
||||
|
||||
public async update(merchant: UpdateMerchantDto): Promise<Merchant> {
|
||||
return await this.merchantDataService.update(merchant);
|
||||
}
|
||||
|
||||
public async remove(id: string): Promise<void> {
|
||||
await this.merchantDataService.delete(id);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,141 +0,0 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { Category, Expense, Merchant, SubCategory, Tag } from '../controllers/expenses/expense.entities';
|
||||
import { CreateExpenseDto, GetExpenseDto, UpdateExpenseDto } from '../controllers/expenses/expense.dto';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
@Injectable()
|
||||
export class ExpensesService {
|
||||
private categories: Category[] = [
|
||||
{
|
||||
id: '1',
|
||||
category: 'Category 1'
|
||||
}
|
||||
];
|
||||
private merchants: Merchant[] = [
|
||||
{
|
||||
id: '1',
|
||||
merchant: 'Merchant 1'
|
||||
}
|
||||
];
|
||||
private subcategories: SubCategory[] = [
|
||||
{
|
||||
id: '1',
|
||||
subcategory: 'Subcategory 1'
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
subcategory: 'Subcategory 2'
|
||||
}
|
||||
];
|
||||
private tags: Tag[] = [
|
||||
{
|
||||
id: '1',
|
||||
tag: 'Tag 1'
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
tag: 'Tag 2'
|
||||
}
|
||||
];
|
||||
private expenses: Expense[] = [
|
||||
{
|
||||
id: '2aa94170-2c57-4c4f-a1e7-13544ba72917',
|
||||
date: new Date('2025-12-01'),
|
||||
cents: 15443,
|
||||
categoryId: '1',
|
||||
merchantId: '1',
|
||||
subcategoryIds: ['1', '2'],
|
||||
tagIds: ['1', '2'],
|
||||
description: 'Full existing expense'
|
||||
},
|
||||
{
|
||||
id: '0708e7f7-3a2a-4b93-81da-38954925ca78',
|
||||
date: new Date('2025-12-02'),
|
||||
cents: 8723,
|
||||
categoryId: '1',
|
||||
subcategoryIds: [],
|
||||
tagIds: [],
|
||||
description: 'Partial existing expense'
|
||||
}
|
||||
];
|
||||
|
||||
public create(createExpenseDto: CreateExpenseDto) {
|
||||
const expense = {
|
||||
id: randomUUID(),
|
||||
date: new Date(createExpenseDto.date),
|
||||
cents: createExpenseDto.cents,
|
||||
categoryId: createExpenseDto.categoryId,
|
||||
merchantId: createExpenseDto.merchantId,
|
||||
subcategoryIds: createExpenseDto.subcategoryIds ?? [],
|
||||
tagIds: createExpenseDto.tagIds ?? [],
|
||||
description: createExpenseDto.description
|
||||
} as Expense;
|
||||
this.expenses.push(expense);
|
||||
|
||||
return this.mapExpense(expense);
|
||||
}
|
||||
|
||||
public findAll() {
|
||||
return this.expenses.map((expense) => {
|
||||
return this.mapExpense(expense);
|
||||
});
|
||||
}
|
||||
|
||||
public findOne(id: string) {
|
||||
const expense = this.getExpense(id);
|
||||
return expense ? this.mapExpense(expense) : undefined;
|
||||
}
|
||||
|
||||
public update(id: string, updateExpenseDto: UpdateExpenseDto) {
|
||||
let index;
|
||||
const expense = this.expenses.find((exp, idx) => {
|
||||
if (exp.id === id) {
|
||||
index = idx;
|
||||
return exp;
|
||||
}
|
||||
});
|
||||
|
||||
if (expense && index) {
|
||||
this.expenses[index] = {
|
||||
...expense,
|
||||
...updateExpenseDto
|
||||
};
|
||||
|
||||
return this.mapExpense(this.expenses[index]);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public remove(id: string) {
|
||||
this.expenses = this.expenses.filter((expense) => expense.id !== id);
|
||||
}
|
||||
|
||||
private getExpense(id: string) {
|
||||
return this.expenses.find((expense) => expense.id === id);
|
||||
}
|
||||
|
||||
private getSubcategories(ids: string[]) {
|
||||
return this.subcategories.filter((sub) => ids.includes(sub.id)).map((s) => s.subcategory);
|
||||
}
|
||||
|
||||
private getTags(ids: string[]) {
|
||||
return this.tags.filter((tag) => ids.includes(tag.id)).map((t) => t.tag);
|
||||
}
|
||||
|
||||
private mapExpense(expense: Expense) {
|
||||
const category = this.categories.find((category) => category.id === expense.categoryId);
|
||||
const merchant = this.merchants.find((merchant) => merchant.id === expense.merchantId);
|
||||
|
||||
return {
|
||||
id: expense.id,
|
||||
date: expense.date,
|
||||
cents: expense.cents,
|
||||
category: category?.category,
|
||||
merchant: merchant?.merchant,
|
||||
subcategories: this.getSubcategories(expense.subcategoryIds),
|
||||
tags: this.getTags(expense.tagIds),
|
||||
description: expense.description
|
||||
} as GetExpenseDto;
|
||||
}
|
||||
}
|
||||
3
src/sub-categories/dto/create-sub-category.dto.ts
Normal file
3
src/sub-categories/dto/create-sub-category.dto.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export class CreateSubCategoryDto {
|
||||
name: string;
|
||||
}
|
||||
4
src/sub-categories/dto/update-sub-category.dto.ts
Normal file
4
src/sub-categories/dto/update-sub-category.dto.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export class UpdateSubCategoryDto {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
10
src/sub-categories/entities/sub-category.entity.ts
Normal file
10
src/sub-categories/entities/sub-category.entity.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class SubCategory {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
}
|
||||
75
src/sub-categories/sub-categories.controller.ts
Normal file
75
src/sub-categories/sub-categories.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 { SubCategoriesService } from './sub-categories.service';
|
||||
import { CreateSubCategoryDto } from './dto/create-sub-category.dto';
|
||||
import { UpdateSubCategoryDto } from './dto/update-sub-category.dto';
|
||||
import { SubCategory } from './entities/sub-category.entity';
|
||||
|
||||
@Controller('sub-categories')
|
||||
export class SubCategoriesController {
|
||||
constructor(private readonly subCategoriesService: SubCategoriesService) { }
|
||||
|
||||
@Get()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findAll(): Promise<SubCategory[]> {
|
||||
return await this.subCategoriesService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findOne(@Param('id') id: string): Promise<SubCategory> {
|
||||
if (!id) {
|
||||
throw new BadRequestException('No ID provided.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.subCategoriesService.findById(id);
|
||||
}
|
||||
catch (error) {
|
||||
throw new NotFoundException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
public async create(@Body() subCategory: CreateSubCategoryDto): Promise<SubCategory> {
|
||||
if (!subCategory.name) {
|
||||
throw new BadRequestException('Sub-category name cannot be empty.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.subCategoriesService.create(subCategory);
|
||||
}
|
||||
catch (error) {
|
||||
throw new InternalServerErrorException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Put()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async update(@Body() subCategory: UpdateSubCategoryDto): Promise<SubCategory> {
|
||||
if (!subCategory.id) {
|
||||
throw new BadRequestException('Sub-category ID cannot be empty.');
|
||||
}
|
||||
|
||||
return await this.subCategoriesService.update(subCategory);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
public async remove(@Param('id') id: string): Promise<void> {
|
||||
return await this.subCategoriesService.remove(id);
|
||||
}
|
||||
}
|
||||
10
src/sub-categories/sub-categories.module.ts
Normal file
10
src/sub-categories/sub-categories.module.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { SubCategoriesService } from './sub-categories.service';
|
||||
import { SubCategoriesController } from './sub-categories.controller';
|
||||
import { SubCategoryDataService } from './sub-category-data.service';
|
||||
|
||||
@Module({
|
||||
controllers: [SubCategoriesController],
|
||||
providers: [SubCategoriesService, SubCategoryDataService]
|
||||
})
|
||||
export class SubCategoriesModule { }
|
||||
35
src/sub-categories/sub-categories.service.ts
Normal file
35
src/sub-categories/sub-categories.service.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateSubCategoryDto } from './dto/create-sub-category.dto';
|
||||
import { UpdateSubCategoryDto } from './dto/update-sub-category.dto';
|
||||
import { SubCategoryDataService } from './sub-category-data.service';
|
||||
import { SubCategory } from './entities/sub-category.entity';
|
||||
|
||||
@Injectable()
|
||||
export class SubCategoriesService {
|
||||
public constructor(private subCategoryDataService: SubCategoryDataService) { }
|
||||
|
||||
public async findAll(): Promise<SubCategory[]> {
|
||||
return await this.subCategoryDataService.getAll();
|
||||
}
|
||||
|
||||
public async findById(id: string): Promise<SubCategory> {
|
||||
const subCategory = await this.subCategoryDataService.getById(id);
|
||||
if (!subCategory) {
|
||||
throw new Error('No sub-category found');
|
||||
}
|
||||
|
||||
return subCategory;
|
||||
}
|
||||
|
||||
public async create(subCategory: CreateSubCategoryDto): Promise<SubCategory> {
|
||||
return await this.subCategoryDataService.create(subCategory.name);
|
||||
}
|
||||
|
||||
public async update(subCategory: UpdateSubCategoryDto): Promise<SubCategory> {
|
||||
return await this.subCategoryDataService.update(subCategory);
|
||||
}
|
||||
|
||||
public async remove(id: string): Promise<void> {
|
||||
await this.subCategoryDataService.delete(id);
|
||||
}
|
||||
}
|
||||
33
src/sub-categories/sub-category-data.service.ts
Normal file
33
src/sub-categories/sub-category-data.service.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { SubCategory } from './entities/sub-category.entity';
|
||||
import { UpdateSubCategoryDto } from './dto/update-sub-category.dto';
|
||||
|
||||
@Injectable()
|
||||
export class SubCategoryDataService {
|
||||
private subCategories: Repository<SubCategory>;
|
||||
|
||||
public constructor(private dataSource: DataSource) {
|
||||
this.subCategories = this.dataSource.getRepository(SubCategory);
|
||||
}
|
||||
|
||||
public async getAll(): Promise<SubCategory[]> {
|
||||
return await this.subCategories.find();
|
||||
}
|
||||
|
||||
public async getById(id: string): Promise<SubCategory | null> {
|
||||
return await this.subCategories.findOneBy({ id });
|
||||
}
|
||||
|
||||
public async create(name: string): Promise<SubCategory> {
|
||||
return await this.subCategories.save({ name });
|
||||
}
|
||||
|
||||
public async update(subCategory: UpdateSubCategoryDto): Promise<SubCategory> {
|
||||
return await this.subCategories.save(subCategory);
|
||||
}
|
||||
|
||||
public async delete(id: string): Promise<void> {
|
||||
await this.subCategories.delete({ id });
|
||||
}
|
||||
}
|
||||
3
src/tags/dto/create-tag.dto.ts
Normal file
3
src/tags/dto/create-tag.dto.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export class CreateTagDto {
|
||||
name: string;
|
||||
}
|
||||
4
src/tags/dto/update-tag.dto.ts
Normal file
4
src/tags/dto/update-tag.dto.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export class UpdateTagDto {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
10
src/tags/entities/tag.entity.ts
Normal file
10
src/tags/entities/tag.entity.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Tag {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
}
|
||||
33
src/tags/tag-data.service.ts
Normal file
33
src/tags/tag-data.service.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Tag } from './entities/tag.entity';
|
||||
import { UpdateTagDto } from './dto/update-tag.dto';
|
||||
|
||||
@Injectable()
|
||||
export class TagDataService {
|
||||
private tags: Repository<Tag>;
|
||||
|
||||
public constructor(private dataSource: DataSource) {
|
||||
this.tags = this.dataSource.getRepository(Tag);
|
||||
}
|
||||
|
||||
public async getAll(): Promise<Tag[]> {
|
||||
return await this.tags.find();
|
||||
}
|
||||
|
||||
public async getById(id: string): Promise<Tag | null> {
|
||||
return await this.tags.findOneBy({ id });
|
||||
}
|
||||
|
||||
public async create(name: string): Promise<Tag> {
|
||||
return await this.tags.save({ name });
|
||||
}
|
||||
|
||||
public async update(tag: UpdateTagDto): Promise<Tag> {
|
||||
return await this.tags.save(tag);
|
||||
}
|
||||
|
||||
public async delete(id: string): Promise<void> {
|
||||
await this.tags.delete({ id });
|
||||
}
|
||||
}
|
||||
75
src/tags/tags.controller.ts
Normal file
75
src/tags/tags.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 { TagsService } from './tags.service';
|
||||
import { CreateTagDto } from './dto/create-tag.dto';
|
||||
import { UpdateTagDto } from './dto/update-tag.dto';
|
||||
import { Tag } from './entities/tag.entity';
|
||||
|
||||
@Controller('tags')
|
||||
export class TagsController {
|
||||
public constructor(private readonly tagsService: TagsService) { }
|
||||
|
||||
@Get()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findAll(): Promise<Tag[]> {
|
||||
return await this.tagsService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async findOne(@Param('id') id: string): Promise<Tag> {
|
||||
if (!id) {
|
||||
throw new BadRequestException('No ID provided.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.tagsService.findById(id);
|
||||
}
|
||||
catch (error) {
|
||||
throw new NotFoundException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
public async create(@Body() tag: CreateTagDto): Promise<Tag> {
|
||||
if (!tag.name) {
|
||||
throw new BadRequestException('Tag name cannot be empty.');
|
||||
}
|
||||
|
||||
try {
|
||||
return await this.tagsService.create(tag);
|
||||
}
|
||||
catch (error) {
|
||||
throw new InternalServerErrorException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@Put()
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async update(@Body() tag: UpdateTagDto): Promise<Tag> {
|
||||
if (!tag.id) {
|
||||
throw new BadRequestException('Tag ID cannot be empty.');
|
||||
}
|
||||
|
||||
return await this.tagsService.update(tag);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
public async remove(@Param('id') id: string): Promise<void> {
|
||||
return await this.tagsService.remove(id);
|
||||
}
|
||||
}
|
||||
10
src/tags/tags.module.ts
Normal file
10
src/tags/tags.module.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { TagsService } from './tags.service';
|
||||
import { TagsController } from './tags.controller';
|
||||
import { TagDataService } from './tag-data.service';
|
||||
|
||||
@Module({
|
||||
controllers: [TagsController],
|
||||
providers: [TagsService, TagDataService]
|
||||
})
|
||||
export class TagsModule { }
|
||||
35
src/tags/tags.service.ts
Normal file
35
src/tags/tags.service.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { CreateTagDto } from './dto/create-tag.dto';
|
||||
import { UpdateTagDto } from './dto/update-tag.dto';
|
||||
import { TagDataService } from './tag-data.service';
|
||||
import { Tag } from './entities/tag.entity';
|
||||
|
||||
@Injectable()
|
||||
export class TagsService {
|
||||
public constructor(private tagDataService: TagDataService) { }
|
||||
|
||||
public async findAll(): Promise<Tag[]> {
|
||||
return await this.tagDataService.getAll();
|
||||
}
|
||||
|
||||
public async findById(id: string): Promise<Tag> {
|
||||
const tag = await this.tagDataService.getById(id);
|
||||
if (!tag) {
|
||||
throw new Error('No tag found');
|
||||
}
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
public async create(tag: CreateTagDto): Promise<Tag> {
|
||||
return await this.tagDataService.create(tag.name);
|
||||
}
|
||||
|
||||
public async update(tag: UpdateTagDto): Promise<Tag> {
|
||||
return await this.tagDataService.update(tag);
|
||||
}
|
||||
|
||||
public async remove(id: string): Promise<void> {
|
||||
await this.tagDataService.delete(id);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue