Skip to content
This repository has been archived by the owner on Jul 21, 2022. It is now read-only.

Commit

Permalink
Manual channel creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibbp committed Mar 8, 2022
1 parent 7b24500 commit 22b0885
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 14 deletions.
25 changes: 19 additions & 6 deletions src/channels/channels.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
48 changes: 43 additions & 5 deletions src/channels/channels.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
ConflictException,
HttpException,
HttpStatus,
Injectable,
InternalServerErrorException,
Logger,
NotFoundException,
} from '@nestjs/common';
Expand All @@ -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 {
Expand All @@ -22,7 +26,7 @@ export class ChannelsService {
private channelsRepository: ChannelsRepository,
private twitchService: TwitchService,
private filesService: FilesService,
) {}
) { }

async create(createChannelDto: CreateChannelDto) {
const { username } = createChannelDto;
Expand Down Expand Up @@ -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');
}
}
}
15 changes: 15 additions & 0 deletions src/channels/dto/manual-create-channel.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/channels/entities/channel.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion src/queues/entities/queue.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/vods/entities/vod.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 22b0885

Please sign in to comment.