added tags resource

This commit is contained in:
Joe Arndt 2026-02-08 16:17:17 -06:00
parent b5ced781c9
commit c90276982f
12 changed files with 205 additions and 30 deletions

View file

@ -1,18 +1,23 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { MerchantsModule } from './merchants/merchants.module'; 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 { 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({ @Module({
imports: [ imports: [
TypeOrmModule.forRoot(sqliteConfig),
MerchantsModule, MerchantsModule,
TypeOrmModule.forRoot({ TagsModule
type: 'sqlite',
database: 'common-cents.db',
entities: [Merchant],
// synchronize: true // schema:sync
})
], ],
controllers: [AppController], controllers: [AppController],
providers: [] providers: []

View file

@ -11,23 +11,23 @@ export class MerchantDataService {
this.merchants = this.dataSource.getRepository(Merchant); this.merchants = this.dataSource.getRepository(Merchant);
} }
public async getAllMerchants(): Promise<Merchant[]> { public async getAll(): Promise<Merchant[]> {
return this.merchants.find(); return await this.merchants.find();
} }
public async getMerchantById(id: string): Promise<Merchant | null> { public async getById(id: string): Promise<Merchant | null> {
return await this.merchants.findOneBy({ id }); return await this.merchants.findOneBy({ id });
} }
public async createMerchant(name: string): Promise<Merchant> { public async create(name: string): Promise<Merchant> {
return await this.merchants.save({ name }); return await this.merchants.save({ name });
} }
public async updateMerchant(updateMerchant: UpdateMerchantDto): Promise<Merchant> { public async update(merchant: UpdateMerchantDto): Promise<Merchant> {
return await this.merchants.save(updateMerchant); return await this.merchants.save(merchant);
} }
public async deleteMerchant(id: string): Promise<void> { public async delete(id: string): Promise<void> {
await this.merchants.delete({ id }); await this.merchants.delete({ id });
} }
} }

View file

@ -44,13 +44,13 @@ export class MerchantsController {
@Post() @Post()
@HttpCode(HttpStatus.CREATED) @HttpCode(HttpStatus.CREATED)
public async create(@Body() newMerchant: CreateMerchantDto): Promise<Merchant> { public async create(@Body() merchant: CreateMerchantDto): Promise<Merchant> {
if (!newMerchant.name) { if (!merchant.name) {
throw new BadRequestException('Merchant name cannot be empty.'); throw new BadRequestException('Merchant name cannot be empty.');
} }
try { try {
return await this.merchantsService.create(newMerchant); return await this.merchantsService.create(merchant);
} }
catch (error) { catch (error) {
throw new InternalServerErrorException(error); throw new InternalServerErrorException(error);
@ -59,17 +59,17 @@ export class MerchantsController {
@Put() @Put()
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
public async update(@Body() updateMerchant: UpdateMerchantDto) { public async update(@Body() merchant: UpdateMerchantDto): Promise<Merchant> {
if (!updateMerchant.id) { if (!merchant.id) {
return new BadRequestException('Merchant ID cannot be empty.'); throw new BadRequestException('Merchant ID cannot be empty.');
} }
return this.merchantsService.update(updateMerchant); return await this.merchantsService.update(merchant);
} }
@Delete(':id') @Delete(':id')
@HttpCode(HttpStatus.NO_CONTENT) @HttpCode(HttpStatus.NO_CONTENT)
public async remove(@Param('id') id: string): Promise<void> { public async remove(@Param('id') id: string): Promise<void> {
return this.merchantsService.remove(id); return await this.merchantsService.remove(id);
} }
} }

View file

@ -7,4 +7,4 @@ import { MerchantDataService } from './merchant-data.service';
controllers: [MerchantsController], controllers: [MerchantsController],
providers: [MerchantsService, MerchantDataService] providers: [MerchantsService, MerchantDataService]
}) })
export class MerchantsModule {} export class MerchantsModule { }

View file

@ -9,11 +9,11 @@ export class MerchantsService {
public constructor(private merchantDataService: MerchantDataService) { } public constructor(private merchantDataService: MerchantDataService) { }
public async findAll(): Promise<Merchant[]> { public async findAll(): Promise<Merchant[]> {
return await this.merchantDataService.getAllMerchants(); return await this.merchantDataService.getAll();
} }
public async findById(id: string): Promise<Merchant> { public async findById(id: string): Promise<Merchant> {
const merchant = await this.merchantDataService.getMerchantById(id); const merchant = await this.merchantDataService.getById(id);
if (!merchant) { if (!merchant) {
throw new Error('Merchant not found.'); throw new Error('Merchant not found.');
} }
@ -21,15 +21,15 @@ export class MerchantsService {
return merchant; return merchant;
} }
public async create(newMerchant: CreateMerchantDto): Promise<Merchant> { public async create(merchant: CreateMerchantDto): Promise<Merchant> {
return await this.merchantDataService.createMerchant(newMerchant.name); return await this.merchantDataService.create(merchant.name);
} }
public async update(updateMerchant: UpdateMerchantDto): Promise<Merchant> { public async update(merchant: UpdateMerchantDto): Promise<Merchant> {
return await this.merchantDataService.updateMerchant(updateMerchant); return await this.merchantDataService.update(merchant);
} }
public async remove(id: string): Promise<void> { public async remove(id: string): Promise<void> {
await this.merchantDataService.deleteMerchant(id); await this.merchantDataService.delete(id);
} }
} }

View file

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

View file

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

View file

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

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

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