Skip to content

Commit

Permalink
feat: update & delete creator artwork
Browse files Browse the repository at this point in the history
  • Loading branch information
harisato committed Aug 16, 2024
1 parent 8da16f0 commit 3af35e0
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 3 deletions.
36 changes: 35 additions & 1 deletion src/modules/artwork/artwork.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Expand All @@ -11,7 +12,12 @@ import {
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { ApiBearerAuth, ApiConsumes, ApiTags } from '@nestjs/swagger';
import {
ApiBearerAuth,
ApiConsumes,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';

import { AuthGuard } from '../../auth/auth.guard';
import { Role } from '../../auth/role.enum';
Expand All @@ -22,6 +28,11 @@ import { FileInterceptor } from '@nestjs/platform-express';
import { ArtworkService } from './artwork.service';
import { SetRequestTimeout } from '../../decorators/set-timeout.decorator';
import { ImportArtworkDto } from './dto/import-artwork.dto';
import {
UpdateArtworkDto,
UpdateArtworkParamDto,
} from './dto/update-artwork.dto';
import { DeleteArtworksDto } from './dto/delete-artworks.dto';

@Controller('artwork')
@ApiTags('artwork')
Expand All @@ -41,4 +52,27 @@ export class ArtworkController {
) {
return this.artworkSvc.import(body, file);
}

@UseGuards(AuthGuard, RolesGuard)
@ApiBearerAuth()
@Roles(Role.Creator)
@Put(':id')
@ApiOperation({ summary: 'update artwork - creator role' })
@UseInterceptors(AuthUserInterceptor)
update(
@Param() param: UpdateArtworkParamDto,
@Body() body: UpdateArtworkDto
) {
return this.artworkSvc.update(param.id, body);
}

@UseGuards(AuthGuard, RolesGuard)
@ApiBearerAuth()
@Roles(Role.Creator)
@Delete()
@ApiOperation({ summary: 'delete artworks - creator role' })
@UseInterceptors(AuthUserInterceptor)
delete(@Body() body: DeleteArtworksDto) {
return this.artworkSvc.deleteArtworks(body);
}
}
42 changes: 42 additions & 0 deletions src/modules/artwork/artwork.graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,48 @@ export class ArtworkGraphql {
);
}

async updateArtwork(variables: any) {
const headers = {
'x-hasura-admin-secret': this.configService.get<string>(
'graphql.adminSecret'
),
};

return this.graphqlSvc.query(
this.configService.get<string>('graphql.endpoint'),
'',
`mutation update_artworks($id: Int!, $creator_id: Int!, $data: artworks_set_input = {}) {
update_artworks(where: {id: {_eq: $id}, creator_id: {_eq: $creator_id}}, _set: $data) {
affected_rows
}
}`,
'update_artworks',
variables,
headers
);
}

async deleteArtworks(variables: any) {
const headers = {
'x-hasura-admin-secret': this.configService.get<string>(
'graphql.adminSecret'
),
};

return this.graphqlSvc.query(
this.configService.get<string>('graphql.endpoint'),
'',
`mutation delete_artworks($ids: [Int!], $creator_id: Int!) {
delete_artworks(where: {id: {_in: $ids}, creator_id: {_eq: $creator_id}}) {
affected_rows
}
}`,
'delete_artworks',
variables,
headers
);
}

async getI18n(variables: any, token: string) {
const result = await this.graphqlSvc.query(
this.configService.get<string>('graphql.endpoint'),
Expand Down
3 changes: 2 additions & 1 deletion src/modules/artwork/artwork.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { FilesModule } from '../files/files.module';
import { ArtworkService } from './artwork.service';
import { ArtworkController } from './artwork.controller';
import { ArtworkGraphql } from './artwork.graphql';
import { CreatorModule } from '../creator/creator.module';

@Module({
imports: [JwtModule, GraphqlModule, FilesModule],
imports: [JwtModule, GraphqlModule, FilesModule, CreatorModule],
providers: [ArtworkService, ArtworkGraphql],
controllers: [ArtworkController],
})
Expand Down
25 changes: 24 additions & 1 deletion src/modules/artwork/artwork.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { FilesService } from '../files/files.service';
import { ImportArtworkDto } from './dto/import-artwork.dto';
import { ArtworkGraphql } from './artwork.graphql';
import { generateSlug } from '../manga/util';
import { UpdateArtworkDto } from './dto/update-artwork.dto';
import { CreatorService } from '../creator/creator.service';
import { DeleteArtworksDto } from './dto/delete-artworks.dto';

@Injectable()
export class ArtworkService implements OnModuleInit {
Expand All @@ -25,7 +28,8 @@ export class ArtworkService implements OnModuleInit {
constructor(
private configService: ConfigService,
private fileService: FilesService,
private artworkGraphql: ArtworkGraphql
private artworkGraphql: ArtworkGraphql,
private creatorService: CreatorService
) {}

onModuleInit() {
Expand Down Expand Up @@ -78,6 +82,25 @@ export class ArtworkService implements OnModuleInit {
return creatorArtworks;
}

async update(id: number, data: UpdateArtworkDto) {
const creatorId = await this.creatorService.getCreatorIdAuthToken();
return this.artworkGraphql.updateArtwork({
id,
creator_id: creatorId,
data: {
name: data.name,
},
});
}

async deleteArtworks(data: DeleteArtworksDto) {
const creatorId = await this.creatorService.getCreatorIdAuthToken();
return this.artworkGraphql.deleteArtworks({
ids: data.ids,
creator_id: creatorId,
});
}

private async importProcess(
token: string,
contest_id: number,
Expand Down
6 changes: 6 additions & 0 deletions src/modules/artwork/dto/delete-artworks.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class DeleteArtworksDto {
@ApiProperty({ type: [String] })
ids: number[];
}
11 changes: 11 additions & 0 deletions src/modules/artwork/dto/update-artwork.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';

export class UpdateArtworkParamDto {
@ApiProperty()
id: number;
}

export class UpdateArtworkDto {
@ApiProperty()
name: string;
}

0 comments on commit 3af35e0

Please sign in to comment.