diff 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
line wrap: on
line diff
--- a/src/lib/Post.luan	Tue Nov 05 14:29:16 2024 -0700
+++ b/src/lib/Post.luan	Tue Nov 05 16:09:10 2024 -0700
@@ -4,6 +4,7 @@
 local Number = require "luan:Number.luan"
 local long = Number.long or error()
 local Db = require "site:/lib/Db.luan"
+local run_in_transaction = Db.run_in_transaction or error()
 
 
 local Post = {}
@@ -37,6 +38,13 @@
 		post.id = doc.id
 	end
 
+	function post.delete()
+		run_in_transaction( function()
+			local id = post.id
+			Db.delete("id:"..id)
+		end )
+	end
+
 	return post
 end
 
@@ -50,4 +58,9 @@
 	return posts
 end
 
+function Post.get_by_id(id)
+	local doc = Db.get_document("id:"..id)
+	return doc and doc.type=="post" and from_doc(doc) or nil
+end
+
 return Post