add channel

This commit is contained in:
Sergey Krylov 2025-07-16 05:51:03 +03:00
parent 0c27a6f513
commit 23caed10b6
6 changed files with 87 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import { SessionModule } from '@/src/module/auth/session/session.module'
import { TotpModule } from '@/src/module/auth/totp/totp.module' import { TotpModule } from '@/src/module/auth/totp/totp.module'
import { VerificationModule } from '@/src/module/auth/verification/verification.module' import { VerificationModule } from '@/src/module/auth/verification/verification.module'
import { CategoryModule } from '@/src/module/category/category.module' import { CategoryModule } from '@/src/module/category/category.module'
import { ChannelModule } from '@/src/module/channel/channel.module'
import { ChatModule } from '@/src/module/chat/chat.module' import { ChatModule } from '@/src/module/chat/chat.module'
import { CronModule } from '@/src/module/cron/cron.module' import { CronModule } from '@/src/module/cron/cron.module'
import { FollowModule } from '@/src/module/follow/follow.module' import { FollowModule } from '@/src/module/follow/follow.module'
@ -60,6 +61,7 @@ import { RedisModule } from './redis/redis.module'
CategoryModule, CategoryModule,
ChatModule, ChatModule,
FollowModule, FollowModule,
ChannelModule,
], ],
}) })
export class CoreModule {} export class CoreModule {}

View File

@ -161,6 +161,8 @@ type Query {
findAllCategories: [CategoryModel!]! findAllCategories: [CategoryModel!]!
findAllStreams(filters: FilterInput!): [StreamModel!]! findAllStreams(filters: FilterInput!): [StreamModel!]!
findCategoryBySlug(slug: String!): CategoryModel! findCategoryBySlug(slug: String!): CategoryModel!
findChannelByUsername(name: String!): UserModel!
findChannelFollowersCount(channelId: String!): Float!
findCurrentSession: SessionModel! findCurrentSession: SessionModel!
findMessagesByStream(streamId: String!): [ChatMessageModel!]! findMessagesByStream(streamId: String!): [ChatMessageModel!]!
findMyFollowers: [FollowModel!]! findMyFollowers: [FollowModel!]!
@ -168,6 +170,7 @@ type Query {
findProfile: UserModel! findProfile: UserModel!
findRandomCategories: [CategoryModel!]! findRandomCategories: [CategoryModel!]!
findRandomStreams: [StreamModel!]! findRandomStreams: [StreamModel!]!
findRecommendedChannels: [UserModel!]!
findSessionsByUser: [SessionModel!]! findSessionsByUser: [SessionModel!]!
findSocialLinks: [SocialLinkModel!]! findSocialLinks: [SocialLinkModel!]!
generateTotpSecret: TotpModel! generateTotpSecret: TotpModel!
@ -254,6 +257,8 @@ type UserModel {
deactivatedAt: DateTime deactivatedAt: DateTime
displayName: String! displayName: String!
email: String! email: String!
followers: [FollowModel!]!
followings: [FollowModel!]!
id: ID! id: ID!
isDeactivated: Boolean! isDeactivated: Boolean!
isEmailVerified: Boolean! isEmailVerified: Boolean!

View File

@ -1,4 +1,5 @@
import { SocialLinkModel } from '@/src/module/auth/profile/inputs/models/social-link.model' import { SocialLinkModel } from '@/src/module/auth/profile/inputs/models/social-link.model'
import { FollowModel } from '@/src/module/follow/model/follow.model'
import { StreamModel } from '@/src/module/stream/models/stream.model' import { StreamModel } from '@/src/module/stream/models/stream.model'
import { Field, ID, ObjectType } from '@nestjs/graphql' import { Field, ID, ObjectType } from '@nestjs/graphql'
import { User } from '@prisma/generated' import { User } from '@prisma/generated'
@ -50,6 +51,12 @@ export class UserModel implements User {
@Field(() => StreamModel) @Field(() => StreamModel)
stream: StreamModel stream: StreamModel
@Field(() => [FollowModel])
followers: FollowModel[]
@Field(() => [FollowModel])
followings: FollowModel[]
@Field(() => Date) @Field(() => Date)
createdAt: Date createdAt: Date

View File

@ -0,0 +1,8 @@
import { Module } from '@nestjs/common'
import { ChannelService } from './channel.service'
import { ChannelResolver } from './channel.resolver'
@Module({
providers: [ChannelResolver, ChannelService],
})
export class ChannelModule {}

View File

@ -0,0 +1,23 @@
import { UserModel } from '@/src/module/auth/account/models/user.model'
import { Args, Query, Resolver } from '@nestjs/graphql'
import { ChannelService } from './channel.service'
@Resolver('Channel')
export class ChannelResolver {
constructor(private readonly channelService: ChannelService) {}
@Query(() => [UserModel], { name: 'findRecommendedChannels' })
public async findRecommended() {
return this.channelService.findRecommendedChannel()
}
@Query(() => UserModel, { name: 'findChannelByUsername' })
public async findByUsername(@Args('name') name: string) {
return this.channelService.findByUsername(name)
}
@Query(() => Number, { name: 'findChannelFollowersCount' })
public async findFollowersCount(@Args('channelId') channelId: string) {
return this.channelService.findFollowersCountByChannel(channelId)
}
}

View File

@ -0,0 +1,42 @@
import { PrismaService } from '@/src/core/prisma/prisma.service'
import { Injectable, NotFoundException } from '@nestjs/common'
@Injectable()
export class ChannelService {
constructor(
private readonly prismaService: PrismaService,
) {
}
public async findRecommendedChannel() {
return this.prismaService.user.findMany({
where: { isDeactivated: false },
orderBy: { followings: { _count: 'desc' } },
include: { stream: true },
take: 7,
})
}
public async findByUsername(name: string) {
const channel = await this.prismaService.user.findUnique({
where: { name, isDeactivated: false },
include: {
socialLink: { orderBy: { position: 'desc' } },
stream: { include: { category: true } },
followings: true,
},
})
if (!channel) {
throw new NotFoundException('Канал не найден')
}
return channel
}
public async findFollowersCountByChannel(channelId: string) {
return this.prismaService.follow.count({
where: { following: { id: channelId } },
})
}
}