Files
no-whatever/src/lib/store.ts
T
kurihada d87d30ccc0 feat: 实现 NoWhatever 别说随便餐厅决策 Web App
- Framer Motion 卡片滑动 UI,带物理阻尼动画
- 多人房间系统,4位房间号 + SWR 实时轮询
- 高德地图 POI v5 API 搜索附近餐厅
- Web Share API 一键邀请,剪贴板降级方案
- SQLite/Prisma 持久化存储
- 移动端优先响应式设计 (Tailwind CSS)
2026-02-24 16:49:43 +08:00

62 lines
1.4 KiB
TypeScript

import { prisma } from "./prisma";
import { Restaurant } from "@/types";
export interface RoomData {
users: string[];
restaurants: Restaurant[];
likes: Record<string, string[]>;
match: string | null;
}
function generateRoomId(): string {
return String(Math.floor(1000 + Math.random() * 9000));
}
export async function createRoom(restaurants: Restaurant[]): Promise<string> {
const data: RoomData = {
users: [],
restaurants,
likes: {},
match: null,
};
let roomId: string;
let attempts = 0;
while (attempts < 20) {
roomId = generateRoomId();
const existing = await prisma.room.findUnique({ where: { id: roomId } });
if (!existing) {
await prisma.room.create({
data: { id: roomId, data: JSON.stringify(data) },
});
return roomId;
}
attempts++;
}
roomId = generateRoomId() + String(Date.now()).slice(-2);
await prisma.room.create({
data: { id: roomId, data: JSON.stringify(data) },
});
return roomId;
}
export async function getRoomData(
roomId: string,
): Promise<RoomData | null> {
const room = await prisma.room.findUnique({ where: { id: roomId } });
if (!room) return null;
return JSON.parse(room.data) as RoomData;
}
export async function updateRoomData(
roomId: string,
data: RoomData,
): Promise<void> {
await prisma.room.update({
where: { id: roomId },
data: { data: JSON.stringify(data) },
});
}