File size: 1,622 Bytes
decea84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
import { apiFetch } from './client'

export interface Batch {
  id: string
  name: string
  instructor_id: string
  class_code: string
}

export interface Topic {
  id: string
  batch_id: string
  name: string
  is_unlocked: boolean
  order_index: number
  question_count: number
}

export async function getMyBatch(): Promise<Batch> {
  const res = await apiFetch('/api/batches/mine')
  if (!res.ok) throw new Error('No batch found')
  return res.json()
}

export async function createBatch(name: string): Promise<Batch> {
  const res = await apiFetch('/api/batches', {
    method: 'POST',
    body: JSON.stringify({ name }),
  })
  if (!res.ok) {
    const err = await res.json()
    throw new Error(err.detail ?? 'Failed to create batch')
  }
  return res.json()
}

export async function getTopics(batch_id: string): Promise<Topic[]> {
  const res = await apiFetch(`/api/topics?batch_id=${batch_id}`)
  if (!res.ok) throw new Error('Failed to load topics')
  return res.json()
}

export async function createTopic(batch_id: string, name: string): Promise<Topic> {
  const res = await apiFetch('/api/topics', {
    method: 'POST',
    body: JSON.stringify({ batch_id, name }),
  })
  if (!res.ok) {
    const err = await res.json()
    throw new Error(err.detail ?? 'Failed to create topic')
  }
  return res.json()
}

export async function setTopicUnlock(topic_id: string, is_unlocked: boolean): Promise<Topic> {
  const res = await apiFetch(`/api/topics/${topic_id}`, {
    method: 'PATCH',
    body: JSON.stringify({ is_unlocked }),
  })
  if (!res.ok) throw new Error('Failed to update topic')
  return res.json()
}