Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#051] 스탯 조회 구현 #53

Merged
merged 10 commits into from
Apr 12, 2024
2 changes: 1 addition & 1 deletion src/Config/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TypeOrmConfigService implements TypeOrmOptionsFactory {
password: this.configService.get('DB_PASSWORD'),
database: this.configService.get('DB_DATABASE'),
entities: [User, Algorithm, Github, Grade, TotalPoint],
synchronize: process.env.NODE_ENV !== 'prod',
synchronize: true,
namingStrategy: new SnakeNamingStrategy(),
};
}
Expand Down
1 change: 1 addition & 0 deletions src/algorithm/algorithm.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import { AlgorithmRepository } from './algorithm.repository';
@Module({
controllers: [AlgorithmController],
providers: [AlgorithmService, AlgorithmRepository],
exports: [AlgorithmService],
})
export class AlgorithmModule {}
3 changes: 3 additions & 0 deletions src/algorithm/algorithm.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export interface BOJInfo {
export class AlgorithmService {
private logger = new Logger(AlgorithmService.name);
constructor(private algorithmRepository: AlgorithmRepository) {}
async findAlgorithm(userId: string) {
return await this.algorithmRepository.findOneById(userId);
}
async createAlgorithm(userId: string, bojId: string) {
const bojInfo = await this.getBOJInfo(bojId);
const isExist = await this.algorithmRepository.findOneById(userId);
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { GithubModule } from './github/github.module';
import { GradeController } from './grade/grade.controller';
import { GradeService } from './grade/grade.service';
import { GradeModule } from './grade/grade.module';
import { RankModule } from './rank/rank.module';
import * as process from 'process';

@Module({
Expand All @@ -27,6 +28,7 @@ import * as process from 'process';
UserModule,
GithubModule,
GradeModule,
RankModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
2 changes: 2 additions & 0 deletions src/github/github.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Module } from '@nestjs/common';
import { GithubService } from './github.service';
import { GithubController } from './github.controller';
import { GithubRepository } from './github.repository';
import { GradeService } from '../grade/grade.service';

@Module({
providers: [GithubService, GithubRepository],
controllers: [GithubController],
exports: [GithubService],
})
export class GithubModule {}
11 changes: 10 additions & 1 deletion src/github/github.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import {
BadRequestException,
Injectable,
Logger,
NotFoundException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { GithubRepository } from './github.repository';
import { Github } from '../Entity/github';
Expand All @@ -12,6 +17,10 @@ export class GithubService {
private githubRepository: GithubRepository,
) {}

async findGithub(userId: string) {
return await this.githubRepository.findOne(userId);
}

public async createGithub(tokens: CreateGithubDto, userId: string) {
const userResource = await this.getUserResource(tokens.accessToken);

Expand Down
1 change: 1 addition & 0 deletions src/grade/grade.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import { GradeController } from './grade.controller';
@Module({
providers: [GradeService, GradeRepository],
controllers: [GradeController],
exports: [GradeService],
})
export class GradeModule {}
5 changes: 5 additions & 0 deletions src/grade/grade.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { Grade } from '../Entity/grade';
@Injectable()
export class GradeService {
constructor(private gradeRepository: GradeRepository) {}

async findGrade(userId: string) {
return await this.gradeRepository.findOne(userId);
}

public async gradeCreate(userId: string, grade: number) {
const isExist = await this.gradeRepository.findOne(userId);

Expand Down
15 changes: 15 additions & 0 deletions src/rank/rank.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Controller, Get, Param, UseGuards } from '@nestjs/common';
import { StatFindDto } from './stat-find.dto';
import { RankService } from './rank.service';
import { JwtAuthGuard } from '../auth/guard/jwt-auth.guard';

@UseGuards(JwtAuthGuard)
@Controller('api')
export class RankController {
constructor(private rankService: RankService) {}

@Get('stat/:id')
async statFind(@Param('id') userId: string): Promise<StatFindDto> {
return await this.rankService.findStat(userId);
}
}
13 changes: 13 additions & 0 deletions src/rank/rank.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { RankController } from './rank.controller';
import { RankService } from './rank.service';
import { GithubModule } from '../github/github.module';
import { AlgorithmModule } from '../algorithm/algorithm.module';
import { GradeModule } from '../grade/grade.module';

@Module({
imports: [GithubModule, AlgorithmModule, GradeModule],
controllers: [RankController],
providers: [RankService],
})
export class RankModule {}
89 changes: 89 additions & 0 deletions src/rank/rank.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Test, TestingModule } from '@nestjs/testing';
import { RankService } from './rank.service';
import { GithubService } from '../github/github.service';
import { AlgorithmService } from '../algorithm/algorithm.service';
import { GradeService } from '../grade/grade.service';
import { Github } from '../Entity/github';
import { Algorithm } from '../Entity/algorithm';
import { Grade } from '../Entity/grade';

const mockGitService = {
findGithub: jest.fn(),
};

const mockGradeService = {
findGrade: jest.fn(),
};

const mockAlgorithmService = {
findAlgorithm: jest.fn(),
};

describe('RankService', () => {
let service: RankService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
RankService,
{
provide: GithubService,
useValue: mockGitService,
},
{
provide: GradeService,
useValue: mockGradeService,
},
{
provide: AlgorithmService,
useValue: mockAlgorithmService,
},
],
}).compile();

service = module.get<RankService>(RankService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});

describe('findStat', function () {
it('should return stat', async function () {
const userId = 'qwe';
const github = new Github();
const algorithm = new Algorithm();
const grade = new Grade();
github.point = 123;
algorithm.point = 123;
grade.grade = 123;

mockGitService.findGithub.mockResolvedValue(github);
mockAlgorithmService.findAlgorithm.mockResolvedValue(algorithm);
mockGradeService.findGrade.mockResolvedValue(grade);

const result = await service.findStat(userId);
expect(result.grade).toEqual(grade.grade);
expect(result.githubPoint).toEqual(github.point);
expect(result.algorithmPoint).toEqual(algorithm.point);
});

it('should return null if stat does not exist', async function () {
const userId = 'qwe';
const github = new Github();
const algorithm = null;
const grade = new Grade();
github.point = 123;
grade.grade = 123;

mockGitService.findGithub.mockResolvedValue(github);
mockAlgorithmService.findAlgorithm.mockResolvedValue(algorithm);
mockGradeService.findGrade.mockResolvedValue(grade);

const result = await service.findStat(userId);
expect(result.grade).toEqual(grade.grade);
expect(result.githubPoint).toEqual(github.point);
expect(result.algorithmPoint).toEqual(null);
});
});
});
25 changes: 25 additions & 0 deletions src/rank/rank.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common';
import { GithubService } from '../github/github.service';
import { AlgorithmService } from '../algorithm/algorithm.service';
import { GradeService } from '../grade/grade.service';

@Injectable()
export class RankService {
constructor(
private githubService: GithubService,
private algorithmService: AlgorithmService,
private gradeService: GradeService,
) {}

async findStat(userId: string) {
const github = await this.githubService.findGithub(userId);
const algorithm = await this.algorithmService.findAlgorithm(userId);
const grade = await this.gradeService.findGrade(userId);

return {
githubPoint: github ? github.point : null,
algorithmPoint: algorithm ? algorithm.point : null,
grade: grade ? grade.grade : null,
};
}
}
5 changes: 5 additions & 0 deletions src/rank/stat-find.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class StatFindDto {
githubPoint: number;
algorithmPoint: number;
grade: number;
}
Loading