feat: room amenities selection in room editor
This commit is contained in:
@@ -8,7 +8,15 @@ export async function GET(request: NextRequest, { params }: { params: { id: stri
|
||||
if (isNaN(id)) return NextResponse.json({ error: 'Invalid id' }, { status: 400 });
|
||||
const { rows } = await db.query('SELECT * FROM rooms WHERE id = $1', [id]);
|
||||
if (!rows.length) return NextResponse.json({ error: 'Room not found' }, { status: 404 });
|
||||
return NextResponse.json({ data: rows[0] });
|
||||
|
||||
// Get amenities for this room
|
||||
const { rows: amenityRows } = await db.query(
|
||||
'SELECT amenity_id FROM room_amenity_links WHERE room_id = $1',
|
||||
[id]
|
||||
);
|
||||
const amenity_ids = amenityRows.map(r => r.amenity_id);
|
||||
|
||||
return NextResponse.json({ data: { ...rows[0], amenity_ids } });
|
||||
} catch (error: any) {
|
||||
console.error('GET /api/rooms/[id]:', error);
|
||||
return NextResponse.json({ error: error.message }, { status: 500 });
|
||||
@@ -21,7 +29,7 @@ export async function PUT(request: NextRequest, { params }: { params: { id: stri
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) return NextResponse.json({ error: 'Invalid id' }, { status: 400 });
|
||||
const body = await request.json();
|
||||
const { name, description, price, photos, sort_order, featured_photo, active } = body;
|
||||
const { name, description, price, photos, sort_order, featured_photo, active, amenity_ids } = body;
|
||||
|
||||
if (!name || !description || price === undefined) {
|
||||
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
|
||||
@@ -31,6 +39,14 @@ export async function PUT(request: NextRequest, { params }: { params: { id: stri
|
||||
`UPDATE rooms SET name = $1, description = $2, price = $3, photos = $4, sort_order = $5, featured_photo = $6, active = $7 WHERE id = $8 RETURNING *`,
|
||||
[name, description, price, photos || [], sort_order || 0, featured_photo || null, active !== false, id]
|
||||
);
|
||||
|
||||
// Update amenities
|
||||
await db.query('DELETE FROM room_amenity_links WHERE room_id = $1', [id]);
|
||||
if (amenity_ids && amenity_ids.length > 0) {
|
||||
const values = amenity_ids.map((aid: number) => `(${id}, ${aid})`).join(',');
|
||||
await db.query(`INSERT INTO room_amenity_links (room_id, amenity_id) VALUES ${values}`);
|
||||
}
|
||||
|
||||
return NextResponse.json({ data: rows[0] });
|
||||
} catch (error: any) {
|
||||
if ((error as Error).message === 'Unauthorized') return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
|
||||
@@ -5,7 +5,21 @@ import { db } from '@/lib/db';
|
||||
export async function GET() {
|
||||
try {
|
||||
const { rows } = await db.query('SELECT * FROM rooms WHERE active = TRUE ORDER BY sort_order, id');
|
||||
return NextResponse.json({ data: rows });
|
||||
|
||||
// Get amenity links for all rooms
|
||||
const { rows: amenityLinks } = await db.query('SELECT room_id, amenity_id FROM room_amenity_links');
|
||||
const amenityMap = new Map<number, number[]>();
|
||||
for (const link of amenityLinks) {
|
||||
if (!amenityMap.has(link.room_id)) amenityMap.set(link.room_id, []);
|
||||
amenityMap.get(link.room_id)!.push(link.amenity_id);
|
||||
}
|
||||
|
||||
const roomsWithAmenities = rows.map(r => ({
|
||||
...r,
|
||||
amenity_ids: amenityMap.get(r.id) || []
|
||||
}));
|
||||
|
||||
return NextResponse.json({ data: roomsWithAmenities });
|
||||
} catch (error: any) {
|
||||
console.error('GET /api/rooms error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to fetch rooms' }, { status: 500 });
|
||||
@@ -16,7 +30,7 @@ export async function POST(request: NextRequest) {
|
||||
try {
|
||||
await requireRole('admin');
|
||||
const body = await request.json();
|
||||
const { name, description, price, photos, sort_order, featured_photo } = body;
|
||||
const { name, description, price, photos, sort_order, featured_photo, amenity_ids } = body;
|
||||
|
||||
if (!name || !description || price === undefined) {
|
||||
return NextResponse.json({ error: 'Missing required fields' }, { status: 400 });
|
||||
@@ -28,8 +42,16 @@ export async function POST(request: NextRequest) {
|
||||
RETURNING *`,
|
||||
[name, description, price, photos || [], sort_order || 0, featured_photo || null]
|
||||
);
|
||||
|
||||
const roomId = rows[0].id;
|
||||
|
||||
// Insert amenity links
|
||||
if (amenity_ids && amenity_ids.length > 0) {
|
||||
const values = amenity_ids.map((aid: number) => `(${roomId}, ${aid})`).join(',');
|
||||
await db.query(`INSERT INTO room_amenity_links (room_id, amenity_id) VALUES ${values}`);
|
||||
}
|
||||
|
||||
return NextResponse.json({ data: rows[0] });
|
||||
return NextResponse.json({ data: { ...rows[0], amenity_ids: amenity_ids || [] } });
|
||||
} catch (error: any) {
|
||||
console.error('POST /api/rooms error:', error);
|
||||
return NextResponse.json({ error: error.message || 'Failed to create room' }, { status: 500 });
|
||||
|
||||
Reference in New Issue
Block a user