Skip to content

Commit

Permalink
feat(be): update manga api
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Jul 26, 2023
1 parent 82b7fe0 commit 0e3418f
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 7 deletions.
34 changes: 34 additions & 0 deletions hasura/metadata/query_collections.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,37 @@
}
}
}
- name: Admin - Update manga
query: |
mutation UpdateManga ($manga_id: Int!, $status: String!, $banner: String!, $poster: String!, $manga_languages: [manga_languages_insert_input!] = {language_id:10,is_main_language:false,description:"",title:""}, $manga_creators: [manga_creator_insert_input!] = {creator_id:10}, $manga_tags: [manga_tag_insert_input!] = {tag_id:10}, $release_date: timestamptz = "") {
delete_manga_tag(where: {manga_id:{_eq:$manga_id}}) {
affected_rows
}
delete_manga_creator(where: {manga_id:{_eq:$manga_id}}) {
affected_rows
}
delete_manga_languages(where: {manga_id:{_eq:$manga_id}}) {
affected_rows
}
insert_manga_one(object: {status:$status,manga_creators:{data:$manga_creators},banner:$banner,poster:$poster,manga_languages:{data:$manga_languages},manga_tags:{data:$manga_tags},id:$manga_id,release_date:$release_date}, on_conflict: {constraint:manga_pkey,update_columns:[banner,poster,status,release_date]}) {
id
banner
poster
status
release_date
created_at
status
manga_creators {
creator_id
}
manga_languages {
language_id
title
is_main_language
description
}
manga_tags {
tag_id
}
}
}
9 changes: 9 additions & 0 deletions hasura/metadata/rest_endpoints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@
- DELETE
name: Admin - Delete User
url: admin/users
- comment: ""
definition:
query:
collection_name: allowed-queries
query_name: Admin - Update manga
methods:
- PUT
name: Admin - Update manga
url: manga/:manga_id
- comment: ""
definition:
query:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "public"."manga" ALTER COLUMN "release_date" TYPE timestamp with time zone;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "public"."manga" ALTER COLUMN "release_date" TYPE date;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "public"."manga" ALTER COLUMN "release_date" drop default;
ALTER TABLE "public"."manga" ALTER COLUMN "release_date" TYPE date;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "public"."manga" ALTER COLUMN "release_date" TYPE timestamptz;
alter table "public"."manga" alter column "release_date" set default now();
42 changes: 41 additions & 1 deletion src/manga/dto/update-manga-request.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsBoolean, IsNumber } from 'class-validator';
import { MangaStatus } from '../../common/enum';
import { IsNumber } from 'class-validator';

export class MangaTag {
@ApiProperty()
@IsNumber()
tag_id: number;
}

export class MangaCreator {
@ApiProperty()
@IsNumber()
creator_id: number;
}

export class MangaLanguage {
@ApiProperty()
title: string;

@ApiProperty()
@IsNumber()
language_id: number;

@ApiProperty()
@IsBoolean()
is_main_language: number;

@ApiProperty()
description: string;
}

export class UpdateMangaRequestDto {
@ApiProperty({ enum: MangaStatus, enumName: 'MangaStatus' })
Expand All @@ -11,6 +39,18 @@ export class UpdateMangaRequestDto {

@ApiPropertyOptional({ type: 'string', format: 'binary' })
poster: Express.Multer.File;

@ApiProperty()
release_date: string;

@ApiProperty({ type: [MangaTag] })
manga_tags: string;

@ApiProperty({ type: MangaCreator, isArray: true })
manga_creators: string;

@ApiProperty({ type: [MangaLanguage] })
manga_languages: string;
}

export class UpdateMangaParamDto {
Expand Down
49 changes: 43 additions & 6 deletions src/manga/manga.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ export class MangaService {
files: Array<Express.Multer.File>,
) {
const { token } = ContextProvider.getAuthUser();
const { status } = data;
const {
status,
release_date,
manga_tags,
manga_creators,
manga_languages,
} = data;

const result = await this.graphqlSvc.query(
this.configSvc.get<string>('graphql.endpoint'),
Expand Down Expand Up @@ -159,24 +165,55 @@ export class MangaService {

// update manga in DB
const udpateVariables = {
id: mangaId,
manga_id: mangaId,
banner: bannerUrl,
poster: posterUrl,
status,
release_date,
manga_tags: plainToInstance(MangaTag, JSON.parse(manga_tags)),
manga_creators: plainToInstance(MangaCreator, JSON.parse(manga_creators)),
manga_languages: plainToInstance(
MangaLanguage,
JSON.parse(manga_languages),
),
};
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}) {
`mutation UpdateManga($manga_id: Int!, $status: String!, $banner: String!, $poster: String!, $manga_languages: [manga_languages_insert_input!] = {language_id: 10, is_main_language: false, description: "", title: ""}, $manga_creators: [manga_creator_insert_input!] = {creator_id: 10}, $manga_tags: [manga_tag_insert_input!] = {tag_id: 10}, $release_date: timestamptz = "") {
delete_manga_tag(where: {manga_id: {_eq: $manga_id}}) {
affected_rows
}
delete_manga_creator(where: {manga_id: {_eq: $manga_id}}) {
affected_rows
}
delete_manga_languages(where: {manga_id: {_eq: $manga_id}}) {
affected_rows
}
insert_manga_one(object: {status: $status, manga_creators: {data: $manga_creators}, banner: $banner, poster: $poster, manga_languages: {data: $manga_languages}, manga_tags: {data: $manga_tags}, id: $manga_id, release_date: $release_date}, on_conflict: {constraint: manga_pkey, update_columns: [banner, poster, status, release_date]}) {
id
banner
poster
status
release_date
created_at
status
manga_creators {
creator_id
}
manga_languages {
language_id
title
is_main_language
description
}
manga_tags {
tag_id
}
}
}`,
'UpdateMangaByPK',
}
`,
'UpdateManga',
udpateVariables,
);

Expand Down

0 comments on commit 0e3418f

Please sign in to comment.