comparison blog/src/lib/Post.luan @ 596:6bb0c83116e9

add blog sample app
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 15 Sep 2015 21:30:33 -0600
parents
children 50540f0813e2
comparison
equal deleted inserted replaced
595:8370c4009cce 596:6bb0c83116e9
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()
7 local Db = require "site:/lib/Db"
8
9
10 local M = {}
11
12 local function from_doc(doc)
13 return M.new {
14 id = doc.id;
15 subject = doc.subject;
16 content = doc.content;
17 date = doc.date;
18 }
19 end
20
21 function M.new(this)
22 assert_string(this.subject)
23 assert_string(this.content)
24 this.date = this.date or now()
25
26 function this.save()
27 local doc = {
28 type = "post";
29 id = this.id;
30 subject = this.subject;
31 content = this.content;
32 date = this.date;
33 }
34 Db.save(doc)
35 this.id = doc.id
36 end
37
38 return this
39 end
40
41 function M.get_by_id(id)
42 local doc = Db.get_document("id:"..id)
43 return doc and from_doc(doc)
44 end
45
46 function M.get_all()
47 local docs = Db.search("type:post",1,1000,"id desc")
48 local posts = {}
49 for _, doc in ipairs(docs) do
50 posts[#posts+1] = from_doc(doc)
51 end
52 return posts
53 end
54
55 return M