-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 북마크 생성 시 문제가 이미 들어 있다면 update - 없으면 insert 하도록 구현 - 북마크 조회 시 해당 유저의 북마크 리스트를 반환 - 북마크 삭제 시 해당 북마크를 삭제 - 북마크 생성, 조회, 삭제 시 유저 정보를 받아오는 pipe 구현 - 북마크 생성 dto 구현 Related to #42
- Loading branch information
Showing
5 changed files
with
144 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,4 +1,52 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { BookmarkWithHistory } from './dto/post-bookmark-body.dto'; | ||
|
||
@Injectable() | ||
export class BookmarkService {} | ||
export class BookmarkService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async upsertBookmark(userId: string, bookmark: BookmarkWithHistory) { | ||
const where = { | ||
user_id: userId, | ||
bookmark_question_id: bookmark.question_id, | ||
}; | ||
const Bookmarked = await this.prisma.bookmark.findFirst({ | ||
select: { id: true }, | ||
where, | ||
}); | ||
|
||
if (!Bookmarked) { | ||
return this.prisma.bookmark.create({ | ||
data: { | ||
history: bookmark.history, | ||
...where, | ||
}, | ||
}); | ||
} else { | ||
return this.prisma.bookmark.update({ | ||
where: { id: Bookmarked.id }, | ||
data: { | ||
history: bookmark.history, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
findAllBookmark(userId: string) { | ||
return this.prisma.bookmark.findMany({ | ||
where: { | ||
user_id: userId, | ||
}, | ||
}); | ||
} | ||
|
||
deleteBookmark(userId: string, questionId: string) { | ||
return this.prisma.bookmark.deleteMany({ | ||
where: { | ||
user_id: userId, | ||
bookmark_question_id: questionId, | ||
}, | ||
}); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/backend/src/bookmark/dto/post-bookmark-body.dto.ts
This file contains 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,40 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Type } from 'class-transformer'; | ||
import { | ||
IsArray, | ||
IsNotEmpty, | ||
IsOptional, | ||
IsString, | ||
IsUUID, | ||
ValidateNested, | ||
} from 'class-validator'; | ||
|
||
export class PostBookmarkReqBody { | ||
@ApiProperty({ | ||
description: '북마크할 문제의 id와 히스토리', | ||
isArray: true, | ||
type: () => BookmarkWithHistory, | ||
}) | ||
@IsArray() | ||
@ValidateNested({ each: true }) | ||
@Type(() => BookmarkWithHistory) | ||
bookmarks: BookmarkWithHistory[]; | ||
} | ||
|
||
export class BookmarkWithHistory { | ||
@ApiProperty({ | ||
description: '북마크할 문제의 id', | ||
}) | ||
@IsUUID() | ||
@IsNotEmpty() | ||
question_id: string; | ||
|
||
@ApiProperty({ | ||
description: '북마크 틀린문제 히스토리', | ||
required: false, | ||
default: '', | ||
}) | ||
@IsString() | ||
@IsOptional() | ||
history: string; | ||
} |
This file contains 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,21 @@ | ||
import { Injectable, PipeTransform } from '@nestjs/common'; | ||
import { AuthToken } from '../../auth/dto/req-auth-token.dto'; | ||
import { AuthService } from '../../auth/auth.service'; | ||
|
||
/** | ||
* Validator supports service container in the case if want to inject dependencies into your custom validator constraint classes | ||
* @see also https://github.com/leosuncin/nest-api-example/blob/master/src/blog/pipes/article.pipe.ts | ||
* @see also https://github.com/typestack/class-validator?tab=readme-ov-file#using-service-container | ||
* @see also https://docs.nestjs.com/techniques/validation | ||
*/ | ||
@Injectable() | ||
export class ParseUserIdPipe | ||
implements PipeTransform<AuthToken, Promise<string>> | ||
{ | ||
constructor(private readonly authService: AuthService) {} | ||
|
||
async transform(value: AuthToken) { | ||
const payload = this.authService.decodeToken(value.accessToken); | ||
return payload.sub; | ||
} | ||
} |