File size: 2,107 Bytes
4bea261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { getGenres, saveGenre, deleteGenre, bulkDeleteGenres, syncFromMovies } from '../../../lib/localDb.js';

export async function GET({ request }) {
  try {
    const list = await getGenres();
    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('genres');
      return new Response(JSON.stringify({ message: 'Đồng bộ thể loại thành công!', list }));
    }

    if (!slug || !name) {
      return new Response(JSON.stringify({ error: 'Slug và Tên thể loại không được để trống' }), { status: 400 });
    }

    const list = await saveGenre(slug, name);
    return new Response(JSON.stringify({ message: 'Lưu thể loại 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 bulkDeleteGenres(slugs);
      return new Response(JSON.stringify({ message: `Đã xóa hàng loạt ${slugs.length} thể loại!`, list }));
    }

    if (!slug) {
      return new Response(JSON.stringify({ error: 'Thiếu slug thể loại để xóa' }), { status: 400 });
    }

    list = await deleteGenre(slug);
    return new Response(JSON.stringify({ message: 'Xóa thể loại thành công!', list }));
  } catch (e) {
    return new Response(JSON.stringify({ error: e.message }), { status: 500 });
  }
}