Skip to content

Commit

Permalink
✨ Implement chunk loading for episodes (limit to 25 per chunks)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed May 18, 2024
1 parent 4371bab commit a0067e2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/modules/webtoon/webtoon/models/dto/chunk-number.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ApiProperty} from "@nestjs/swagger";
import {IsInt, IsOptional} from "class-validator";

export class ChunkNumberDto{
@ApiProperty({required: false})
@IsOptional()
@IsInt()
chunk: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import EpisodeLineModel from "../models/episode-line.model";
import {ApiProperty} from "@nestjs/swagger";

export default class EpisodeChunkResponse{
@ApiProperty()
episodes: EpisodeLineModel[];
@ApiProperty()
currentChunk: number;
@ApiProperty()
maxChunks: number;

constructor(
episodes: EpisodeLineModel[],
currentChunk: number,
maxChunks: number
){
this.episodes = episodes;
this.currentChunk = currentChunk;
this.maxChunks = maxChunks;
}
}
20 changes: 15 additions & 5 deletions src/modules/webtoon/webtoon/webtoon-database.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import {PrismaService} from "../../misc/prisma.service";
import {MiscService} from "../../misc/misc.service";
import ImageTypes from "./models/enums/image-types";
import WebtoonResponse from "./models/responses/webtoon-response";
import EpisodeChunkResponse from "./models/responses/episode-chunk.response";

@Injectable()
export class WebtoonDatabaseService{

private readonly CHUNK_SIZE: number = 25;

constructor(
private readonly prismaService: PrismaService,
private readonly miscService: MiscService
Expand Down Expand Up @@ -267,14 +270,19 @@ export class WebtoonDatabaseService{
);
}

async getEpisodes(webtoonId: number): Promise<EpisodeLineModel[]>{
async getEpisodes(webtoonId: number, chunkNumber: number): Promise<EpisodeChunkResponse>{
const dbWebtoon: any = await this.prismaService.webtoons.findFirst({
where: {
id: webtoonId
}
});
if(!dbWebtoon)
throw new NotFoundException(`Webtoon with id ${webtoonId} not found in database.`);
const episodeCount: number = await this.prismaService.episodes.count({
where: {
webtoon_id: webtoonId
}
});
const episodes: any[] = await this.prismaService.episodes.findMany({
where: {
webtoon_id: webtoonId
Expand All @@ -284,14 +292,16 @@ export class WebtoonDatabaseService{
},
orderBy: {
number: "asc"
}
},
skip: (chunkNumber - 1) * this.CHUNK_SIZE,
take: this.CHUNK_SIZE
});
const response: EpisodeLineModel[] = [];
const episodeLines: EpisodeLineModel[] = [];
for(const episode of episodes){
const thumbnail: string = this.miscService.bufferToDataURL(this.loadImage(episode.thumbnail.sum));
response.push(new EpisodeLineModel(episode.id, episode.title, episode.number, thumbnail));
episodeLines.push(new EpisodeLineModel(episode.id, episode.title, episode.number, thumbnail));
}
return response;
return new EpisodeChunkResponse(episodeLines, chunkNumber, Math.ceil(episodeCount / this.CHUNK_SIZE));
}

async getEpisodeInfos(episodeId: number): Promise<EpisodeResponse>{
Expand Down
12 changes: 7 additions & 5 deletions src/modules/webtoon/webtoon/webtoon.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Controller, Get, Param} from "@nestjs/common";
import {Controller, Get, Param, Query} from "@nestjs/common";
import {ApiResponse, ApiTags} from "@nestjs/swagger";
import {WebtoonDatabaseService} from "./webtoon-database.service";
import {WebtoonIdDto} from "./models/dto/webtoon-id.dto";
Expand All @@ -7,7 +7,8 @@ import EpisodeResponse from "./models/responses/episode.response";
import LightWebtoonResponse from "./models/responses/light-webtoon-response";
import {Throttle} from "@nestjs/throttler";
import WebtoonResponse from "./models/responses/webtoon-response";
import EpisodeLineModel from "./models/models/episode-line.model";
import EpisodeChunkResponse from "./models/responses/episode-chunk.response";
import {ChunkNumberDto} from "./models/dto/chunk-number.dto";

@Controller("webtoons")
@ApiTags("Webtoon")
Expand All @@ -32,10 +33,11 @@ export class WebtoonController{
}

@Get(":webtoonId/episodes")
@ApiResponse({status: 200, description: "Returns a list of episodes for a webtoon", type: EpisodeLineModel, isArray: true})
@ApiResponse({status: 200, description: "Returns a list of episodes for a webtoon", type: EpisodeChunkResponse})
@ApiResponse({status: 404, description: "Webtoon not found"})
async getWebtoonEpisodes(@Param() webtoonIdDto: WebtoonIdDto): Promise<EpisodeLineModel[]>{
return this.webtoonDatabaseService.getEpisodes(webtoonIdDto.webtoonId);
async getWebtoonEpisodes(@Param() webtoonIdDto: WebtoonIdDto, @Query() chunkNumberDto: ChunkNumberDto): Promise<EpisodeChunkResponse>{
const chunk = chunkNumberDto.chunk ?? 1;
return this.webtoonDatabaseService.getEpisodes(webtoonIdDto.webtoonId, chunk);
}

@Get("episodes/:episodeId")
Expand Down

0 comments on commit a0067e2

Please sign in to comment.