Add TypeORM DB and Resources #2

Merged
joe merged 8 commits from add-sqlite into master 2026-02-09 00:10:26 +00:00
8 changed files with 176 additions and 3 deletions
Showing only changes of commit 7599080106 - Show all commits

View file

@ -1,18 +1,20 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { MerchantsModule } from './merchants/merchants.module';
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]
entities: [Merchant, Tag, Category, SubCategory]
}
@Module({
@ -20,7 +22,8 @@ const sqliteConfig: TypeOrmModuleOptions = {
TypeOrmModule.forRoot(sqliteConfig),
MerchantsModule,
TagsModule,
CategoriesModule
CategoriesModule,
SubCategoriesModule
],
controllers: [AppController],
providers: []

View file

@ -0,0 +1,3 @@
export class CreateSubCategoryDto {
name: string;
}

View file

@ -0,0 +1,4 @@
export class UpdateSubCategoryDto {
id: string;
name: string;
}

View file

@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class SubCategory {
@PrimaryGeneratedColumn()
id: string;
@Column()
name: string;
}

View 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 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);
}
}

View 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 { }

View 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);
}
}

View 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 });
}
}