diff --git a/src/modules/webtoon/update/update.controller.ts b/src/modules/webtoon/update/update.controller.ts index 78dfcac..87439dc 100644 --- a/src/modules/webtoon/update/update.controller.ts +++ b/src/modules/webtoon/update/update.controller.ts @@ -1,4 +1,4 @@ -import {Controller, Post, UseGuards} from "@nestjs/common"; +import {Controller, Logger, Post, UseGuards} from "@nestjs/common"; import {UpdateService} from "./update.service"; import {ApiBearerAuth, ApiTags} from "@nestjs/swagger"; import {AdminGuard} from "../admin/guard/admin.guard"; @@ -8,6 +8,9 @@ import {AdminGuard} from "../admin/guard/admin.guard"; @ApiTags("Update") @UseGuards(AdminGuard) export class UpdateController{ + + private readonly logger = new Logger(UpdateController.name); + constructor( private readonly updateService: UpdateService, ){} @@ -15,6 +18,6 @@ export class UpdateController{ @Post("webtoons/thumbnails") @ApiBearerAuth() async updateThumbnails(): Promise{ - await this.updateService.updateThumbnails(); + this.updateService.updateThumbnails().then(() => this.logger.log("Thumbnails updated")); } } diff --git a/src/modules/webtoon/update/update.service.ts b/src/modules/webtoon/update/update.service.ts index 78dc48e..f2c6cce 100644 --- a/src/modules/webtoon/update/update.service.ts +++ b/src/modules/webtoon/update/update.service.ts @@ -1,4 +1,4 @@ -import {Injectable} from "@nestjs/common"; +import {Injectable, Logger} from "@nestjs/common"; import {PrismaService} from "../../misc/prisma.service"; import {WebtoonParserService} from "../webtoon/webtoon-parser.service"; import CachedWebtoonModel from "../webtoon/models/models/cached-webtoon.model"; @@ -10,6 +10,8 @@ import ImageTypes from "../webtoon/models/enums/image-types"; @Injectable() export class UpdateService{ + private readonly logger = new Logger(UpdateService.name); + constructor( private readonly prismaService: PrismaService, private readonly webtoonParser: WebtoonParserService, @@ -20,53 +22,65 @@ export class UpdateService{ async updateThumbnails(): Promise{ this.webtoonParser.clearCache(); await this.webtoonParser.loadCache(); - const dbWebtoons: any[] = await this.prismaService.webtoons.findMany({ - select: { - id: true, - title: true, - language: true, - thumbnail_id: true - } - }); - const thumbnailsToDelete: number[] = dbWebtoons.map(webtoon => webtoon.thumbnail_id); - // Save new thumbnails - const dbThumbnailType = await this.prismaService.imageTypes.findFirst({ - where: { - name: ImageTypes.WEBTOON_THUMBNAIL - }, - select: { - id: true - } - }); - for(const webtoon of dbWebtoons){ - const cachedWebtoon: CachedWebtoonModel = this.webtoonParser.findWebtoon(webtoon.title, webtoon.language); - const thumbnail: Buffer = await this.miscService.convertThumbnail(cachedWebtoon.thumbnail); - const sum: string = this.webtoonDatabaseService.saveImage(thumbnail); - const dbThumbnail = await this.prismaService.images.create({ - data: { - sum: sum, - type_id: dbThumbnailType.id - } - }); - await this.prismaService.webtoons.update({ - where: { - id: webtoon.id - }, - data: { - thumbnail_id: dbThumbnail.id + await this.prismaService.$transaction(async(tx) => { + const dbWebtoons: any[] = await tx.webtoons.findMany({ + select: { + id: true, + title: true, + language: true, + thumbnail_id: true } }); - } + const thumbnailsToDelete: number[] = dbWebtoons.map(webtoon => webtoon.thumbnail_id); - // Delete old thumbnails - for(const thumbnailId of thumbnailsToDelete){ - const deletedImage = await this.prismaService.images.delete({ + // Save new thumbnails + const dbThumbnailType = await tx.imageTypes.findFirst({ where: { - id: thumbnailId + name: ImageTypes.WEBTOON_THUMBNAIL + }, + select: { + id: true } }); - this.webtoonDatabaseService.removeImage(deletedImage.sum); - } + for(const webtoon of dbWebtoons){ + this.logger.debug(`Updating thumbnail for webtoon ${webtoon.title} (${webtoon.language})`); + const cachedWebtoon: CachedWebtoonModel = this.webtoonParser.findWebtoon(webtoon.title, webtoon.language); + const thumbnail: Buffer = await this.miscService.convertThumbnail(cachedWebtoon.thumbnail); + const sum: string = this.webtoonDatabaseService.saveImage(thumbnail); + // Check if thumbnail already exists + let dbThumbnail = await tx.images.findFirst({ + where: { + sum: sum + } + }); + if(dbThumbnail) + thumbnailsToDelete.splice(thumbnailsToDelete.indexOf(dbThumbnail.id), 1); + else + dbThumbnail = await tx.images.create({ + data: { + sum: sum, + type_id: dbThumbnailType.id + } + }); + // Update webtoon thumbnail + await tx.webtoons.update({ + where: { + id: webtoon.id + }, + data: { + thumbnail_id: dbThumbnail.id + } + }); + } + // Delete old thumbnails + for(const thumbnailId of thumbnailsToDelete){ + const deletedImage = await tx.images.delete({ + where: { + id: thumbnailId + } + }); + this.webtoonDatabaseService.removeImage(deletedImage.sum); + } + }); } - }