Last active
February 5, 2019 01:38
-
-
Save karai17/84f4a881b8cac2d6c1499bafeb1e0dbe to your computer and use it in GitHub Desktop.
what have i done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --[[ 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