Add TypeORM DB and Resources #2
12 changed files with 205 additions and 30 deletions
|
|
@ -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: []
|
||||
|
|
|
|||
|
|
@ -11,23 +11,23 @@ export class MerchantDataService {
|
|||
this.merchants = this.dataSource.getRepository(Merchant);
|
||||
}
|
||||
|
||||
public async getAllMerchants(): Promise<Merchant[]> {
|
||||
return this.merchants.find();
|
||||
public async getAll(): Promise<Merchant[]> {
|
||||
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 });
|
||||
}
|
||||
|
||||
public async createMerchant(name: string): Promise<Merchant> {
|
||||
public async create(name: string): Promise<Merchant> {
|
||||
return await this.merchants.save({ name });
|
||||
}
|
||||
|
||||
public async updateMerchant(updateMerchant: UpdateMerchantDto): Promise<Merchant> {
|
||||
return await this.merchants.save(updateMerchant);
|
||||
public async update(merchant: UpdateMerchantDto): Promise<Merchant> {
|
||||
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 });
|
||||
}
|
||||
}
|
||||
|
|
@ -44,13 +44,13 @@ export class MerchantsController {
|
|||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
public async create(@Body() newMerchant: CreateMerchantDto): Promise<Merchant> {
|
||||
if (!newMerchant.name) {
|
||||
public async create(@Body() merchant: CreateMerchantDto): Promise<Merchant> {
|
||||
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<Merchant> {
|
||||
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<void> {
|
||||
return this.merchantsService.remove(id);
|
||||
return await this.merchantsService.remove(id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,4 +7,4 @@ import { MerchantDataService } from './merchant-data.service';
|
|||
controllers: [MerchantsController],
|
||||
providers: [MerchantsService, MerchantDataService]
|
||||
})
|
||||
export class MerchantsModule {}
|
||||
export class MerchantsModule { }
|
||||
|
|
|
|||
|
|
@ -9,11 +9,11 @@ export class MerchantsService {
|
|||
public constructor(private merchantDataService: MerchantDataService) { }
|
||||
|
||||
public async findAll(): Promise<Merchant[]> {
|
||||
return await this.merchantDataService.getAllMerchants();
|
||||
return await this.merchantDataService.getAll();
|
||||
}
|
||||
|
||||
public async findById(id: string): Promise<Merchant> {
|
||||
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<Merchant> {
|
||||
return await this.merchantDataService.createMerchant(newMerchant.name);
|
||||
public async create(merchant: CreateMerchantDto): Promise<Merchant> {
|
||||
return await this.merchantDataService.create(merchant.name);
|
||||
}
|
||||
|
||||
public async update(updateMerchant: UpdateMerchantDto): Promise<Merchant> {
|
||||
return await this.merchantDataService.updateMerchant(updateMerchant);
|
||||
public async update(merchant: UpdateMerchantDto): Promise<Merchant> {
|
||||
return await this.merchantDataService.update(merchant);
|
||||
}
|
||||
|
||||
public async remove(id: string): Promise<void> {
|
||||
await this.merchantDataService.deleteMerchant(id);
|
||||
await this.merchantDataService.delete(id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
3
src/tags/dto/create-tag.dto.ts
Normal file
3
src/tags/dto/create-tag.dto.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export class CreateTagDto {
|
||||
name: string;
|
||||
}
|
||||
4
src/tags/dto/update-tag.dto.ts
Normal file
4
src/tags/dto/update-tag.dto.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export class UpdateTagDto {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
10
src/tags/entities/tag.entity.ts
Normal file
10
src/tags/entities/tag.entity.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Tag {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
}
|
||||
33
src/tags/tag-data.service.ts
Normal file
33
src/tags/tag-data.service.ts
Normal 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 });
|
||||
}
|
||||
}
|
||||
75
src/tags/tags.controller.ts
Normal file
75
src/tags/tags.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 { 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
10
src/tags/tags.module.ts
Normal 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
35
src/tags/tags.service.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue