add news support, work out some form handling details

This commit is contained in:
Sky Johnson 2025-06-04 11:11:06 -05:00
parent 54819ec21d
commit 043b74e25a
8 changed files with 52 additions and 3 deletions

View File

@ -1,4 +1,8 @@
<h1>Config</h1> {{ if has_flash_success() then }}
<div class="ui alert">
{{ get_flash_success() }}
</div>
{{ end }}
<form action="/admin/config" method="post"> <form action="/admin/config" method="post">
<div> <div>

View File

@ -0,0 +1,27 @@
<p>Here you can write a new news article. This will replace the current one on town pages.</p>
<form action="/admin/news" method="post" id="news_form">
<textarea name="content" style="display: block; width: 100%; min-height: 240px;"></textarea>
<div>
<label for="author">Author</label>
<input type="text" name="author" id="author" value="{{ username }}">
</div>
<button type="submit" class="ui button primary">Submit</button>
</form>
<script>
$('#news_form').on('submit', function(e) {
e.preventDefault()
$('#main .ui.alert').remove()
$.post('/admin/news', $(this).serialize())
.done(function(data) {
const alertClass = data.success ? 'success' : 'failure'
$('#main').prepend(`<div class="ui alert ${alertClass}">${data.message}</div>`)
})
.fail(function() {
$('#main').prepend('<div class="ui alert failure">Request failed</div>')
})
})
</script>

BIN
moonshark

Binary file not shown.

View File

@ -2,7 +2,6 @@
cursor: pointer; cursor: pointer;
display: inline-block; display: inline-block;
border: none; border: none;
font-size: 1rem;
background: #f7f8fa linear-gradient(rgba(255, 255, 255, 0), rgba(0, 0, 0, 0.1)); background: #f7f8fa linear-gradient(rgba(255, 255, 255, 0), rgba(0, 0, 0, 0.1));
box-shadow: 0 1px 0 1px rgba(255, 255, 255, 0.3) inset, 0 0 0 1px #adb2bb inset; box-shadow: 0 1px 0 1px rgba(255, 255, 255, 0.3) inset, 0 0 0 1px #adb2bb inset;
color: #111111; color: #111111;

View File

@ -4,4 +4,5 @@ else
env_set("game_open", false) env_set("game_open", false)
end end
http_redirect("/admin/config") redirect_with_success("/admin/config", "Config saved successfully")
--http_redirect("/admin/config")

View File

@ -0,0 +1,5 @@
local a = require("admin")
local d = sqlite("dk"):get_one("SELECT username FROM users WHERE id = :i", {i = session_get("user_id")})
return a.page("news.html", "News", {username = d.username})

View File

@ -0,0 +1,12 @@
local content = string.trim(ctx.form.content)
if content == "" then return { success = 0, message = "Content is required" } end
local author = string.trim(ctx.form.author)
if author == "" then return { success = 0, message = "Author is required" } end
sqlite("dk"):insert("news", {
content = content,
author = author
})
return { success = 1, message = "News posted successfully" }

View File

@ -140,6 +140,7 @@ db:create_table("monsters",
db:create_table("news", db:create_table("news",
"id INTEGER PRIMARY KEY AUTOINCREMENT", "id INTEGER PRIMARY KEY AUTOINCREMENT",
"posted INTEGER NOT NULL DEFAULT (unixepoch())", "posted INTEGER NOT NULL DEFAULT (unixepoch())",
"type INTEGER NOT NULL DEFAULT 0",
"author TEXT NOT NULL DEFAULT 'Guild Master'", "author TEXT NOT NULL DEFAULT 'Guild Master'",
"content TEXT NOT NULL" "content TEXT NOT NULL"
) )