twitch-clone/backend/src/module/category/category.service.ts
2025-07-13 11:21:06 +03:00

72 lines
1.5 KiB
TypeScript

import { PrismaService } from '@/src/core/prisma/prisma.service'
import { Injectable, NotFoundException } from '@nestjs/common'
@Injectable()
export class CategoryService {
constructor(
private readonly prismaService: PrismaService,
) {
}
public async findAll() {
return this.prismaService.category.findMany({
orderBy: { createdAt: 'desc' },
include: {
streams: {
include: {
user: true,
category: true,
},
},
},
})
}
public async findRandom() {
const total = await this.prismaService.category.count({})
const randomIndexes = new Set<number>()
while (randomIndexes.size < 7) {
const randomIndex = Math.floor(Math.random() * total)
randomIndexes.add(randomIndex)
}
const categories = await this.prismaService.category.findMany({
take: total,
skip: 0,
include: {
streams: {
include: {
user: true,
category: true,
},
},
},
})
return Array.from(randomIndexes).map(index => categories[index])
}
public async findBySlug(slug: string) {
const category = await this.prismaService.category.findUnique({
where: {
slug,
},
include: {
streams: {
include: {
user: true,
category: true,
},
},
},
})
if (!category) {
throw new NotFoundException('Категория не найдена')
}
return category
}
}