23
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local ipairs = Luan.ipairs or error()
|
|
4 local Number = require "luan:Number.luan"
|
|
5 local long = Number.long or error()
|
24
|
6 local Html = require "luan:Html.luan"
|
|
7 local html_encode = Html.encode or error()
|
23
|
8 local Db = require "site:/lib/Db.luan"
|
46
|
9 local Shared = require "site:/lib/Shared.luan"
|
|
10 local languages = Shared.languages or error()
|
23
|
11
|
|
12
|
|
13 local Course = {}
|
|
14
|
|
15 local function from_doc(doc)
|
|
16 doc.type == "course" or error "wrong type"
|
|
17 return Course.new {
|
|
18 id = doc.id
|
|
19 user_id = doc.course_user_id
|
|
20 language = doc.course_language
|
24
|
21 updated = doc.course_updated
|
23
|
22 name = doc.name
|
|
23 ai_system_prompt = doc.ai_system_prompt
|
25
|
24 ai_first_message = doc.ai_first_message
|
46
|
25 tts_instructions = doc.tts_instructions
|
23
|
26 }
|
|
27 end
|
|
28
|
|
29 local function to_doc(course)
|
|
30 return {
|
|
31 type = "course"
|
|
32 id = course.id
|
|
33 course_user_id = long(course.user_id)
|
|
34 course_language = course.language or error()
|
24
|
35 course_updated = long(course.updated)
|
23
|
36 name = course.name or error()
|
|
37 ai_system_prompt = course.ai_system_prompt or error()
|
25
|
38 ai_first_message = course.ai_first_message
|
46
|
39 tts_instructions = course.tts_instructions
|
23
|
40 }
|
|
41 end
|
|
42
|
|
43 function Course.new(course)
|
|
44
|
|
45 function course.save()
|
24
|
46 local doc = to_doc(course)
|
23
|
47 Db.save(doc)
|
24
|
48 course.id = doc.id
|
|
49 end
|
|
50
|
|
51 function course.name_html()
|
|
52 return html_encode(course.name)
|
23
|
53 end
|
|
54
|
|
55 function course.language_name()
|
46
|
56 return languages[course.language]
|
23
|
57 end
|
|
58
|
|
59 return course
|
|
60 end
|
|
61
|
24
|
62 function Course.search(query,sort,rows)
|
23
|
63 rows = rows or 1000000
|
|
64 local courses = {}
|
|
65 local docs = Db.search(query,1,rows,{sort=sort})
|
|
66 for _, doc in ipairs(docs) do
|
|
67 courses[#courses+1] = from_doc(doc)
|
|
68 end
|
|
69 return courses
|
|
70 end
|
|
71
|
|
72 function Course.get_by_id(id)
|
|
73 local doc = Db.get_document("id:"..id)
|
|
74 return doc and doc.type=="course" and from_doc(doc) or nil
|
|
75 end
|
|
76
|
|
77 return Course
|