Skip to content

Commit

Permalink
Merge pull request #90 from Boost-Coder/feature/Transaction-#89
Browse files Browse the repository at this point in the history
[#89] 트랜잭션 적용 및 리펙토링
  • Loading branch information
namewhat99 authored Apr 22, 2024
2 parents 3a6b0ee + bfbb11b commit 990c45a
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 175 deletions.
76 changes: 76 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"rxjs": "^7.8.1",
"typeorm": "^0.3.20",
"typeorm-naming-strategies": "^4.1.0",
"typeorm-transactional": "^0.5.0",
"uuid": "^9.0.1",
"winston": "^3.13.0",
"winston-daily-rotate-file": "^5.0.0"
Expand Down
9 changes: 9 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ import * as process from 'process';
import { HttpLoggerInterceptor } from './utils/httpLoggerInterceptor';
import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core';
import { HttpExceptionFilter } from './utils/httpExceptionFilter';
import { addTransactionalDataSource } from 'typeorm-transactional';
import { DataSource } from 'typeorm';

@Module({
imports: [
TypeOrmModule.forRootAsync({
useClass: TypeOrmConfigService,
async dataSourceFactory(options) {
if (!options) {
throw new Error('Invalid options passed');
}

return addTransactionalDataSource(new DataSource(options));
},
}),
ConfigModule.forRoot({
isGlobal: true,
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { AppModule } from './app.module';
import { winstonLogger } from './Config/winston.config';
import { ValidationPipe } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { initializeTransactionalContext } from 'typeorm-transactional';

async function bootstrap() {
initializeTransactionalContext();
const app = await NestFactory.create(AppModule, {
logger: winstonLogger,
});
Expand Down
64 changes: 9 additions & 55 deletions src/stat/repository/algorithm.repository.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,23 @@
import { Inject, Injectable, Scope } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { REQUEST } from '@nestjs/core';
import { Algorithm } from '../../Entity/algorithm';
import { RankListOptionDto } from '../dto/rank-list-option.dto';
import { User } from '../../Entity/user';
import { PointFindDto } from '../dto/rank-find.dto';
import { StatRepository } from '../../utils/stat.repository';

@Injectable({ scope: Scope.REQUEST })
@Injectable()
export class AlgorithmRepository extends StatRepository {
constructor(dataSource: DataSource, @Inject(REQUEST) req: Request) {
super(dataSource, req, Algorithm);
}

async save(algorithm: Algorithm) {
await this.repository.save(algorithm);
constructor(dataSource: DataSource) {
super(dataSource, Algorithm);
}

async findOneById(userId: string) {
return await this.repository.findOneBy({ userId: userId });
}
public async findIndividualAlgorithmRank(
userId: string,
options: PointFindDto,
) {
const queryBuilder = this.repository
.createQueryBuilder()
.select(['b.rank', 'b.user_id', 'b.major'])
.distinct(true)
.from((sub) => {
return sub
.select('RANK() OVER (ORDER BY a.point DESC)', 'rank')
.addSelect('a.user_id', 'user_id')
.addSelect('a.point', 'point')
.from(Algorithm, 'a')
.innerJoin(User, 'u', 'a.user_id = u.user_id')
.addSelect('u.major', 'major')
.where(this.createClassificationOption(options));
}, 'b')
.where(`b.user_id = '${userId}'`);

return await queryBuilder.getRawOne();
}

createCursorOption(options: RankListOptionDto) {
if (!options.cursorPoint && !options.cursorUserId) {
return 'b.point > -1';
} else {
return `b.point < ${options.cursorPoint} or b.point = ${options.cursorPoint} AND b.user_id > '${options.cursorUserId}'`;
}
}

createClassificationOption(options: PointFindDto) {
if (options.major != null) {
return `u.major like '${options.major}'`;
} else {
return `u.id > 0`;
}
return await this.findOneBy({ userId: userId });
}

async update(userId: string, algorithm: Algorithm) {
await this.repository.update({ userId: userId }, algorithm);
async updateAlgorithm(userId: string, algorithm: Algorithm) {
await this.update({ userId: userId }, algorithm);
}

async delete(userId: string) {
await this.repository.delete({ userId: userId });
async deleteAlgorithm(userId: string) {
await this.delete({ userId: userId });
}
}
25 changes: 10 additions & 15 deletions src/stat/repository/github.repository.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { Inject, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { Github } from '../../Entity/github';
import { DataSource } from 'typeorm';
import { REQUEST } from '@nestjs/core';
import { StatRepository } from '../../utils/stat.repository';

@Injectable()
export class GithubRepository extends StatRepository {
constructor(dataSource: DataSource, @Inject(REQUEST) req: Request) {
super(dataSource, req, Github);
constructor(dataSource: DataSource) {
super(dataSource, Github);
}

public async save(github: Github) {
await this.repository.save(github);
public async findOneById(id: string) {
return await this.findOneBy({ userId: id });
}

public async findOne(id: string) {
return await this.repository.findOneBy({ userId: id });
}

public async update(github: Github) {
return await this.repository.update(
public async updateGithub(github: Github) {
return await this.update(
{ userId: github.userId },
{
githubId: github.githubId,
Expand All @@ -29,11 +24,11 @@ export class GithubRepository extends StatRepository {
);
}

public async delete(id: string) {
await this.repository.delete({ userId: id });
public async deleteGithub(id: string) {
await this.delete({ userId: id });
}

public async findAll() {
return await this.repository.find();
return await this.find();
}
}
23 changes: 9 additions & 14 deletions src/stat/repository/grade.repository.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { Inject, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { REQUEST } from '@nestjs/core';
import { Grade } from '../../Entity/grade';
import { StatRepository } from '../../utils/stat.repository';

@Injectable()
export class GradeRepository extends StatRepository {
constructor(dataSource: DataSource, @Inject(REQUEST) req: Request) {
super(dataSource, req, Grade);
constructor(dataSource: DataSource) {
super(dataSource, Grade);
}

public async save(grade: Grade) {
await this.repository.save(grade);
public async findOneById(id: string) {
return await this.findOneBy({ userId: id });
}

public async findOne(id: string) {
return await this.repository.findOneBy({ userId: id });
}

public async update(newGrade: Grade) {
return await this.repository.update(
public async updateGrade(newGrade: Grade) {
return await this.update(
{ userId: newGrade.userId },
{
grade: newGrade.grade,
Expand All @@ -28,7 +23,7 @@ export class GradeRepository extends StatRepository {
);
}

public async delete(id: string) {
await this.repository.delete({ userId: id });
public async deleteGrade(id: string) {
await this.delete({ userId: id });
}
}
18 changes: 7 additions & 11 deletions src/stat/repository/total.repository.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { DataSource } from 'typeorm';
import { Inject } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { TotalPoint } from '../../Entity/totalPoint';
import { StatRepository } from '../../utils/stat.repository';
import { Injectable } from '@nestjs/common';

@Injectable()
export class TotalRepository extends StatRepository {
constructor(dataSource: DataSource, @Inject(REQUEST) req: Request) {
super(dataSource, req, TotalPoint);
constructor(private dataSource: DataSource) {
super(dataSource, TotalPoint);
}

async findOneById(userId: string) {
return await this.repository.findOneBy({ userId: userId });
return await this.findOneBy({ userId: userId });
}

async update(total: TotalPoint, userId: string) {
return await this.repository.update({ userId: userId }, total);
}

async save(total: TotalPoint) {
return await this.repository.save(total);
async updateTotal(total: TotalPoint, userId: string) {
return await this.update({ userId: userId }, total);
}
}
Loading

0 comments on commit 990c45a

Please sign in to comment.