Mercurial Hosting > luan
annotate blog/src/lib/Post.luan @ 639:54f118abc842
Added tag 0.16 for changeset 3ab4f282f0bd
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 07 Mar 2016 16:46:06 -0700 |
parents | b73f005f3735 |
children | ca169567ce07 |
rev | line source |
---|---|
596 | 1 local Luan = require "luan:Luan" |
2 local error = Luan.error | |
3 local ipairs = Luan.ipairs or error() | |
4 local assert_string = Luan.assert_string or error() | |
5 local Time = require "luan:Time" | |
6 local now = Time.now or error() | |
599
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
7 local String = require "luan:String" |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
8 local trim = String.trim or error() |
596 | 9 local Db = require "site:/lib/Db" |
10 | |
11 | |
12 local M = {} | |
13 | |
14 local function from_doc(doc) | |
15 return M.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 | |
23 function M.new(this) | |
24 assert_string(this.subject) | |
25 assert_string(this.content) | |
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 } |
36 Db.save(doc) | |
37 this.id = doc.id | |
38 end | |
39 | |
40 return this | |
41 end | |
42 | |
43 function M.get_by_id(id) | |
44 local doc = Db.get_document("id:"..id) | |
45 return doc and from_doc(doc) | |
46 end | |
47 | |
48 function M.get_all() | |
49 local docs = Db.search("type:post",1,1000,"id desc") | |
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 | |
599
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
57 function M.search(query) |
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 |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
60 return M.get_all() |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
61 end |
50540f0813e2
support default search fields in lucene;
Franklin Schmidt <fschmidt@gmail.com>
parents:
596
diff
changeset
|
62 local docs = Db.search(query,1,1000) |
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 |
596 | 70 return M |