Mercurial Hosting > lang
comparison src/lib/Course.luan @ 23:0c17c233c45a
start courses
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 30 Jul 2025 23:29:33 -0600 |
parents | |
children | 87fe70201aa8 |
comparison
equal
deleted
inserted
replaced
22:7b35fb1e6603 | 23:0c17c233c45a |
---|---|
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() | |
6 local Db = require "site:/lib/Db.luan" | |
7 local languages = require "site:/lib/languages.luan" | |
8 | |
9 | |
10 local Course = {} | |
11 | |
12 local function from_doc(doc) | |
13 doc.type == "course" or error "wrong type" | |
14 return Course.new { | |
15 id = doc.id | |
16 user_id = doc.course_user_id | |
17 language = doc.course_language | |
18 name = doc.name | |
19 ai_system_prompt = doc.ai_system_prompt | |
20 } | |
21 end | |
22 | |
23 local function to_doc(course) | |
24 return { | |
25 type = "course" | |
26 id = course.id | |
27 course_user_id = long(course.user_id) | |
28 course_language = course.language or error() | |
29 name = course.name or error() | |
30 ai_system_prompt = course.ai_system_prompt or error() | |
31 } | |
32 end | |
33 | |
34 function Course.new(course) | |
35 | |
36 function course.save() | |
37 local doc = to_doc(chat) | |
38 Db.save(doc) | |
39 chat.id = doc.id | |
40 end | |
41 | |
42 function course.language_name() | |
43 return languages[course.language].name | |
44 end | |
45 | |
46 return course | |
47 end | |
48 | |
49 local function search(query,sort,rows) | |
50 rows = rows or 1000000 | |
51 local courses = {} | |
52 local docs = Db.search(query,1,rows,{sort=sort}) | |
53 for _, doc in ipairs(docs) do | |
54 courses[#courses+1] = from_doc(doc) | |
55 end | |
56 return courses | |
57 end | |
58 | |
59 function Course.get_for_language(language) | |
60 return search("course_language:"..language) | |
61 end | |
62 | |
63 function Course.get_by_id(id) | |
64 local doc = Db.get_document("id:"..id) | |
65 return doc and doc.type=="course" and from_doc(doc) or nil | |
66 end | |
67 | |
68 return Course |