From 5bc87cd0d9aeb9ea6d26ac1476dc884fba705da6 Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Sat, 4 Nov 2023 23:16:37 +0800 Subject: [PATCH] feat: transaction --- src/coffees/coffees.module.ts | 3 ++- src/coffees/coffees.service.ts | 26 ++++++++++++++++++- src/coffees/entities/coffee.entity.ts | 3 +++ .../entities/event.entity/event.entity.ts | 18 +++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/events/entities/event.entity/event.entity.ts diff --git a/src/coffees/coffees.module.ts b/src/coffees/coffees.module.ts index e31687c..62af814 100644 --- a/src/coffees/coffees.module.ts +++ b/src/coffees/coffees.module.ts @@ -4,13 +4,14 @@ import { CoffeesService } from './coffees.service'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Coffee } from './entities/coffee.entity'; import { Flavor } from './entities/flavor.entity'; +import { Event } from '../events/entities/event.entity/event.entity'; // nest g module 命令行创建 module // 也可以简写 nest g mo // 也可以简写 nest g mo moduleName @Module({ - imports: [TypeOrmModule.forFeature([Coffee, Flavor])], + imports: [TypeOrmModule.forFeature([Coffee, Flavor, Event])], controllers: [CoffeesController], providers: [CoffeesService], }) diff --git a/src/coffees/coffees.service.ts b/src/coffees/coffees.service.ts index 7b2127d..c71aeaa 100644 --- a/src/coffees/coffees.service.ts +++ b/src/coffees/coffees.service.ts @@ -8,9 +8,10 @@ import { Coffee } from './entities/coffee.entity'; import { CreateCoffeesDto } from './dto/create-coffees.dto/create-coffees.dto'; import { UpdateCoffeesDto } from './dto/update--coffees.dto/update--coffees.dto'; import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; +import { Connection, Repository } from 'typeorm'; import { Flavor } from './entities/flavor.entity'; import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto/pagination-query.dto'; +import { Event } from '../events/entities/event.entity/event.entity'; // nest generate service 命令行创建 controller // 简写:nest j s @@ -22,6 +23,7 @@ export class CoffeesService { private readonly coffeeRepository: Repository, @InjectRepository(Flavor) private readonly flavorRepository: Repository, + private readonly connection: Connection, ) {} findAll(paginationQuery: PaginationQueryDto) { @@ -79,6 +81,28 @@ export class CoffeesService { return this.coffeeRepository.remove(coffee); } + async recommendCoffee(coffee: Coffee) { + const queryRunner = this.connection.createQueryRunner(); + + await queryRunner.connect(); + await queryRunner.startTransaction(); + + try { + coffee.recommendations++; + const recommendEvent = new Event(); + recommendEvent.name = 'recommend_coffee'; + recommendEvent.type = 'coffee'; + recommendEvent.payload = { coffeeId: coffee.id }; + await queryRunner.manager.save(coffee); + await queryRunner.manager.save(recommendEvent); + await queryRunner.commitTransaction(); + } catch (err) { + await queryRunner.rollbackTransaction(); + } finally { + await queryRunner.release(); + } + } + private async preloadFlavorsByNames(name: string): Promise { const existFlavor = await this.flavorRepository.findOneBy({ name }); if (existFlavor) { diff --git a/src/coffees/entities/coffee.entity.ts b/src/coffees/entities/coffee.entity.ts index c60ad6a..64d274c 100644 --- a/src/coffees/entities/coffee.entity.ts +++ b/src/coffees/entities/coffee.entity.ts @@ -19,6 +19,9 @@ export class Coffee { @Column() brand: string; + @Column({ default: 0 }) + recommendations: number; + // @Column('json', { nullable: true }) // 可选 @JoinTable() // cascade: true 表示级联保存, 保存coffee的时候, 会级联保存flavor diff --git a/src/events/entities/event.entity/event.entity.ts b/src/events/entities/event.entity/event.entity.ts new file mode 100644 index 0000000..28bd32a --- /dev/null +++ b/src/events/entities/event.entity/event.entity.ts @@ -0,0 +1,18 @@ +import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'; + +@Index(['name', 'type']) // 复合索引 +@Entity() +export class Event { + @PrimaryGeneratedColumn() + id: number; + + @Column() + type: string; + + @Index() // 索引 + @Column() + name: string; + + @Column('json') + payload: Record; +}