| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import { Audio, AudioDocument } from '../../models/Audio';
- import { User } from '../../models/User';
- import { PaginationResult } from '../../types';
- import mongoose from 'mongoose';
- // 获取用户音频列表
- export async function getAudioList(
- userId: string,
- options: {
- page?: number;
- pageSize?: number;
- isFavorite?: boolean;
- keyword?: string;
- category?: string;
- }
- ): Promise<PaginationResult<AudioDocument>> {
- const { page = 1, pageSize = 10, isFavorite, keyword, category } = options;
- // 验证 userId 是否为有效的 ObjectId
- if (!userId || !mongoose.Types.ObjectId.isValid(userId)) {
- return {
- list: [],
- total: 0,
- page,
- pageSize,
- totalPages: 0,
- };
- }
- const query: Record<string, unknown> = { userId };
- if (isFavorite !== undefined) {
- query.isFavorite = isFavorite;
- }
- if (keyword) {
- query.$or = [
- { title: { $regex: keyword, $options: 'i' } },
- { text: { $regex: keyword, $options: 'i' } },
- ];
- }
- if (category) {
- query.category = category;
- }
- const total = await Audio.countDocuments(query);
- const totalPages = Math.ceil(total / pageSize);
- const list = await Audio.find(query)
- .sort({ createdAt: -1 })
- .skip((page - 1) * pageSize)
- .limit(pageSize);
- return {
- list,
- total,
- page,
- pageSize,
- totalPages,
- };
- }
- // 获取单个音频
- export async function getAudioById(audioId: string, userId: string): Promise<AudioDocument | null> {
- // 验证 userId 是否为有效的 ObjectId
- if (!userId || !mongoose.Types.ObjectId.isValid(userId)) {
- return null;
- }
- return Audio.findOne({ _id: audioId, userId });
- }
- // 删除音频
- export async function deleteAudio(audioId: string, userId: string): Promise<boolean> {
- // 验证 userId 是否为有效的 ObjectId
- if (!userId || !mongoose.Types.ObjectId.isValid(userId)) {
- return false;
- }
- const result = await Audio.findOneAndDelete({ _id: audioId, userId });
- return !!result;
- }
- // 切换收藏状态
- export async function toggleFavorite(audioId: string, userId: string): Promise<AudioDocument | null> {
- // 验证 userId 是否为有效的 ObjectId
- if (!userId || !mongoose.Types.ObjectId.isValid(userId)) {
- return null;
- }
- const audio = await Audio.findOne({ _id: audioId, userId });
- if (!audio) return null;
- audio.isFavorite = !audio.isFavorite;
- await audio.save();
- return audio;
- }
- // 更新音频信息
- export async function updateAudio(
- audioId: string,
- userId: string,
- data: { title?: string; tags?: string[] }
- ): Promise<AudioDocument | null> {
- // 验证 userId 是否为有效的 ObjectId
- if (!userId || !mongoose.Types.ObjectId.isValid(userId)) {
- return null;
- }
- const audio = await Audio.findOne({ _id: audioId, userId });
- if (!audio) return null;
- if (data.title) audio.title = data.title;
- if (data.tags) audio.tags = data.tags;
- await audio.save();
- return audio;
- }
- // 获取用户使用统计
- export async function getUserStats(userId: string): Promise<{
- totalAudios: number;
- totalDuration: number;
- totalWords: number;
- favoriteCount: number;
- }> {
- // 验证 userId 是否为有效的 ObjectId
- if (!userId || !mongoose.Types.ObjectId.isValid(userId)) {
- return { totalAudios: 0, totalDuration: 0, totalWords: 0, favoriteCount: 0 };
- }
- const stats = await Audio.aggregate([
- { $match: { userId: userId } },
- {
- $group: {
- _id: null,
- totalAudios: { $sum: 1 },
- totalDuration: { $sum: '$audioDuration' },
- totalWords: { $sum: '$wordCount' },
- favoriteCount: {
- $sum: { $cond: [{ $eq: ['$isFavorite', true] }, 1, 0] },
- },
- },
- },
- ]);
- if (stats.length === 0) {
- return { totalAudios: 0, totalDuration: 0, totalWords: 0, favoriteCount: 0 };
- }
- return {
- totalAudios: stats[0].totalAudios,
- totalDuration: stats[0].totalDuration,
- totalWords: stats[0].totalWords,
- favoriteCount: stats[0].favoriteCount,
- };
- }
|