Skip to content

Commit

Permalink
feat(be): add api update manga
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Jul 10, 2023
1 parent ffdf604 commit 188c100
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/manga/dto/update-manga-request.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions src/manga/manga.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Param,
Post,
UploadedFiles,
UseGuards,
Expand All @@ -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 {
Expand All @@ -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<Express.Multer.File>,
) {
const { mangaId } = param;
return this.mangaSvc.update(mangaId, data, files);
}
}
70 changes: 70 additions & 0 deletions src/manga/manga.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -102,4 +103,73 @@ export class MangaService {

return updateResponse;
}

async update(
mangaId: number,
data: UpdateMangaRequestDto,
files: Array<Express.Multer.File>,
) {
const { token } = ContextProvider.getAuthUser();
const { status } = data;

const result = await this.graphqlSvc.query(
this.configSvc.get<string>('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<string>('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;
}
}

0 comments on commit 188c100

Please sign in to comment.