import { NextRequest, NextResponse } from 'next/server'; import { db } from '@/lib/db'; import { announcements } from '@/lib/db/schema'; import { eq } from 'drizzle-orm'; import { getSession } from '@/lib/session'; export async function PUT(request: NextRequest, { params }: { params: { id: string } }) { try { const session = await getSession(); if (!session || session.role !== 'admin') { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const { title, content, priority, expiresAt, isActive } = await request.json(); const announcementId = params.id; if (!title || !content) { return NextResponse.json({ error: 'Title and content are required' }, { status: 400 }); } // Check if announcement exists const existing = await db.select().from(announcements).where(eq(announcements.id, announcementId)).limit(1); if (existing.length === 0) { return NextResponse.json({ error: 'Announcement not found' }, { status: 404 }); } // Update announcement const [updatedAnnouncement] = await db .update(announcements) .set({ title, content, priority: priority || 'medium', expiresAt: expiresAt ? new Date(expiresAt) : null, isActive: isActive !== undefined ? isActive : true, updatedAt: new Date(), }) .where(eq(announcements.id, announcementId)) .returning(); return NextResponse.json({ announcement: updatedAnnouncement, message: 'Announcement updated successfully', }); } catch (error) { console.error('Error updating announcement:', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); } } export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) { try { const session = await getSession(); if (!session || session.role !== 'admin') { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const announcementId = params.id; // Check if announcement exists const existing = await db.select().from(announcements).where(eq(announcements.id, announcementId)).limit(1); if (existing.length === 0) { return NextResponse.json({ error: 'Announcement not found' }, { status: 404 }); } // Delete announcement await db.delete(announcements).where(eq(announcements.id, announcementId)); return NextResponse.json({ message: 'Announcement deleted successfully' }); } catch (error) { console.error('Error deleting announcement:', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); } }