From 22b0885721cc893b3ad132c875e545d7f3dcc5fa Mon Sep 17 00:00:00 2001 From: Zibbp Date: Tue, 8 Mar 2022 19:50:11 +0000 Subject: [PATCH] Manual channel creation --- src/channels/channels.controller.ts | 25 +++++++--- src/channels/channels.service.ts | 48 +++++++++++++++++-- src/channels/dto/manual-create-channel.dto.ts | 15 ++++++ src/channels/entities/channel.entity.ts | 2 +- src/queues/entities/queue.entity.ts | 2 +- src/vods/entities/vod.entity.ts | 2 +- 6 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 src/channels/dto/manual-create-channel.dto.ts diff --git a/src/channels/channels.controller.ts b/src/channels/channels.controller.ts index e2d8ce8..39a4765 100755 --- a/src/channels/channels.controller.ts +++ b/src/channels/channels.controller.ts @@ -16,10 +16,11 @@ import { GetUser } from 'src/auth/get-user.decorator'; import { User, UserRole } from 'src/users/entities/user.entity'; import { Roles } from 'src/auth/role.decorator'; import { RolesGuard } from 'src/auth/roles.guard'; +import { ManualCreateChannelDto } from './dto/manual-create-channel.dto'; @Controller({ path: 'channels', version: '1' }) export class ChannelsController { - constructor(private readonly channelsService: ChannelsService) {} + constructor(private readonly channelsService: ChannelsService) { } @Post() @UseGuards(AuthGuard(), RolesGuard) @@ -38,13 +39,25 @@ export class ChannelsController { return this.channelsService.findOne(id); } - @Patch(':id') - update(@Param('id') id: string, @Body() updateChannelDto: UpdateChannelDto) { - return this.channelsService.update(+id, updateChannelDto); - } + // @Patch(':id') + // update(@Param('id') id: string, @Body() updateChannelDto: UpdateChannelDto) { + // return this.channelsService.update(+id, updateChannelDto); + // } @Delete(':id') + @UseGuards(AuthGuard(), RolesGuard) + @Roles(UserRole.ADMIN) remove(@Param('id') id: string) { - return this.channelsService.remove(+id); + return this.channelsService.remove(id); + } + + @Post('/manual') + @UseGuards(AuthGuard(), RolesGuard) + @Roles(UserRole.ADMIN) + createManual( + @Body() manualCreateChannelDto: ManualCreateChannelDto, + @GetUser() user: User, + ) { + return this.channelsService.createManual(manualCreateChannelDto); } } diff --git a/src/channels/channels.service.ts b/src/channels/channels.service.ts index dfa460b..e5d7e89 100755 --- a/src/channels/channels.service.ts +++ b/src/channels/channels.service.ts @@ -1,7 +1,9 @@ import { + ConflictException, HttpException, HttpStatus, Injectable, + InternalServerErrorException, Logger, NotFoundException, } from '@nestjs/common'; @@ -13,6 +15,8 @@ import { ChannelsRepository } from './channels.repository'; import { CreateChannelDto } from './dto/create-channel.dto'; import { InternalCreateChannelDto } from './dto/internal-create-channel.dto'; import { UpdateChannelDto } from './dto/update-channel.dto'; +import { ManualCreateChannelDto } from './dto/manual-create-channel.dto'; +import { User } from 'src/users/entities/user.entity'; @Injectable() export class ChannelsService { @@ -22,7 +26,7 @@ export class ChannelsService { private channelsRepository: ChannelsRepository, private twitchService: TwitchService, private filesService: FilesService, - ) {} + ) { } async create(createChannelDto: CreateChannelDto) { const { username } = createChannelDto; @@ -101,11 +105,45 @@ export class ChannelsService { return channel; } - update(id: number, updateChannelDto: UpdateChannelDto) { - return `This action updates a #${id} channel`; + // update(id: number, updateChannelDto: UpdateChannelDto) { + // return `This action updates a #${id} channel`; + // } + + async createManual(manualCreateChannelDto: ManualCreateChannelDto) { + const checkChannelId = await this.channelsRepository.findOne( + manualCreateChannelDto.id, + ); + if (checkChannelId) { + throw new ConflictException( + 'Channel exists with supplied id', + manualCreateChannelDto.id, + ); + } + + try { + const d = new Date(); + const channel = await this.channelsRepository.create({ + id: manualCreateChannelDto.id, + login: manualCreateChannelDto.login, + displayName: manualCreateChannelDto.displayName, + profileImagePath: manualCreateChannelDto.profileImagePath, + createdAt: d, + }); + await this.channelsRepository.save(channel); + return channel; + } catch (error) { + this.logger.error('Error creating channel', error); + throw new InternalServerErrorException('Error creating channel'); + } } - remove(id: number) { - return `This action removes a #${id} channel`; + async remove(id: string) { + try { + await this.channelsRepository.delete(id); + return 'deleted'; + } catch (error) { + this.logger.error('Error deleting channel', error); + throw new InternalServerErrorException('Error deleting channel'); + } } } diff --git a/src/channels/dto/manual-create-channel.dto.ts b/src/channels/dto/manual-create-channel.dto.ts new file mode 100644 index 0000000..1af57f1 --- /dev/null +++ b/src/channels/dto/manual-create-channel.dto.ts @@ -0,0 +1,15 @@ +import { IsDate, IsString } from 'class-validator'; + +export class ManualCreateChannelDto { + @IsString() + id: string; + + @IsString() + login: string; + + @IsString() + displayName: string; + + @IsString() + profileImagePath: string; +} diff --git a/src/channels/entities/channel.entity.ts b/src/channels/entities/channel.entity.ts index 9b1ec90..317e4aa 100755 --- a/src/channels/entities/channel.entity.ts +++ b/src/channels/entities/channel.entity.ts @@ -14,7 +14,7 @@ export class Channel { @PrimaryColumn({ unique: true }) id: string; - @OneToMany(() => Vod, (vod) => vod.channel) + @OneToMany(() => Vod, (vod) => vod.channel, { onDelete: 'CASCADE' }) vods: Vod[]; @Column({ unique: true }) diff --git a/src/queues/entities/queue.entity.ts b/src/queues/entities/queue.entity.ts index 177836b..b85b6b2 100755 --- a/src/queues/entities/queue.entity.ts +++ b/src/queues/entities/queue.entity.ts @@ -17,7 +17,7 @@ export class Queue { @Column() vodId: string; - @ManyToOne(() => User, (user) => user.queues) + @ManyToOne(() => User, (user) => user.queues, { onDelete: 'SET NULL' }) user: User; // @ManyToOne(() => Vod, (vod) => vod.queue) diff --git a/src/vods/entities/vod.entity.ts b/src/vods/entities/vod.entity.ts index bcd9eb6..3d789b9 100755 --- a/src/vods/entities/vod.entity.ts +++ b/src/vods/entities/vod.entity.ts @@ -20,7 +20,7 @@ export class Vod { @PrimaryColumn({ unique: true }) id: string; - @ManyToOne(() => Channel, (channel) => channel.vods) + @ManyToOne(() => Channel, (channel) => channel.vods, { onDelete: 'CASCADE' }) channel: Channel; // @OneToMany(() => Queue, (queue) => queue.vod)