-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/badge 기능 구현 #56 #58
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
base: develop
Are you sure you want to change the base?
The head ref may contain hidden characters: "Feature/Badge_\uAE30\uB2A5_\uAD6C\uD604_#56"
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { | ||
| Body, | ||
| Controller, | ||
| Delete, | ||
| Get, | ||
| Post, | ||
| Req, | ||
| UseGuards, | ||
| } from '@nestjs/common'; | ||
| import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger'; | ||
| import { Request } from 'express'; | ||
| import { Badge } from 'src/entities/badge.entity'; | ||
| import { LoginAuthGuard } from 'src/services/auth/guard/login.guard'; | ||
| import { AccessPayload } from 'src/services/auth/payload/access.payload'; | ||
| import { BadgeService } from 'src/services/badge/badge.service'; | ||
| import { PostBadgeRequestDto } from 'src/services/badge/dto/post-badge.dto'; | ||
| import { GrantRequestBadgeDto } from 'src/services/badge/dto/post-grant.dto'; | ||
| import { UserService } from 'src/services/user/user.service'; | ||
|
|
||
| @Controller('badge') | ||
| @ApiTags('badge') | ||
| export class BadgeController { | ||
| constructor( | ||
| private readonly badgeService: BadgeService, | ||
| private readonly userService: UserService, | ||
| ) {} | ||
|
|
||
| @Post('/post') | ||
| @ApiOperation({ | ||
| summary: 'Create Badge', | ||
| description: 'Create Badge', | ||
| }) | ||
| @ApiCreatedResponse({ | ||
| description: 'Badge created', | ||
| }) | ||
| @UseGuards(LoginAuthGuard) | ||
| async createBadge( | ||
| @Req() req: Request, | ||
| @Body() postBadgeRequestDto: PostBadgeRequestDto, | ||
| ): Promise<void> { | ||
| const userId = (req.user as AccessPayload).userId; | ||
| if (await this.userService.checkBadgeForAuth(userId, 'admin')) { | ||
| await this.badgeService.createBadge( | ||
| postBadgeRequestDto.title, | ||
| postBadgeRequestDto.description, | ||
| postBadgeRequestDto.icon, | ||
| postBadgeRequestDto.key, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Post('/grant') | ||
| @ApiOperation({ | ||
| summary: 'Grant Badge', | ||
| description: 'Grant Badge', | ||
| }) | ||
| @ApiCreatedResponse({ | ||
| description: 'Badge granted', | ||
| }) | ||
| @UseGuards(LoginAuthGuard) | ||
| async grantBadge( | ||
| @Req() req: Request, | ||
| @Body() grantRequestBadgeDto: GrantRequestBadgeDto, | ||
| ): Promise<void> { | ||
| const userId = (req.user as AccessPayload).userId; | ||
| if (await this.userService.checkBadgeForAuth(userId, 'admin')) { | ||
| await this.badgeService.grantBadgeToUser( | ||
| grantRequestBadgeDto.badgeId, | ||
| grantRequestBadgeDto.userId, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Get('') | ||
| @ApiOperation({ | ||
| summary: 'Get Badges', | ||
| description: 'Get Badges', | ||
| }) | ||
| @ApiCreatedResponse({ | ||
| description: 'Badges', | ||
| }) | ||
| async getBadges(): Promise<Badge[]> { | ||
| return this.badgeService.getBadges(); | ||
| } | ||
|
|
||
| @Get('/:userId') | ||
| @ApiOperation({ | ||
| summary: 'Get Badges By UserId', | ||
| description: 'Get Badges By UserId', | ||
| }) | ||
| @ApiCreatedResponse({ | ||
| description: 'Badges', | ||
| }) | ||
| async getBadgesByUserId(userId: string): Promise<Badge[]> { | ||
| return this.badgeService.getBadgesByUserId(userId); | ||
| } | ||
|
Comment on lines
+92
to
+102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 유저 프로필 조호와 합칩시다 |
||
|
|
||
| @Delete('/:userId/:badgeId') | ||
| @ApiOperation({ | ||
| summary: 'Deprive Badge', | ||
| description: 'Deprive Badge', | ||
| }) | ||
| @ApiCreatedResponse({ | ||
| description: 'Badge deprived', | ||
| }) | ||
| @UseGuards(LoginAuthGuard) | ||
| async depriveBadge( | ||
| @Req() req: Request, | ||
| userId: string, | ||
| badgeId: string, | ||
| ): Promise<void> { | ||
| const adminId = (req.user as AccessPayload).userId; | ||
| if (await this.userService.checkBadgeForAuth(adminId, 'admin')) { | ||
| await this.badgeService.depriveBadgeFromUser(badgeId, userId); | ||
| } | ||
| } | ||
| // todo: badge delete를 하는 기능 해당 뱃지를 가진 user_badge table의 해당 뱃지를 가진 row를 삭제하는 기능이 필요 | ||
| // @Delete('/:badgeId') | ||
| // @ApiOperation({ | ||
| // summary: 'Delete Badge', | ||
| // description: 'Delete Badge', | ||
| // }) | ||
| // @ApiCreatedResponse({ | ||
| // description: 'Badge deleted', | ||
| // }) | ||
| // @UseGuards(LoginAuthGuard) | ||
| // async deleteBadge(@Req() req: Request, badgeId: string): Promise<void> { | ||
| // const userId = (req.user as AccessPayload).userId; | ||
| // if (await this.userService.checkBadgeForAuth(userId, 'admin')) { | ||
| // await this.badgeService.deleteBadge(badgeId); | ||
| // } | ||
| // } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { InjectRepository } from '@nestjs/typeorm'; | ||
| import ERROR from 'src/common/error'; | ||
| import { BadgeLog } from 'src/entities/badge-log.entity'; | ||
| import { Badge } from 'src/entities/badge.entity'; | ||
| import { File } from 'src/entities/file.entity'; | ||
| import { User } from 'src/entities/user.entity'; | ||
| import { Repository } from 'typeorm'; | ||
|
|
||
| @Injectable() | ||
| export class BadgeService { | ||
| constructor( | ||
| @InjectRepository(Badge) | ||
| private readonly badgeRepository: Repository<Badge>, | ||
| @InjectRepository(File) | ||
| private readonly fileRepository: Repository<File>, | ||
| @InjectRepository(User) | ||
| private readonly userRepository: Repository<User>, | ||
| @InjectRepository(BadgeLog) | ||
| private readonly badgeLogRepository: Repository<BadgeLog>, | ||
|
Comment on lines
+20
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 뱃지 로그 관련은 다 빼도 됩니다 |
||
| ) {} | ||
|
|
||
| async createBadge( | ||
| title: string, | ||
| description: string, | ||
| icon: string = null, | ||
| key: string, | ||
| ): Promise<Badge> { | ||
| const badge = new Badge(); | ||
| badge.title = title; | ||
| badge.description = description; | ||
| badge._key = key; | ||
| if (icon) { | ||
| const file = await this.fileRepository.findOne({ where: { id: icon } }); | ||
| badge.icon = file; | ||
| } else { | ||
| badge.icon = null; | ||
| } | ||
| return await this.badgeRepository.save(badge); | ||
| } | ||
|
|
||
| async getBadges(): Promise<Badge[]> { | ||
| return this.badgeRepository.find(); | ||
| } | ||
|
|
||
| async getBadgesByUserId(userId: string): Promise<Badge[]> { | ||
| const userWithBadges = await this.userRepository.findOne({ | ||
| where: { id: userId }, | ||
| relations: ['badges'], | ||
| }); | ||
|
|
||
| return userWithBadges.badges; | ||
| } | ||
|
|
||
| async grantBadgeToUser(badgeId: string, userId: string): Promise<BadgeLog> { | ||
| const badge = await this.badgeRepository.findOne({ | ||
| where: { id: badgeId }, | ||
| }); | ||
| const user = await this.userRepository.findOne({ | ||
| where: { id: userId }, | ||
| relations: ['badges'], | ||
| }); | ||
|
|
||
| user.badges = Array.isArray(user.badges) | ||
| ? [...user.badges, badge] | ||
| : [badge]; | ||
| await this.userRepository.save(user); | ||
|
|
||
| const badgeLog = new BadgeLog(); | ||
| badgeLog.badge = badge; | ||
| badgeLog.user = user; | ||
| badgeLog.isGiven = true; | ||
| return await this.badgeLogRepository.save(badgeLog); | ||
|
||
| } | ||
| // todo: badge delete를 하는 기능 해당 뱃지를 가진 user_badge table의 해당 뱃지를 가진 row를 삭제하는 기능이 필요 | ||
| // async deleteBadge(badgeId: string): Promise<void> { | ||
| // const badge = await this.badgeRepository.findOne({ | ||
| // where: { id: badgeId }, | ||
| // }); | ||
|
|
||
| // if (!badge) { | ||
| // throw ERROR.NOT_FOUND; | ||
| // } | ||
|
|
||
| // await this.badgeRepository.manager.connection | ||
| // .createQueryBuilder() | ||
| // .delete() | ||
| // .from('user_badge') | ||
| // .where('badgeId = :badgeId', { badgeId }) | ||
| // .execute(); | ||
|
|
||
| // await this.badgeRepository.delete({ id: badgeId }); | ||
| // } | ||
|
|
||
| async depriveBadgeFromUser(badgeId: string, userId: string): Promise<void> { | ||
|
||
| const user = await this.userRepository.findOne({ | ||
| where: { id: userId }, | ||
| }); | ||
|
|
||
| const badge = await this.badgeRepository.findOne({ | ||
| where: { id: badgeId }, | ||
| }); | ||
|
|
||
| if (!user || !badge) { | ||
| throw ERROR.NOT_FOUND; | ||
| } | ||
|
|
||
| await this.userRepository | ||
| .createQueryBuilder() | ||
| .relation(User, 'badges') | ||
| .of(user) | ||
| .remove(badge); | ||
|
Comment on lines
+130
to
+134
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 부여 안된 뱃지만 삭제하는걸로 합시다 |
||
|
|
||
| const badgeLog = new BadgeLog(); | ||
| badgeLog.badge = badge; | ||
| badgeLog.user = user; | ||
| badgeLog.isGiven = false; | ||
| await this.badgeLogRepository.save(badgeLog); | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export class PostBadgeRequestDto { | ||
| title: string; | ||
| description: string; | ||
| icon?: string; | ||
| key: string; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export class GrantRequestBadgeDto { | ||
| userId: string; | ||
| badgeId: string; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이거 형식을 export class GrantRequestBadgeDto {
userId:string;
grantedBadges:string[];
deprivedBadges:string[];
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이건 어드민 권한으로 해야할 듯 합니다