Skip to content

Instantly share code, notes, and snippets.

@karai17
Last active February 5, 2019 01:38
Show Gist options
  • Save karai17/84f4a881b8cac2d6c1499bafeb1e0dbe to your computer and use it in GitHub Desktop.
Save karai17/84f4a881b8cac2d6c1499bafeb1e0dbe to your computer and use it in GitHub Desktop.
what have i done
--[[ PUT /api/users/1 ]]--
local models = require "models"
local auth = require "utils.auth"
local action = setmetatable({}, require "action_base")
function action.GET(app)
-- verify api user is an admin
return auth.admin(app,
-- forward method
function(_)
-- get user from db
return models.users.get_user(app, app.params.uri_user_id,
-- action method
function(_, user)
user.butt = true
-- set user
return models.users.set_user(app, user) or
-- success
{
status = 200,
json = user
}
end)
end)
end
return action
--[[ utils.auth ]]--
local auth = {}
function auth.admin(app, method)
if app.api_user.auth < 8 then
return {
status = 401,
json = {}
}
end
return type(method) = "function" and method(app) or true
end
return auth
--[[ model.users ]]--
local Users = require("lapis.db.model").Model:extend("users")
function Users.get_user(app, id, method)
local user = Users:get(id)
if not user then
return {
status = 400,
json = {}
}
end
return type(method) = "function" and method(app, user) or user
end
function Users.set_user(app, user, method)
local ok = user:update()
if not ok then
return {
status = 400,
json = {}
}
end
return type(method) = "function" and method(app, user) or user
end
return Users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment