Fix image handling to support category, room_id, and location_target fields
This commit is contained in:
@@ -5,7 +5,7 @@ import { db } from '@/lib/db';
|
|||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
await requireRole('admin');
|
await requireRole('admin');
|
||||||
const { rows } = await db.query('SELECT id, filename, uploaded_at FROM images ORDER BY uploaded_at DESC');
|
const { rows } = await db.query('SELECT id, filename, category, room_id, location_target, uploaded_at FROM images ORDER BY uploaded_at DESC');
|
||||||
return NextResponse.json({ images: rows, count: rows.length });
|
return NextResponse.json({ images: rows, count: rows.length });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if ((error as Error).message === 'Unauthorized') {
|
if ((error as Error).message === 'Unauthorized') {
|
||||||
@@ -20,15 +20,15 @@ export async function POST(request: NextRequest) {
|
|||||||
try {
|
try {
|
||||||
await requireRole('admin');
|
await requireRole('admin');
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
const { filename, data } = body;
|
const { filename, data, category, room_id, location_target } = body;
|
||||||
|
|
||||||
if (!filename || !data) {
|
if (!filename || !data) {
|
||||||
return NextResponse.json({ error: 'Missing filename or data' }, { status: 400 });
|
return NextResponse.json({ error: 'Missing filename or data' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const { rows } = await db.query(
|
const { rows } = await db.query(
|
||||||
'INSERT INTO images (filename, data) VALUES ($1, $2) RETURNING id, filename, uploaded_at',
|
'INSERT INTO images (filename, data, category, room_id, location_target) VALUES ($1, $2, $3, $4, $5) RETURNING id, filename, category, room_id, location_target, uploaded_at',
|
||||||
[filename, data]
|
[filename, data, category || 'other', room_id || null, location_target || 'gallery']
|
||||||
);
|
);
|
||||||
|
|
||||||
return NextResponse.json({ image: rows[0] });
|
return NextResponse.json({ image: rows[0] });
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ export async function initSchema() {
|
|||||||
data TEXT NOT NULL,
|
data TEXT NOT NULL,
|
||||||
category VARCHAR(100) DEFAULT 'other',
|
category VARCHAR(100) DEFAULT 'other',
|
||||||
room_id INTEGER REFERENCES rooms(id) ON DELETE SET NULL,
|
room_id INTEGER REFERENCES rooms(id) ON DELETE SET NULL,
|
||||||
|
location_target VARCHAR(100) DEFAULT 'gallery',
|
||||||
uploaded_at TIMESTAMP DEFAULT NOW()
|
uploaded_at TIMESTAMP DEFAULT NOW()
|
||||||
);
|
);
|
||||||
`);
|
`);
|
||||||
|
|||||||
Reference in New Issue
Block a user