audio.service.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getAudioList = getAudioList;
  4. exports.getAudioById = getAudioById;
  5. exports.deleteAudio = deleteAudio;
  6. exports.toggleFavorite = toggleFavorite;
  7. exports.updateAudio = updateAudio;
  8. exports.getUserStats = getUserStats;
  9. const models_1 = require("../../models");
  10. // 获取用户音频列表
  11. async function getAudioList(userId, options) {
  12. const { page = 1, pageSize = 10, isFavorite, keyword, category } = options;
  13. // 构建查询条件
  14. const where = {};
  15. // 只有提供了有效的 userId 才添加用户过滤条件
  16. if (userId) {
  17. where.userId = parseInt(userId);
  18. }
  19. if (isFavorite !== undefined) {
  20. where.isFavorite = isFavorite;
  21. }
  22. if (keyword) {
  23. where.OR = [
  24. { title: { contains: keyword } },
  25. { text: { contains: keyword } },
  26. ];
  27. }
  28. if (category) {
  29. where.category = category;
  30. }
  31. const total = await models_1.prisma.audio.count({ where });
  32. const totalPages = Math.ceil(total / pageSize);
  33. const list = await models_1.prisma.audio.findMany({
  34. where,
  35. orderBy: { createdAt: 'desc' },
  36. skip: (page - 1) * pageSize,
  37. take: pageSize,
  38. });
  39. return {
  40. list,
  41. total,
  42. page,
  43. pageSize,
  44. totalPages,
  45. };
  46. }
  47. // 获取单个音频
  48. async function getAudioById(audioId, userId) {
  49. const id = parseInt(audioId);
  50. if (isNaN(id)) {
  51. return null;
  52. }
  53. // 如果有有效的 userId,添加用户过滤
  54. if (userId) {
  55. return models_1.prisma.audio.findFirst({
  56. where: { id, userId: parseInt(userId) },
  57. });
  58. }
  59. // 没有 userId,返回该音频(允许匿名访问)
  60. return models_1.prisma.audio.findUnique({ where: { id } });
  61. }
  62. // 删除音频
  63. async function deleteAudio(audioId, userId) {
  64. // 验证 userId 是否为有效
  65. if (!userId) {
  66. return false;
  67. }
  68. const id = parseInt(audioId);
  69. if (isNaN(id)) {
  70. return false;
  71. }
  72. try {
  73. await models_1.prisma.audio.delete({
  74. where: { id, userId: parseInt(userId) },
  75. });
  76. return true;
  77. }
  78. catch {
  79. return false;
  80. }
  81. }
  82. // 切换收藏状态
  83. async function toggleFavorite(audioId, userId) {
  84. // 验证 userId 是否为有效
  85. if (!userId) {
  86. return null;
  87. }
  88. const id = parseInt(audioId);
  89. if (isNaN(id)) {
  90. return null;
  91. }
  92. const audio = await models_1.prisma.audio.findFirst({
  93. where: { id, userId: parseInt(userId) },
  94. });
  95. if (!audio)
  96. return null;
  97. const updated = await models_1.prisma.audio.update({
  98. where: { id: audio.id },
  99. data: { isFavorite: !audio.isFavorite },
  100. });
  101. return updated;
  102. }
  103. // 更新音频信息
  104. async function updateAudio(audioId, userId, data) {
  105. // 验证 userId 是否为有效
  106. if (!userId) {
  107. return null;
  108. }
  109. const id = parseInt(audioId);
  110. if (isNaN(id)) {
  111. return null;
  112. }
  113. const audio = await models_1.prisma.audio.findFirst({
  114. where: { id, userId: parseInt(userId) },
  115. });
  116. if (!audio)
  117. return null;
  118. const updated = await models_1.prisma.audio.update({
  119. where: { id: audio.id },
  120. data: {
  121. ...(data.title && { title: data.title }),
  122. ...(data.tags && { tags: JSON.stringify(data.tags) }),
  123. },
  124. });
  125. return updated;
  126. }
  127. // 获取用户使用统计
  128. async function getUserStats(userId) {
  129. // 验证 userId 是否为有效
  130. if (!userId) {
  131. return { totalAudios: 0, totalDuration: 0, totalWords: 0, favoriteCount: 0 };
  132. }
  133. const uid = parseInt(userId);
  134. if (isNaN(uid)) {
  135. return { totalAudios: 0, totalDuration: 0, totalWords: 0, favoriteCount: 0 };
  136. }
  137. const totalAudios = await models_1.prisma.audio.count({ where: { userId: uid } });
  138. const allAudios = await models_1.prisma.audio.findMany({
  139. where: { userId: uid },
  140. select: {
  141. audioDuration: true,
  142. wordCount: true,
  143. isFavorite: true,
  144. },
  145. });
  146. const totalDuration = allAudios.reduce((sum, a) => sum + a.audioDuration, 0);
  147. const totalWords = allAudios.reduce((sum, a) => sum + a.wordCount, 0);
  148. const favoriteCount = allAudios.filter(a => a.isFavorite).length;
  149. return {
  150. totalAudios,
  151. totalDuration,
  152. totalWords,
  153. favoriteCount,
  154. };
  155. }