From 71f1143c9adc96601f1b257ad4bc98d2b1edbd45 Mon Sep 17 00:00:00 2001 From: harisato Date: Thu, 7 Sep 2023 15:04:30 +0700 Subject: [PATCH] feat(be): view protected chapter --- src/chapter/chapter.controller.ts | 13 ++++- src/chapter/chapter.module.ts | 3 +- src/chapter/chapter.service.ts | 54 +++++++++++++++++++++ src/chapter/dto/view-chapter-request.dto.ts | 8 +++ src/manga/manga.module.ts | 1 + 5 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/chapter/dto/view-chapter-request.dto.ts diff --git a/src/chapter/chapter.controller.ts b/src/chapter/chapter.controller.ts index 5ed5752f..022c71f4 100644 --- a/src/chapter/chapter.controller.ts +++ b/src/chapter/chapter.controller.ts @@ -1,9 +1,8 @@ import { Body, Controller, - Ip, + Get, Param, - Patch, Post, Put, UploadedFile, @@ -26,6 +25,7 @@ import { Roles } from '../auth/roles.decorator'; import { RolesGuard } from '../auth/role.guard'; import { SetRequestTimeout } from '../decorators/set-timeout.decorator'; import { UploadInputDto } from './dto/upload.dto'; +import { ViewProtectedChapterRequestDto } from './dto/view-chapter-request.dto'; @Controller('chapter') @ApiTags('chapter') @@ -73,4 +73,13 @@ export class ChapterController { ) { return this.chapterSvc.update(param, data, files); } + + @UseGuards(AuthGuard, RolesGuard) + @ApiBearerAuth() + @Roles(Role.User) + @Get(':chapterId') + @UseInterceptors(AuthUserInterceptor) + view(@Param() data: ViewProtectedChapterRequestDto) { + return this.chapterSvc.view(data); + } } diff --git a/src/chapter/chapter.module.ts b/src/chapter/chapter.module.ts index 037443f6..f3bc5d6f 100644 --- a/src/chapter/chapter.module.ts +++ b/src/chapter/chapter.module.ts @@ -4,9 +4,10 @@ import { ChapterService } from './chapter.service'; import { ChapterController } from './chapter.controller'; import { GraphqlModule } from '../graphql/graphql.module'; import { FilesModule } from '../files/files.module'; +import { MangaModule } from '../manga/manga.module'; @Module({ - imports: [JwtModule, GraphqlModule, FilesModule], + imports: [JwtModule, GraphqlModule, FilesModule, MangaModule], providers: [ChapterService], controllers: [ChapterController], }) diff --git a/src/chapter/chapter.service.ts b/src/chapter/chapter.service.ts index 59b7adc1..f1dd2d17 100644 --- a/src/chapter/chapter.service.ts +++ b/src/chapter/chapter.service.ts @@ -28,6 +28,8 @@ import { UpdateChapterRequestDto, } from './dto/update-chapter-request.dto'; import { UploadInputDto } from './dto/upload.dto'; +import { MangaService } from '../manga/manga.service'; +import { ViewProtectedChapterRequestDto } from './dto/view-chapter-request.dto'; @Injectable() export class ChapterService { @@ -37,6 +39,7 @@ export class ChapterService { private configService: ConfigService, private graphqlSvc: GraphqlService, private filesService: FilesService, + private mangaService: MangaService, ) {} async upload(data: UploadInputDto, file: Express.Multer.File) { @@ -361,6 +364,57 @@ export class ChapterService { } } + async view(data: ViewProtectedChapterRequestDto) { + try { + const { token } = ContextProvider.getAuthUser(); + + const { chapterId } = data; + + // insert chapter to DB + const result = await this.graphqlSvc.query( + this.configService.get('graphql.endpoint'), + token, + `query GetMangaIdByChapterId($id: Int = 10) { + chapters(where: {id: {_eq: $id}}) { + manga_id + chapter_type + chapter_languages(where: {chapter: {status: {_eq: "Published"}}}) { + language_id + detail + } + } + }`, + 'GetMangaIdByChapterId', + { + id: chapterId, + }, + ); + + if (result.errors && result.errors.length > 0) { + return result; + } + + if (result.data.chapters[0].chapter_type === 'NFTs only') { + const access = await this.mangaService.getAccess( + result.data.chapters[0].manga_id, + ); + + this.logger.debug(`Access ${JSON.stringify(access)}`); + + if (!access.nft || access.nft !== true) { + result.data.chapters[0].chapter_languages = []; + } + } + + return result; + } catch (errors) { + this.logger.error(errors); + return { + errors, + }; + } + } + async insertChapterLanguages( token: string, chapterId: number, diff --git a/src/chapter/dto/view-chapter-request.dto.ts b/src/chapter/dto/view-chapter-request.dto.ts new file mode 100644 index 00000000..6382731f --- /dev/null +++ b/src/chapter/dto/view-chapter-request.dto.ts @@ -0,0 +1,8 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsNumber } from 'class-validator'; + +export class ViewProtectedChapterRequestDto { + @ApiProperty() + @IsNumber() + chapterId: number; +} diff --git a/src/manga/manga.module.ts b/src/manga/manga.module.ts index 97a847e1..4150977e 100644 --- a/src/manga/manga.module.ts +++ b/src/manga/manga.module.ts @@ -9,5 +9,6 @@ import { GraphqlModule } from '../graphql/graphql.module'; imports: [JwtModule, FilesModule, GraphqlModule], providers: [MangaService], controllers: [MangaController], + exports: [MangaService], }) export class MangaModule {}