Spaces:
Sleeping
Sleeping
| import { getServers, saveServer, deleteServer, bulkDeleteServers, syncFromMovies } from '../../../lib/localDb.js'; | |
| export async function GET({ request }) { | |
| try { | |
| const list = await getServers(); | |
| 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 { name, originalName, sync } = await request.json(); | |
| if (sync) { | |
| const list = await syncFromMovies('servers'); | |
| return new Response(JSON.stringify({ message: 'Đồng bộ danh sách máy chủ thành công!', list })); | |
| } | |
| if (!name) { | |
| return new Response(JSON.stringify({ error: 'Tên máy chủ không được để trống' }), { status: 400 }); | |
| } | |
| const list = await saveServer(name, originalName); | |
| return new Response(JSON.stringify({ message: 'Lưu máy chủ 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 { name, names } = await request.json(); | |
| let list; | |
| if (names && Array.isArray(names)) { | |
| list = await bulkDeleteServers(names); | |
| return new Response(JSON.stringify({ message: `Đã xóa hàng loạt ${names.length} máy chủ!`, list })); | |
| } | |
| if (!name) { | |
| return new Response(JSON.stringify({ error: 'Thiếu tên máy chủ để xóa' }), { status: 400 }); | |
| } | |
| list = await deleteServer(name); | |
| return new Response(JSON.stringify({ message: 'Xóa máy chủ thành công!', list })); | |
| } catch (e) { | |
| return new Response(JSON.stringify({ error: e.message }), { status: 500 }); | |
| } | |
| } | |