| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import { PrismaClient } from '@prisma/client';
- const prisma = new PrismaClient();
- /**
- * 获取用户偏好设置
- */
- export async function getPreferences(userId: string) {
- const userIdNum = parseInt(userId);
-
- let preferences = await prisma.userPreference.findUnique({
- where: { userId: userIdNum },
- });
- // 如果不存在,创建默认偏好
- if (!preferences) {
- preferences = await prisma.userPreference.create({
- data: {
- userId: userIdNum,
- playSpeed: 1.0,
- quality: 'standard',
- theme: 'light',
- },
- });
- }
- return preferences;
- }
- /**
- * 更新用户偏好设置
- */
- export async function updatePreferences(
- userId: string,
- data: {
- playSpeed?: number;
- quality?: string;
- theme?: string;
- }
- ) {
- 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',
- },
- });
- }
- }
|