|
|
@@ -1,6 +1,7 @@
|
|
|
import { Audio, AudioDocument } from '../../models/Audio';
|
|
|
import { User } from '../../models/User';
|
|
|
import { PaginationResult } from '../../types';
|
|
|
+import mongoose from 'mongoose';
|
|
|
|
|
|
// 获取用户音频列表
|
|
|
export async function getAudioList(
|
|
|
@@ -10,9 +11,21 @@ export async function getAudioList(
|
|
|
pageSize?: number;
|
|
|
isFavorite?: boolean;
|
|
|
keyword?: string;
|
|
|
+ category?: string;
|
|
|
}
|
|
|
): Promise<PaginationResult<AudioDocument>> {
|
|
|
- const { page = 1, pageSize = 10, isFavorite, keyword } = options;
|
|
|
+ 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 };
|
|
|
|
|
|
@@ -27,6 +40,10 @@ export async function getAudioList(
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ if (category) {
|
|
|
+ query.category = category;
|
|
|
+ }
|
|
|
+
|
|
|
const total = await Audio.countDocuments(query);
|
|
|
const totalPages = Math.ceil(total / pageSize);
|
|
|
|
|
|
@@ -46,17 +63,29 @@ export async function getAudioList(
|
|
|
|
|
|
// 获取单个音频
|
|
|
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;
|
|
|
|
|
|
@@ -71,6 +100,10 @@ export async function updateAudio(
|
|
|
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;
|
|
|
|
|
|
@@ -88,6 +121,11 @@ export async function getUserStats(userId: string): Promise<{
|
|
|
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 } },
|
|
|
{
|