Mercurial Hosting > luan
annotate examples/blog/src/lib/Post.luan @ 1756:a4b232aa0554
serve.luan - allow extra args
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 05 Feb 2023 22:08:50 -0700 |
parents | 82415c9c0015 |
children |
rev | line source |
---|---|
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
604
diff
changeset
|
1 local Luan = require "luan:Luan.luan" |
596 | 2 local error = Luan.error |
3 local ipairs = Luan.ipairs or error() | |
1164 | 4 local type = Luan.type or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
604
diff
changeset
|
5 local Time = require "luan:Time.luan" |
596 | 6 local now = Time.now or error() |
693
ca169567ce07
module URIs must now include ".luan"
Franklin Schmidt <fschmidt@gmail.com>
parents:
604
diff
changeset
|
7 local String = require "luan:String.luan" |
599
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
8 local trim = String.trim or error() |
1429
82415c9c0015
move versioning into Lucene
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
9 local Db = require "site:/lib/Db.luan" |
596 | 10 |
11 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
779
diff
changeset
|
12 local Post = {} |
596 | 13 |
14 local function from_doc(doc) | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
779
diff
changeset
|
15 return Post.new { |
604
b73f005f3735
table constructor now uses end_of_line as a delimiter
Franklin Schmidt <fschmidt@gmail.com>
parents:
599
diff
changeset
|
16 id = doc.id |
b73f005f3735
table constructor now uses end_of_line as a delimiter
Franklin Schmidt <fschmidt@gmail.com>
parents:
599
diff
changeset
|
17 subject = doc.subject |
b73f005f3735
table constructor now uses end_of_line as a delimiter
Franklin Schmidt <fschmidt@gmail.com>
parents:
599
diff
changeset
|
18 content = doc.content |
b73f005f3735
table constructor now uses end_of_line as a delimiter
Franklin Schmidt <fschmidt@gmail.com>
parents:
599
diff
changeset
|
19 date = doc.date |
596 | 20 } |
21 end | |
22 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
779
diff
changeset
|
23 function Post.new(this) |
1164 | 24 type(this.subject)=="string" or error "subject must be string" |
25 type(this.content)=="string" or error "content must be string" | |
596 | 26 this.date = this.date or now() |
27 | |
28 function this.save() | |
29 local doc = { | |
604
b73f005f3735
table constructor now uses end_of_line as a delimiter
Franklin Schmidt <fschmidt@gmail.com>
parents:
599
diff
changeset
|
30 type = "post" |
b73f005f3735
table constructor now uses end_of_line as a delimiter
Franklin Schmidt <fschmidt@gmail.com>
parents:
599
diff
changeset
|
31 id = this.id |
b73f005f3735
table constructor now uses end_of_line as a delimiter
Franklin Schmidt <fschmidt@gmail.com>
parents:
599
diff
changeset
|
32 subject = this.subject |
b73f005f3735
table constructor now uses end_of_line as a delimiter
Franklin Schmidt <fschmidt@gmail.com>
parents:
599
diff
changeset
|
33 content = this.content |
b73f005f3735
table constructor now uses end_of_line as a delimiter
Franklin Schmidt <fschmidt@gmail.com>
parents:
599
diff
changeset
|
34 date = this.date |
596 | 35 } |
1429
82415c9c0015
move versioning into Lucene
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
36 Db.save(doc) |
596 | 37 this.id = doc.id |
38 end | |
39 | |
40 return this | |
41 end | |
42 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
779
diff
changeset
|
43 function Post.get_by_id(id) |
1429
82415c9c0015
move versioning into Lucene
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
44 local doc = Db.get_document("id:"..id) |
596 | 45 return doc and from_doc(doc) |
46 end | |
47 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
779
diff
changeset
|
48 function Post.get_all() |
1429
82415c9c0015
move versioning into Lucene
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
49 local docs = Db.search("type:post",1,1000,{sort="id desc"}) |
596 | 50 local posts = {} |
51 for _, doc in ipairs(docs) do | |
52 posts[#posts+1] = from_doc(doc) | |
53 end | |
54 return posts | |
55 end | |
56 | |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
779
diff
changeset
|
57 function Post.search(query) |
599
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
58 query = trim(query) |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
59 if #query == 0 then |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
779
diff
changeset
|
60 return Post.get_all() |
599
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
61 end |
1429
82415c9c0015
move versioning into Lucene
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
62 local docs = Db.search(query,1,1000) |
599
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
63 local posts = {} |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
64 for _, doc in ipairs(docs) do |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
65 posts[#posts+1] = from_doc(doc) |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
66 end |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
67 return posts |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
68 end |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
69 |
1387
bc40bc9aab3a
start postgres backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1369
diff
changeset
|
70 function Post.delete_by_id(id) |
1429
82415c9c0015
move versioning into Lucene
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
71 Db.delete("id:"..id) |
1387
bc40bc9aab3a
start postgres backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1369
diff
changeset
|
72 end |
bc40bc9aab3a
start postgres backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
1369
diff
changeset
|
73 |
1088
bae2d0c2576c
change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents:
779
diff
changeset
|
74 return Post |