diff --git a/src/manga/dto/update-manga-request.dto.ts b/src/manga/dto/update-manga-request.dto.ts new file mode 100644 index 00000000..b438e73e --- /dev/null +++ b/src/manga/dto/update-manga-request.dto.ts @@ -0,0 +1,20 @@ +import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; +import { MangaStatus } from '../../common/enum'; +import { IsNumber } from 'class-validator'; + +export class UpdateMangaRequestDto { + @ApiProperty({ enum: MangaStatus, enumName: 'MangaStatus' }) + status: MangaStatus; + + @ApiPropertyOptional({ type: 'string', format: 'binary' }) + banner: Express.Multer.File; + + @ApiPropertyOptional({ type: 'string', format: 'binary' }) + poster: Express.Multer.File; +} + +export class UpdateMangaParamDto { + @ApiProperty() + @IsNumber() + mangaId: number; +} diff --git a/src/manga/manga.controller.ts b/src/manga/manga.controller.ts index d25a41ac..db6757f4 100644 --- a/src/manga/manga.controller.ts +++ b/src/manga/manga.controller.ts @@ -1,6 +1,7 @@ import { Body, Controller, + Param, Post, UploadedFiles, UseGuards, @@ -12,6 +13,10 @@ import { ApiBearerAuth, ApiConsumes } from '@nestjs/swagger'; import { AuthUserInterceptor } from '../interceptors/auth-user-interceptor.service'; import { AnyFilesInterceptor } from '@nestjs/platform-express'; import { CreateMangaRequestDto } from './dto/create-manga-request.dto'; +import { + UpdateMangaParamDto, + UpdateMangaRequestDto, +} from './dto/update-manga-request.dto'; @Controller('manga') export class MangaController { @@ -29,4 +34,18 @@ export class MangaController { // console.log(data); return this.mangaSvc.create(data, files); } + + @UseGuards(AuthGuard) + @ApiBearerAuth() + @Post(':mangaId') + @ApiConsumes('multipart/form-data') + @UseInterceptors(AuthUserInterceptor, AnyFilesInterceptor()) + update( + @Param() param: UpdateMangaParamDto, + @Body() data: UpdateMangaRequestDto, + @UploadedFiles() files: Array, + ) { + const { mangaId } = param; + return this.mangaSvc.update(mangaId, data, files); + } } diff --git a/src/manga/manga.service.ts b/src/manga/manga.service.ts index 10d5348a..1329578a 100644 --- a/src/manga/manga.service.ts +++ b/src/manga/manga.service.ts @@ -10,6 +10,7 @@ import { import { ContextProvider } from '../providers/contex.provider'; import { FilesService } from '../files/files.service'; import { GraphqlService } from '../graphql/graphql.service'; +import { UpdateMangaRequestDto } from './dto/update-manga-request.dto'; @Injectable() export class MangaService { @@ -102,4 +103,73 @@ export class MangaService { return updateResponse; } + + async update( + mangaId: number, + data: UpdateMangaRequestDto, + files: Array, + ) { + const { token } = ContextProvider.getAuthUser(); + const { status } = data; + + const result = await this.graphqlSvc.query( + this.configSvc.get('graphql.endpoint'), + token, + `query QueryMangaById($id: Int = 10) { + manga_by_pk(id: $id) { + id + poster + banner + } + }`, + 'QueryMangaById', + { + id: mangaId, + }, + ); + + if (result.errors && result.errors.length > 0) { + return result; + } + + if (result.data.manga_by_pk === null) { + return result.data; + } + + let { poster: posterUrl, banner: bannerUrl } = result.data.manga_by_pk; + + // upload files + const bannerFile = files.filter((f) => f.fieldname === 'banner')[0]; + if (bannerFile) + bannerUrl = await this.filesService.uploadImageToS3(mangaId, bannerFile); + + const posterFile = files.filter((f) => f.fieldname === 'poster')[0]; + if (posterFile) + posterUrl = await this.filesService.uploadImageToS3(mangaId, posterFile); + + // update manga in DB + const udpateVariables = { + id: mangaId, + banner: bannerUrl, + poster: posterUrl, + status, + }; + const updateResponse = await this.graphqlSvc.query( + this.configSvc.get('graphql.endpoint'), + token, + `mutation UpdateMangaByPK($banner: String = "", $poster: String = "", $id: Int = 10, $status: String = "") { + update_manga_by_pk(pk_columns: {id: $id}, _set: {banner: $banner, poster: $poster, status: $status}) { + id + banner + poster + status + created_at + } + }`, + 'UpdateMangaByPK', + udpateVariables, + ); + + return updateResponse; + } }