| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.getPreferences = getPreferences;
- exports.updatePreferences = updatePreferences;
- const client_1 = require("@prisma/client");
- const prisma = new client_1.PrismaClient();
- /**
- * 获取用户偏好设置
- */
- async function getPreferences(userId) {
- const userIdNum = parseInt(userId);
- // 使用 upsert 语义,避免外键约束问题
- const preferences = await prisma.userPreference.upsert({
- where: { userId: userIdNum },
- update: {},
- create: {
- userId: userIdNum,
- playSpeed: 1.0,
- quality: 'standard',
- theme: 'light',
- },
- });
- return preferences;
- }
- /**
- * 更新用户偏好设置
- */
- async function updatePreferences(userId, data) {
- const userIdNum = parseInt(userId);
- // 使用 upsert 语义
- const existing = await prisma.userPreference.findUnique({
- where: { userId: userIdNum },
- });
- if (existing) {
- // 更新
- return await prisma.userPreference.update({
- where: { userId: userIdNum },
- data,
- });
- }
- else {
- // 创建
- return await prisma.userPreference.create({
- data: {
- userId: userIdNum,
- playSpeed: data.playSpeed ?? 1.0,
- quality: data.quality ?? 'standard',
- theme: data.theme ?? 'light',
- },
- });
- }
- }
|