-
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
Open
kbs1027
wants to merge
5
commits into
develop
Choose a base branch
from
Feature/Badge_기능_구현_#56
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
The head ref may contain hidden characters: "Feature/Badge_\uAE30\uB2A5_\uAD6C\uD604_#56"
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import { | ||
| Body, | ||
| Controller, | ||
| Delete, | ||
| Get, | ||
| Param, | ||
| Post, | ||
| Query, | ||
| Req, | ||
| UseGuards, | ||
| } from '@nestjs/common'; | ||
| import { ApiCreatedResponse, ApiOperation, ApiTags } from '@nestjs/swagger'; | ||
| import { Request } from 'express'; | ||
| import { Badge } from 'src/entities/badge.entity'; | ||
| import { GetUsersResponseDto } from 'src/services/auth/dto/get-users.dto'; | ||
| 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<GetUsersResponseDto> { | ||
| const userId = (req.user as AccessPayload).userId; | ||
| if (await this.userService.checkBadgeForAuth(userId, 'admin')) { | ||
| return await this.badgeService.grantBadgeToUser( | ||
| grantRequestBadgeDto.badgeIds, | ||
| grantRequestBadgeDto.userId, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Get('') | ||
| @ApiOperation({ | ||
| summary: 'Get Badges', | ||
| description: 'Get Badges', | ||
| }) | ||
| @ApiCreatedResponse({ | ||
| description: 'Badges', | ||
| }) | ||
| async getBadges(@Req() req: Request): Promise<Badge[]> { | ||
| const userId = (req.user as AccessPayload).userId; | ||
| if (await this.userService.checkBadgeForAuth(userId, 'admin')) { | ||
| 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); | ||
| } | ||
|
|
||
| @Delete('/:userId') | ||
| @ApiOperation({ | ||
| summary: 'Deprive Badge', | ||
| description: 'Deprive Badge', | ||
| }) | ||
| @ApiCreatedResponse({ | ||
| description: 'Badge deprived', | ||
| }) | ||
| @UseGuards(LoginAuthGuard) | ||
| async depriveBadge( | ||
| @Req() req: Request, | ||
| @Param('userId') userId: string, | ||
| @Query('badgeIds') badgeIds: string[], | ||
| ): Promise<void> { | ||
| const adminId = (req.user as AccessPayload).userId; | ||
| if (await this.userService.checkBadgeForAuth(adminId, 'admin')) { | ||
| await this.badgeService.depriveBadgeFromUser(badgeIds, 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); | ||
| // } | ||
| // } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| 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 { GetUsersResponseDto } from 'src/services/auth/dto/get-users.dto'; | ||
| import { In, 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( | ||
| badgeIds: string[], | ||
| userId: string, | ||
| ): Promise<GetUsersResponseDto> { | ||
| const badges = await this.badgeRepository.find({ | ||
| where: { id: In(badgeIds) }, | ||
| }); | ||
|
|
||
| if (badges.length !== badgeIds.length) { | ||
| throw ERROR.NOT_FOUND; | ||
| } | ||
|
|
||
| const user = await this.userRepository.findOne({ | ||
| where: { id: userId }, | ||
| relations: ['badges'], | ||
| }); | ||
|
|
||
| if (!user) { | ||
| throw ERROR.NOT_FOUND; | ||
| } | ||
|
|
||
| user.badges = Array.isArray(user.badges) | ||
| ? [...user.badges, ...badges] | ||
| : badges; | ||
|
|
||
| const grantedUser = await this.userRepository.save(user); | ||
| return { | ||
| id: grantedUser.id, | ||
| username: grantedUser.username, | ||
| badges: grantedUser.badges, | ||
| email: grantedUser.email, | ||
| phoneNumber: grantedUser.phoneNumber, | ||
| studentId: grantedUser.studentId, | ||
| activation: grantedUser.activation, | ||
| createdAt: grantedUser.createdAt, | ||
| updatedAt: grantedUser.updatedAt, | ||
| }; | ||
| } | ||
| // 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( | ||
| badgeIds: string[], | ||
| userId: string, | ||
| ): Promise<void> { | ||
| const user = await this.userRepository.findOne({ | ||
| where: { id: userId }, | ||
| }); | ||
|
|
||
| const badge = await this.badgeRepository.find({ | ||
| where: { id: In(badgeIds) }, | ||
| }); | ||
|
|
||
| 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. 부여 안된 뱃지만 삭제하는걸로 합시다 |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export class GrantRequestBadgeDto { | ||
| userId: string; | ||
| badgeIds: string[]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
이 부분은 유저 프로필 조호와 합칩시다