Spaces:
Sleeping
Sleeping
| import { getCountries, saveCountry, deleteCountry, bulkDeleteCountries, syncFromMovies } from '../../../lib/localDb.js'; | |
| export async function GET({ request }) { | |
| try { | |
| const list = await getCountries(); | |
| return new Response(JSON.stringify(list)); | |
| } catch (e) { | |
| return new Response(JSON.stringify({ error: e.message }), { status: 500 }); | |
| } | |
| } | |
| export async function POST({ request }) { | |
| const cookies = request.headers.get('cookie') || ''; | |
| if (!cookies.includes('auth_token=')) { | |
| return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401 }); | |
| } | |
| try { | |
| const { slug, name, sync } = await request.json(); | |
| if (sync) { | |
| const list = await syncFromMovies('countries'); | |
| return new Response(JSON.stringify({ message: 'Đồng bộ quốc gia thành công!', list })); | |
| } | |
| if (!slug || !name) { | |
| return new Response(JSON.stringify({ error: 'Slug và Tên quốc gia không được để trống' }), { status: 400 }); | |
| } | |
| const list = await saveCountry(slug, name); | |
| return new Response(JSON.stringify({ message: 'Lưu quốc gia thành công!', list })); | |
| } catch (e) { | |
| return new Response(JSON.stringify({ error: e.message }), { status: 500 }); | |
| } | |
| } | |
| export async function DELETE({ request }) { | |
| const cookies = request.headers.get('cookie') || ''; | |
| if (!cookies.includes('auth_token=')) { | |
| return new Response(JSON.stringify({ error: 'Unauthorized' }), { status: 401 }); | |
| } | |
| try { | |
| const { slug, slugs } = await request.json(); | |
| let list; | |
| if (slugs && Array.isArray(slugs)) { | |
| list = await bulkDeleteCountries(slugs); | |
| return new Response(JSON.stringify({ message: `Đã xóa hàng loạt ${slugs.length} quốc gia!`, list })); | |
| } | |
| if (!slug) { | |
| return new Response(JSON.stringify({ error: 'Thiếu slug quốc gia để xóa' }), { status: 400 }); | |
| } | |
| list = await deleteCountry(slug); | |
| return new Response(JSON.stringify({ message: 'Xóa quốc gia thành công!', list })); | |
| } catch (e) { | |
| return new Response(JSON.stringify({ error: e.message }), { status: 500 }); | |
| } | |
| } | |