comparison src/lib/Post.luan @ 10:f9e6a4cc4f7d

add Post
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 30 Oct 2024 23:18:45 -0600
parents
children c54c806fcc6e
comparison
equal deleted inserted replaced
9:b8b12fd8be22 10:f9e6a4cc4f7d
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
8
9 local Post = {}
10
11 local function from_doc(doc)
12 doc.type == "post" or error "wrong type"
13 return Post.new {
14 id = doc.id
15 chat_id = doc.post_chat_id
16 author_id = doc.author_id
17 date = doc.date
18 content = doc.content
19 }
20 end
21
22 local function to_doc(post)
23 return {
24 type = "post"
25 id = post.id
26 post_chat_id = long(post.chat_id)
27 author_id = long(post.author_id)
28 date = long(post.date)
29 content = post.content or error()
30 }
31 end
32
33 function Post.new(post)
34 function post.save()
35 local doc = to_doc(post)
36 Db.save(doc)
37 post.id = doc.id
38 end
39
40 return post
41 end
42
43 function Post.search(query,sort,rows)
44 rows = rows or 1000000
45 local posts = {}
46 local docs = Db.search(query,1,rows,{sort=sort})
47 for _, doc in ipairs(docs) do
48 posts[#posts+1] = from_doc(doc)
49 end
50 return posts
51 end
52
53 return Post