43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { pool } from '@/lib/db'
|
|
import crypto from 'crypto'
|
|
|
|
export async function GET() {
|
|
try {
|
|
const [rows] = await pool.query('SELECT * FROM weekly_availability ORDER BY weekday, start_time')
|
|
return NextResponse.json(rows)
|
|
} catch (error) {
|
|
console.error('Error fetching weekly availability', error)
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const body = await req.json()
|
|
const { weekday, start_time, end_time, note } = body
|
|
if (weekday === undefined || !start_time || !end_time) {
|
|
return NextResponse.json({ error: 'Missing fields' }, { status: 400 })
|
|
}
|
|
|
|
const id = crypto.randomUUID()
|
|
await pool.query('INSERT INTO weekly_availability SET ?', [{ id, weekday, start_time, end_time, note }])
|
|
return NextResponse.json({ ok: true, id })
|
|
} catch (error) {
|
|
console.error('Error creating weekly availability', error)
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function DELETE(req: Request) {
|
|
try {
|
|
const { id } = await req.json()
|
|
if (!id) return NextResponse.json({ error: 'Missing id' }, { status: 400 })
|
|
await pool.query('DELETE FROM weekly_availability WHERE id = ?', [id])
|
|
return NextResponse.json({ ok: true })
|
|
} catch (error) {
|
|
console.error('Error deleting weekly availability', error)
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
|
|
}
|
|
}
|