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

[#41] 학점 등록 / 수정 기능 #44

Merged
merged 9 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { AlgorithmModule } from './algorithm/algorithm.module';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
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 * as process from 'process';

@Module({
Expand All @@ -23,6 +26,7 @@ import * as process from 'process';
AuthModule,
UserModule,
GithubModule,
GradeModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
41 changes: 41 additions & 0 deletions src/grade/grade.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
Body,
Controller,
Delete,
Param,
Patch,
Post,
UseGuards,
} from '@nestjs/common';
import { GradeService } from './grade.service';
import { JwtAuthGuard } from '../auth/guard/jwt-auth.guard';
import { OwnershipGuard } from '../auth/guard/ownership.guard';
import { UserId } from '../decorator/user-id.decorator';

@UseGuards(JwtAuthGuard)
@Controller('api/stat/grade')
export class GradeController {
constructor(private readonly gradeService: GradeService) {}
@Post()
public async createGrade(
@Body('grade') grade: number,
@UserId() userId: string,
) {
await this.gradeService.gradeCreate(userId, grade);
}

@Patch(':id')
@UseGuards(OwnershipGuard)
public async modifyGrade(
@Body('grade') grade: number,
@Param('id') userId: string,
) {
await this.gradeService.gradeModify(userId, grade);
}

@Delete(':id')
@UseGuards(OwnershipGuard)
public async deleteGrade() {
await this.gradeService.gradeDelete();
}
}
10 changes: 10 additions & 0 deletions src/grade/grade.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { GradeService } from './grade.service';
import { GradeRepository } from './grade.repository';
import { GradeController } from './grade.controller';

@Module({
providers: [GradeService, GradeRepository],
controllers: [GradeController],
})
export class GradeModule {}
36 changes: 36 additions & 0 deletions src/grade/grade.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { BaseRepository } from '../utils/base.repository';
import { Inject, Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { Github } from '../Entity/github';
import { REQUEST } from '@nestjs/core';
import { Grade } from '../Entity/grade';

@Injectable()
export class GradeRepository extends BaseRepository {
private repository: Repository<Grade>;

constructor(dataSource: DataSource, @Inject(REQUEST) req: Request) {
super(dataSource, req);
this.repository = this.getRepository(Grade);
}

public async save(grade: Grade) {
await this.repository.save(grade);
}

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

public async update(newGrade: Grade) {
return await this.repository.update(
{ userId: newGrade.userId },
{
grade: newGrade.grade,
point: newGrade.point,
},
);
}

public async delete(id: string) {}
}
42 changes: 42 additions & 0 deletions src/grade/grade.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { GradeRepository } from './grade.repository';
import { Grade } from '../Entity/grade';

@Injectable()
export class GradeService {
constructor(private gradeRepository: GradeRepository) {}
public async gradeCreate(userId: string, grade: number) {
const isExist = await this.gradeRepository.findOne(userId);

if (isExist) {
throw new BadRequestException('이미 등록했습니다.');
}
const newGrade = new Grade();
newGrade.userId = userId;
newGrade.grade = grade;
newGrade.point = this.calculatePoint(grade);

await this.gradeRepository.save(newGrade);
}

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

if (!isExist) {
throw new BadRequestException('등록된 학점정보가 없습니다');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개인적인 생각으로는 NotFound 가 더 어울리지 않을까 생각이됩니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 오키오키 반영해서 올리겠읍니다

}

const newGrade = new Grade();
newGrade.userId = userId;
newGrade.grade = grade;
newGrade.point = this.calculatePoint(grade);

await this.gradeRepository.update(newGrade);
}

public async gradeDelete() {}

public calculatePoint(grade: number) {
return 0;
}
}
Loading