Commit 19c7c301 authored by f jt's avatar f jt
Browse files

revised

parent 590f1c60
......@@ -20,6 +20,7 @@ model Room {
roomName String
createdAt DateTime @default(now())
messages Message[]
roomcreatorId Int
}
model Message {
......
......@@ -9,7 +9,7 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
}
const { roomName } = await request.json();
const { roomName ,userId} = await request.json();
if (!roomName || typeof roomName !== 'string' || roomName.trim() === '') {
return NextResponse.json({ message: '有效的房间名称是必需的' }, { status: 400 });
......@@ -18,6 +18,7 @@ export async function POST(request: NextRequest) {
const newRoom = await prisma.room.create({
data: {
roomName: roomName.trim(),
roomcreatorId: userId,
},
});
......
......@@ -15,6 +15,16 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ message: '有效的 Room ID 是必需的' }, { status: 400 });
}
const room = await prisma.room.findUnique({
where: { roomId },
});
if (!room) {
return NextResponse.json({ message: '房间未找到' }, { status: 404 });
}
if(user.userId !== room.roomcreatorId){
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
}
await prisma.$transaction([
prisma.message.deleteMany({
where: { roomId: roomId },
......
......@@ -27,6 +27,7 @@ interface Room {
roomId: number;
roomName: string;
lastMessage: Message | null;
roomcreatorId: number;
}
type RoomListRes = { rooms: Room[] };
......@@ -130,7 +131,7 @@ const ChatRoomPage = ({ user, onLogout }: { user: User, onLogout: () => void })
const handleAddRoom = async () => {
if (!newRoomName.trim()) return;
try {
await addRoomTrigger({ roomName: newRoomName });
await addRoomTrigger({ roomName: newRoomName, userId:user.id });
setNewRoomName('');
mutateRooms();
} catch (e) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment