Add TypeORM DB and Resources #2
8 changed files with 176 additions and 3 deletions
|
|
@ -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: []
|
||||
|
|
|
|||
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()
|
||||
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 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 });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue