diff --git a/src/app.module.ts b/src/app.module.ts index 8f4b4f2..99710f3 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,18 +1,23 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { MerchantsModule } from './merchants/merchants.module'; -import { TypeOrmModule } from '@nestjs/typeorm'; +import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; import { Merchant } from './merchants/entities/merchant.entity'; +import { TagsModule } from './tags/tags.module'; +import { Tag } from './tags/entities/tag.entity'; + +const sqliteConfig: TypeOrmModuleOptions = { + synchronize: true, // typeorm -h (schema:sync) + type: 'sqlite', + database: 'common-cents.db', + entities: [Merchant, Tag] +} @Module({ imports: [ + TypeOrmModule.forRoot(sqliteConfig), MerchantsModule, - TypeOrmModule.forRoot({ - type: 'sqlite', - database: 'common-cents.db', - entities: [Merchant], - // synchronize: true // schema:sync - }) + TagsModule ], controllers: [AppController], providers: [] diff --git a/src/merchants/merchant-data.service.ts b/src/merchants/merchant-data.service.ts index ad61ae0..efbb956 100644 --- a/src/merchants/merchant-data.service.ts +++ b/src/merchants/merchant-data.service.ts @@ -11,23 +11,23 @@ export class MerchantDataService { this.merchants = this.dataSource.getRepository(Merchant); } - public async getAllMerchants(): Promise { - return this.merchants.find(); + public async getAll(): Promise { + return await this.merchants.find(); } - public async getMerchantById(id: string): Promise { + public async getById(id: string): Promise { return await this.merchants.findOneBy({ id }); } - public async createMerchant(name: string): Promise { + public async create(name: string): Promise { return await this.merchants.save({ name }); } - public async updateMerchant(updateMerchant: UpdateMerchantDto): Promise { - return await this.merchants.save(updateMerchant); + public async update(merchant: UpdateMerchantDto): Promise { + return await this.merchants.save(merchant); } - public async deleteMerchant(id: string): Promise { + public async delete(id: string): Promise { await this.merchants.delete({ id }); } } \ No newline at end of file diff --git a/src/merchants/merchants.controller.ts b/src/merchants/merchants.controller.ts index 339b9c2..97a7ac3 100644 --- a/src/merchants/merchants.controller.ts +++ b/src/merchants/merchants.controller.ts @@ -44,13 +44,13 @@ export class MerchantsController { @Post() @HttpCode(HttpStatus.CREATED) - public async create(@Body() newMerchant: CreateMerchantDto): Promise { - if (!newMerchant.name) { + public async create(@Body() merchant: CreateMerchantDto): Promise { + if (!merchant.name) { throw new BadRequestException('Merchant name cannot be empty.'); } try { - return await this.merchantsService.create(newMerchant); + return await this.merchantsService.create(merchant); } catch (error) { throw new InternalServerErrorException(error); @@ -59,17 +59,17 @@ export class MerchantsController { @Put() @HttpCode(HttpStatus.OK) - public async update(@Body() updateMerchant: UpdateMerchantDto) { - if (!updateMerchant.id) { - return new BadRequestException('Merchant ID cannot be empty.'); + public async update(@Body() merchant: UpdateMerchantDto): Promise { + if (!merchant.id) { + throw new BadRequestException('Merchant ID cannot be empty.'); } - return this.merchantsService.update(updateMerchant); + return await this.merchantsService.update(merchant); } @Delete(':id') @HttpCode(HttpStatus.NO_CONTENT) public async remove(@Param('id') id: string): Promise { - return this.merchantsService.remove(id); + return await this.merchantsService.remove(id); } } diff --git a/src/merchants/merchants.module.ts b/src/merchants/merchants.module.ts index 9ce6372..ffb8ca0 100644 --- a/src/merchants/merchants.module.ts +++ b/src/merchants/merchants.module.ts @@ -7,4 +7,4 @@ import { MerchantDataService } from './merchant-data.service'; controllers: [MerchantsController], providers: [MerchantsService, MerchantDataService] }) -export class MerchantsModule {} +export class MerchantsModule { } diff --git a/src/merchants/merchants.service.ts b/src/merchants/merchants.service.ts index c85ef46..ef98690 100644 --- a/src/merchants/merchants.service.ts +++ b/src/merchants/merchants.service.ts @@ -9,11 +9,11 @@ export class MerchantsService { public constructor(private merchantDataService: MerchantDataService) { } public async findAll(): Promise { - return await this.merchantDataService.getAllMerchants(); + return await this.merchantDataService.getAll(); } public async findById(id: string): Promise { - const merchant = await this.merchantDataService.getMerchantById(id); + const merchant = await this.merchantDataService.getById(id); if (!merchant) { throw new Error('Merchant not found.'); } @@ -21,15 +21,15 @@ export class MerchantsService { return merchant; } - public async create(newMerchant: CreateMerchantDto): Promise { - return await this.merchantDataService.createMerchant(newMerchant.name); + public async create(merchant: CreateMerchantDto): Promise { + return await this.merchantDataService.create(merchant.name); } - public async update(updateMerchant: UpdateMerchantDto): Promise { - return await this.merchantDataService.updateMerchant(updateMerchant); + public async update(merchant: UpdateMerchantDto): Promise { + return await this.merchantDataService.update(merchant); } public async remove(id: string): Promise { - await this.merchantDataService.deleteMerchant(id); + await this.merchantDataService.delete(id); } } diff --git a/src/tags/dto/create-tag.dto.ts b/src/tags/dto/create-tag.dto.ts new file mode 100644 index 0000000..8235483 --- /dev/null +++ b/src/tags/dto/create-tag.dto.ts @@ -0,0 +1,3 @@ +export class CreateTagDto { + name: string; +} diff --git a/src/tags/dto/update-tag.dto.ts b/src/tags/dto/update-tag.dto.ts new file mode 100644 index 0000000..ab32e5f --- /dev/null +++ b/src/tags/dto/update-tag.dto.ts @@ -0,0 +1,4 @@ +export class UpdateTagDto { + id: string; + name: string; +} diff --git a/src/tags/entities/tag.entity.ts b/src/tags/entities/tag.entity.ts new file mode 100644 index 0000000..fdf7a2d --- /dev/null +++ b/src/tags/entities/tag.entity.ts @@ -0,0 +1,10 @@ +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; + +@Entity() +export class Tag { + @PrimaryGeneratedColumn('uuid') + id: string; + + @Column() + name: string; +} diff --git a/src/tags/tag-data.service.ts b/src/tags/tag-data.service.ts new file mode 100644 index 0000000..df417c5 --- /dev/null +++ b/src/tags/tag-data.service.ts @@ -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; + + public constructor(private dataSource: DataSource) { + this.tags = this.dataSource.getRepository(Tag); + } + + public async getAll(): Promise { + return await this.tags.find(); + } + + public async getById(id: string): Promise { + return await this.tags.findOneBy({ id }); + } + + public async create(name: string): Promise { + return await this.tags.save({ name }); + } + + public async update(tag: UpdateTagDto): Promise { + return await this.tags.save(tag); + } + + public async delete(id: string): Promise { + await this.tags.delete({ id }); + } +} diff --git a/src/tags/tags.controller.ts b/src/tags/tags.controller.ts new file mode 100644 index 0000000..d27412e --- /dev/null +++ b/src/tags/tags.controller.ts @@ -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 { + return await this.tagsService.findAll(); + } + + @Get(':id') + @HttpCode(HttpStatus.OK) + public async findOne(@Param('id') id: string): Promise { + 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 { + if (!tag.name) { + throw new BadRequestException('Tag name cannot be empty.'); + } + + try { + return this.tagsService.create(tag); + } + catch (error) { + throw new InternalServerErrorException(error); + } + } + + @Put() + @HttpCode(HttpStatus.OK) + public async update(@Body() tag: UpdateTagDto): Promise { + 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 { + return await this.tagsService.remove(id); + } +} diff --git a/src/tags/tags.module.ts b/src/tags/tags.module.ts new file mode 100644 index 0000000..5f4c074 --- /dev/null +++ b/src/tags/tags.module.ts @@ -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 { } diff --git a/src/tags/tags.service.ts b/src/tags/tags.service.ts new file mode 100644 index 0000000..ab5ff92 --- /dev/null +++ b/src/tags/tags.service.ts @@ -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 { + return await this.tagDataService.getAll(); + } + + public async findById(id: string): Promise { + const tag = await this.tagDataService.getById(id); + if (!tag) { + throw new Error('No tag found'); + } + + return tag; + } + + public async create(tag: CreateTagDto): Promise { + return await this.tagDataService.create(tag.name); + } + + public async update(tag: UpdateTagDto): Promise { + return await this.tagDataService.update(tag); + } + + public async remove(id: string): Promise { + await this.tagDataService.delete(id); + } +}