Add admin slideshow, horizontal animated calendar, and weather strip
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireRole } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export async function PUT(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const id = parseInt(params.id, 10);
|
||||
const body = await request.json();
|
||||
const { title, subtitle, image_id, image_url, active, sort_order } = body;
|
||||
|
||||
const { rows } = await db.query(
|
||||
`UPDATE slides SET title = $1, subtitle = $2, image_id = $3, image_url = $4, active = $5, sort_order = $6
|
||||
WHERE id = $7 RETURNING *`,
|
||||
[title || null, subtitle || null, image_id || null, image_url || null, active, sort_order || 0, id]
|
||||
);
|
||||
|
||||
if (rows.length === 0) {
|
||||
return NextResponse.json({ error: 'Slide not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ data: rows[0] });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('PUT /api/slides/:id error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to update slide' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const id = parseInt(params.id, 10);
|
||||
await db.query('DELETE FROM slides WHERE id = $1', [id]);
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('DELETE /api/slides/:id error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to delete slide' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { requireRole } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const { rows } = await db.query(
|
||||
`SELECT s.*, i.data as image_data
|
||||
FROM slides s
|
||||
LEFT JOIN images i ON s.image_id = i.id
|
||||
WHERE s.active = TRUE
|
||||
ORDER BY s.sort_order, s.id`
|
||||
);
|
||||
return NextResponse.json({ data: rows });
|
||||
} catch (error: any) {
|
||||
console.error('GET /api/slides error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to fetch slides' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const body = await request.json();
|
||||
const { title, subtitle, image_id, image_url, active, sort_order } = body;
|
||||
|
||||
const { rows } = await db.query(
|
||||
`INSERT INTO slides (title, subtitle, image_id, image_url, active, sort_order)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *`,
|
||||
[title || null, subtitle || null, image_id || null, image_url || null, active !== false, sort_order || 0]
|
||||
);
|
||||
|
||||
return NextResponse.json({ data: rows[0] });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
console.error('POST /api/slides error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to create slide' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user