added categories resource
This commit is contained in:
parent
c90276982f
commit
50e2b6e2b7
8 changed files with 166 additions and 2 deletions
|
|
@ -5,19 +5,22 @@ import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
|
||||||
import { Merchant } from './merchants/entities/merchant.entity';
|
import { Merchant } from './merchants/entities/merchant.entity';
|
||||||
import { TagsModule } from './tags/tags.module';
|
import { TagsModule } from './tags/tags.module';
|
||||||
import { Tag } from './tags/entities/tag.entity';
|
import { Tag } from './tags/entities/tag.entity';
|
||||||
|
import { CategoriesModule } from './categories/categories.module';
|
||||||
|
import { Category } from './categories/entities/category.entity';
|
||||||
|
|
||||||
const sqliteConfig: TypeOrmModuleOptions = {
|
const sqliteConfig: TypeOrmModuleOptions = {
|
||||||
synchronize: true, // typeorm -h (schema:sync)
|
synchronize: true, // typeorm -h (schema:sync)
|
||||||
type: 'sqlite',
|
type: 'sqlite',
|
||||||
database: 'common-cents.db',
|
database: 'common-cents.db',
|
||||||
entities: [Merchant, Tag]
|
entities: [Merchant, Tag, Category]
|
||||||
}
|
}
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forRoot(sqliteConfig),
|
TypeOrmModule.forRoot(sqliteConfig),
|
||||||
MerchantsModule,
|
MerchantsModule,
|
||||||
TagsModule
|
TagsModule,
|
||||||
|
CategoriesModule
|
||||||
],
|
],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: []
|
providers: []
|
||||||
|
|
|
||||||
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 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;
|
||||||
|
}
|
||||||
1
src/categories/entities/category.entity.ts
Normal file
1
src/categories/entities/category.entity.ts
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
export class Category {}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue