add sorting topics view
This commit is contained in:
35
apps/mod.lua
35
apps/mod.lua
@ -1,23 +1,46 @@
|
||||
local app = require("lapis").Application()
|
||||
|
||||
local db = require("lapis.db")
|
||||
|
||||
local util = require("util")
|
||||
|
||||
local models = require("models")
|
||||
local Users = models.Users
|
||||
|
||||
app:get("user_list", "/list", function(self)
|
||||
-- everything here requires a logged in moderator
|
||||
app:before_filter(function(self)
|
||||
self.me = util.get_logged_in_user(self)
|
||||
if not self.me then
|
||||
return {redirect_to = self:url_for("all_topics")}
|
||||
self:write{redirect_to = self:url_for("all_topics")}
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
if not self.me:is_mod() then
|
||||
return {redirect_to = self:url_for("all_topics")}
|
||||
self:write{redirect_to = self:url_for("all_topics")}
|
||||
return
|
||||
end
|
||||
|
||||
end)
|
||||
|
||||
app:get("user_list", "/list", function(self)
|
||||
self.users = Users:select("")
|
||||
|
||||
|
||||
return {render = "mod.user-list"}
|
||||
end)
|
||||
|
||||
app:get("sort_topics", "/sort-topics", function(self)
|
||||
self.topics = db.query("SELECT * FROM topics ORDER BY sort_order ASC")
|
||||
self.page_title = "sorting topics"
|
||||
return {render = "mod.sort-topics"}
|
||||
end)
|
||||
|
||||
app:post("sort_topics", "/sort-topics", function(self)
|
||||
local updates = self.params
|
||||
db.query("BEGIN")
|
||||
for topic_id, new_order in pairs(updates) do
|
||||
db.update("topics", {sort_order = new_order}, {id = topic_id})
|
||||
end
|
||||
db.query("COMMIT")
|
||||
return {redirect_to = self:url_for("sort_topics")}
|
||||
end)
|
||||
|
||||
return app
|
||||
|
Reference in New Issue
Block a user