changeset 23:0c17c233c45a

start courses
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 30 Jul 2025 23:29:33 -0600
parents 7b35fb1e6603
children 87fe70201aa8
files src/chats.html.luan src/lib/Course.luan src/lib/Db.luan src/new_chat.red.luan
diffstat 4 files changed, 92 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
diff -r 7b35fb1e6603 -r 0c17c233c45a src/chats.html.luan
--- a/src/chats.html.luan	Wed Jul 30 19:51:49 2025 -0600
+++ b/src/chats.html.luan	Wed Jul 30 23:29:33 2025 -0600
@@ -1,6 +1,7 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
+local pairs = Luan.pairs or error()
 local Io = require "luan:Io.luan"
 local Http = require "luan:http/Http.luan"
 local Shared = require "site:/lib/Shared.luan"
@@ -10,6 +11,9 @@
 local current_user = User.current or error()
 local Chat = require "site:/lib/Chat.luan"
 local chat_search = Chat.search or error()
+local languages = require "site:/lib/languages.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "chats.html"
 
 
 return function()
@@ -19,6 +23,7 @@
 		return
 	end
 	local chats = chat_search( "chat_user_id:"..user.id, "chat_updated desc" )
+	local select_language = #chats > 0 and chats[1].language or nil
 	Io.stdout = Http.response.text_writer()
 %>
 <!doctype html>
@@ -26,6 +31,9 @@
 	<head>
 <%		head() %>
 		<style>
+			form {
+				margin-bottom: 20px;
+			}
 			td {
 				padding: 8px 8px;
 			}
@@ -35,7 +43,17 @@
 <%		header() %>
 		<div content>
 			<h1>Your Chats</h1>
-			<p><a href="new_chat.red">new chat</a></p>
+			<form action="new_chat.red">
+				<select name=language>
+<%	for _, lang in pairs(languages) do
+		local code = lang.code
+		local selected = code==select_language and "selected" or ""
+%>
+					<option value="<%=code%>" <%=selected%> ><%=lang.name%></option>
+<%	end %>
+				</select>
+				<input type=submit value="new chat">
+			</form>
 			<table>
 <%	for _, chat in ipairs(chats) do %>
 				<tr>
diff -r 7b35fb1e6603 -r 0c17c233c45a src/lib/Course.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/Course.luan	Wed Jul 30 23:29:33 2025 -0600
@@ -0,0 +1,68 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local Number = require "luan:Number.luan"
+local long = Number.long or error()
+local Db = require "site:/lib/Db.luan"
+local languages = require "site:/lib/languages.luan"
+
+
+local Course = {}
+
+local function from_doc(doc)
+	doc.type == "course" or error "wrong type"
+	return Course.new {
+		id = doc.id
+		user_id = doc.course_user_id
+		language = doc.course_language
+		name = doc.name
+		ai_system_prompt = doc.ai_system_prompt
+	}
+end
+
+local function to_doc(course)
+	return {
+		type = "course"
+		id = course.id
+		course_user_id = long(course.user_id)
+ 		course_language = course.language or error()
+		name = course.name or error()
+		ai_system_prompt = course.ai_system_prompt or error()
+	}
+end
+
+function Course.new(course)
+
+	function course.save()
+		local doc = to_doc(chat)
+		Db.save(doc)
+		chat.id = doc.id
+	end
+
+	function course.language_name()
+		return languages[course.language].name
+	end
+
+	return course
+end
+
+local function search(query,sort,rows)
+	rows = rows or 1000000
+	local courses = {}
+	local docs = Db.search(query,1,rows,{sort=sort})
+	for _, doc in ipairs(docs) do
+		courses[#courses+1] = from_doc(doc)
+	end
+	return courses
+end
+
+function Course.get_for_language(language)
+	return search("course_language:"..language)
+end
+
+function Course.get_by_id(id)
+	local doc = Db.get_document("id:"..id)
+	return doc and doc.type=="course" and from_doc(doc) or nil
+end
+
+return Course
diff -r 7b35fb1e6603 -r 0c17c233c45a src/lib/Db.luan
--- a/src/lib/Db.luan	Wed Jul 30 19:51:49 2025 -0600
+++ b/src/lib/Db.luan	Wed Jul 30 23:29:33 2025 -0600
@@ -21,6 +21,9 @@
 Db.indexed_fields.chat_user_id = Lucene.type.long
 Db.indexed_fields.chat_updated = Lucene.type.long
 
+Db.indexed_fields.course_user_id = Lucene.type.long
+Db.indexed_fields.course_language = Lucene.type.string
+
 Db.restore_from_log()
 
 if Http.is_serving then
diff -r 7b35fb1e6603 -r 0c17c233c45a src/new_chat.red.luan
--- a/src/new_chat.red.luan	Wed Jul 30 19:51:49 2025 -0600
+++ b/src/new_chat.red.luan	Wed Jul 30 23:29:33 2025 -0600
@@ -8,10 +8,11 @@
 
 return function()
 	local user = current_user() or error()
+	local language = Http.request.parameters.language or error()
 	local chat = Chat.new{
 		user_id = user.id
 		name = "whatever"
-		language = "jp"
+		language = language
 	}
 	chat.save()
 	Http.response.send_redirect("chat.html?chat="..chat.id)