comparison src/add_post.js.luan @ 10:f9e6a4cc4f7d

add Post
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 30 Oct 2024 23:18:45 -0600
parents
children 9f45d32670ae
comparison
equal deleted inserted replaced
9:b8b12fd8be22 10:f9e6a4cc4f7d
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local Time = require "luan:Time.luan"
4 local time_now = Time.now or error()
5 local Io = require "luan:Io.luan"
6 local Http = require "luan:http/Http.luan"
7 local User = require "site:/lib/User.luan"
8 local current_user = User.current or error()
9 local Db = require "site:/lib/Db.luan"
10 local run_in_transaction = Db.run_in_transaction or error()
11 local Chat = require "site:/lib/Chat.luan"
12 local get_chat_by_id = Chat.get_by_id or error()
13 local Post = require "site:/lib/Post.luan"
14 local new_post = Post.new or error()
15
16
17 return function()
18 local user = current_user() or error()
19 local chat = Http.request.parameters.chat or error()
20 local content = Http.request.parameters.content or error()
21 run_in_transaction( function()
22 chat = get_chat_by_id(chat) or error()
23 local now = time_now()
24 local post = new_post{
25 chat_id = chat.id
26 author_id = user.id
27 date = now
28 content = content
29 }
30 post.save()
31 chat.updated = now
32 chat.save()
33 end )
34 end