Skip to content

Commit

Permalink
feat(be): view protected chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Sep 7, 2023
1 parent 3ac6f3a commit 71f1143
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/chapter/chapter.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {
Body,
Controller,
Ip,
Get,
Param,
Patch,
Post,
Put,
UploadedFile,
Expand All @@ -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')
Expand Down Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/chapter/chapter.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
})
Expand Down
54 changes: 54 additions & 0 deletions src/chapter/chapter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<string>('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,
Expand Down
8 changes: 8 additions & 0 deletions src/chapter/dto/view-chapter-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNumber } from 'class-validator';

export class ViewProtectedChapterRequestDto {
@ApiProperty()
@IsNumber()
chapterId: number;
}
1 change: 1 addition & 0 deletions src/manga/manga.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ import { GraphqlModule } from '../graphql/graphql.module';
imports: [JwtModule, FilesModule, GraphqlModule],
providers: [MangaService],
controllers: [MangaController],
exports: [MangaService],
})
export class MangaModule {}

0 comments on commit 71f1143

Please sign in to comment.