Mercurial Hosting > luan
comparison examples/blog/src/lib/Post.luan @ 1088:bae2d0c2576c
change module naming convention
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 26 Dec 2016 22:29:36 -0700 |
parents | c38f6619feb9 |
children | 1f9d34a6f308 |
comparison
equal
deleted
inserted
replaced
1087:4aab4dd3ac9c | 1088:bae2d0c2576c |
---|---|
7 local String = require "luan:String.luan" | 7 local String = require "luan:String.luan" |
8 local trim = String.trim or error() | 8 local trim = String.trim or error() |
9 local Db = require "site:/lib/Db.luan" | 9 local Db = require "site:/lib/Db.luan" |
10 | 10 |
11 | 11 |
12 local M = {} | 12 local Post = {} |
13 | 13 |
14 local function from_doc(doc) | 14 local function from_doc(doc) |
15 return M.new { | 15 return Post.new { |
16 id = doc.id | 16 id = doc.id |
17 subject = doc.subject | 17 subject = doc.subject |
18 content = doc.content | 18 content = doc.content |
19 date = doc.date | 19 date = doc.date |
20 } | 20 } |
21 end | 21 end |
22 | 22 |
23 function M.new(this) | 23 function Post.new(this) |
24 assert_string(this.subject) | 24 assert_string(this.subject) |
25 assert_string(this.content) | 25 assert_string(this.content) |
26 this.date = this.date or now() | 26 this.date = this.date or now() |
27 | 27 |
28 function this.save() | 28 function this.save() |
38 end | 38 end |
39 | 39 |
40 return this | 40 return this |
41 end | 41 end |
42 | 42 |
43 function M.get_by_id(id) | 43 function Post.get_by_id(id) |
44 local doc = Db.get_document("id:"..id) | 44 local doc = Db.get_document("id:"..id) |
45 return doc and from_doc(doc) | 45 return doc and from_doc(doc) |
46 end | 46 end |
47 | 47 |
48 function M.get_all() | 48 function Post.get_all() |
49 local docs = Db.search("type:post",1,1000,"id desc") | 49 local docs = Db.search("type:post",1,1000,"id desc") |
50 local posts = {} | 50 local posts = {} |
51 for _, doc in ipairs(docs) do | 51 for _, doc in ipairs(docs) do |
52 posts[#posts+1] = from_doc(doc) | 52 posts[#posts+1] = from_doc(doc) |
53 end | 53 end |
54 return posts | 54 return posts |
55 end | 55 end |
56 | 56 |
57 function M.search(query) | 57 function Post.search(query) |
58 query = trim(query) | 58 query = trim(query) |
59 if #query == 0 then | 59 if #query == 0 then |
60 return M.get_all() | 60 return Post.get_all() |
61 end | 61 end |
62 local docs = Db.search(query,1,1000) | 62 local docs = Db.search(query,1,1000) |
63 local posts = {} | 63 local posts = {} |
64 for _, doc in ipairs(docs) do | 64 for _, doc in ipairs(docs) do |
65 posts[#posts+1] = from_doc(doc) | 65 posts[#posts+1] = from_doc(doc) |
66 end | 66 end |
67 return posts | 67 return posts |
68 end | 68 end |
69 | 69 |
70 return M | 70 return Post |