comparison src/lib/Post.luan @ 23:c54c806fcc6e

add delete_post
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Nov 2024 16:09:10 -0700
parents f9e6a4cc4f7d
children af41be2dcdec
comparison
equal deleted inserted replaced
22:949914955bfe 23:c54c806fcc6e
2 local error = Luan.error 2 local error = Luan.error
3 local ipairs = Luan.ipairs or error() 3 local ipairs = Luan.ipairs or error()
4 local Number = require "luan:Number.luan" 4 local Number = require "luan:Number.luan"
5 local long = Number.long or error() 5 local long = Number.long or error()
6 local Db = require "site:/lib/Db.luan" 6 local Db = require "site:/lib/Db.luan"
7 local run_in_transaction = Db.run_in_transaction or error()
7 8
8 9
9 local Post = {} 10 local Post = {}
10 11
11 local function from_doc(doc) 12 local function from_doc(doc)
35 local doc = to_doc(post) 36 local doc = to_doc(post)
36 Db.save(doc) 37 Db.save(doc)
37 post.id = doc.id 38 post.id = doc.id
38 end 39 end
39 40
41 function post.delete()
42 run_in_transaction( function()
43 local id = post.id
44 Db.delete("id:"..id)
45 end )
46 end
47
40 return post 48 return post
41 end 49 end
42 50
43 function Post.search(query,sort,rows) 51 function Post.search(query,sort,rows)
44 rows = rows or 1000000 52 rows = rows or 1000000
48 posts[#posts+1] = from_doc(doc) 56 posts[#posts+1] = from_doc(doc)
49 end 57 end
50 return posts 58 return posts
51 end 59 end
52 60
61 function Post.get_by_id(id)
62 local doc = Db.get_document("id:"..id)
63 return doc and doc.type=="post" and from_doc(doc) or nil
64 end
65
53 return Post 66 return Post