<commit_before><?php

namespace Zhiyi\Plus\Http\Controllers\Admin;

use Zhiyi\Plus\Models\Role;
use Illuminate\Http\Request;
use Zhiyi\Plus\Models\Permission;
use Zhiyi\Plus\Http\Controllers\Controller;

class RoleController extends Controller
{
    /**
     * Get roles.
     *
     * @return mixed
     *
     * @author <NAME> <<EMAIL>>
     */
    public function roles()
    {
        $roles = Role::all();

        return response()->json($roles)->setStatusCode(200);
    }

    /**
     * 删除用户组.
     *
     * @param Role $role
     *
     * @return mixed
     *
     * @author <NAME> <<EMAIL>>
     */
    public function delete(Role $role)
    {
        if ($role->delete()) {
            return response('', 204);
        }

        return response()->json([
            'errors' => ['删除失败'],
        ])->setStatusCode(500);
    }

    /**
     * 获取全部权限节点.
     *
     * @return mixed
     *
     * @author <NAME> <<EMAIL>>
     */
    public function perms()
    {
        $perms = Permission::all();

        return response()->json($perms)->setStatusCode(200);
    }

    public function createPerm(Request $request)
    {
        $name = $request->input('name');
        $display_name = $request->input('display_name');
        $description = $request->input('description');

        if (! $name) {
            return response()->json([
                'errors' => ['name' => '名称不能为空'],
            ])->setStatusCode(422);
        } elseif (Permission::where('name', 'LIKE', $name)->first()) {
            return response()->json([
                'errors' => ['name' => '名称已经存在'],
            ])->setStatusCode(422);
        }

        $perm = new Permission();
        $perm->name = $name;
        $perm->display_name = $display_name;
        $perm->description = $description;

        if (! $perm->save()) {
            return response()->json([
                'errors' => ['保存失败'],
            ])->setStatusCode(400);
        }

        return response()->json($perm)->setStatusCode(201);
    }

    /**
     * 更新权限节点.
     *
     * @param Request    $request
     * @param Permission $perm
     *
     * @return mixed
     *
     * @author <NAME> <<EMAIL>>
     */
    public function updatePerm(Request $request, Permission $perm)
    {
        $key = $request->input('key');
        $value = $request->input('value');

        if (! in_array($key, ['display_name', 'description'])) {
            return response()->json([
                'errors' => ['请求不合法'],
            ])->setStatusCode(422);
        }

        $perm->$key = $value;

        if (! $perm->save()) {
            return response()->json([
                'errors' => ['数据更新失败'],
            ])->setStatusCode(500);
        }

        return response()->json([
            'messages' => ['更新成功'],
        ])->setStatusCode(201);
    }

    public function deletePerm(Permission $perm)
    {
        if ($perm->delete()) {
            return response('', 204);
        }

        return response()->json([
            'errors' => ['删除失败'],
        ])->setStatusCode(500);
    }
}
<commit_msg>更新注释文档～<commit_after><?php

namespace Zhiyi\Plus\Http\Controllers\Admin;

use Zhiyi\Plus\Models\Role;
use Illuminate\Http\Request;
use Zhiyi\Plus\Models\Permission;
use Zhiyi\Plus\Http\Controllers\Controller;

class RoleController extends Controller
{
    /**
     * Get roles.
     *
     * @return mixed
     *
     * @author <NAME> <<EMAIL>>
     */
    public function roles()
    {
        $roles = Role::all();

        return response()->json($roles)->setStatusCode(200);
    }

    /**
     * 删除用户组.
     *
     * @param Role $role
     *
     * @return mixed
     *
     * @author <NAME> <<EMAIL>>
     */
    public function delete(Role $role)
    {
        if ($role->delete()) {
            return response('', 204);
        }

        return response()->json([
            'errors' => ['删除失败'],
        ])->setStatusCode(500);
    }

    /**
     * 获取全部权限节点.
     *
     * @return mixed
     *
     * @author <NAME> <<EMAIL>>
     */
    public function perms()
    {
        $perms = Permission::all();

        return response()->json($perms)->setStatusCode(200);
    }

    /**
     * 创建权限节点.
     *
     * @param Request $request
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function createPerm(Request $request)
    {
        $name = $request->input('name');
        $display_name = $request->input('display_name');
        $description = $request->input('description');

        if (! $name) {
            return response()->json([
                'errors' => ['name' => '名称不能为空'],
            ])->setStatusCode(422);
        } elseif (Permission::where('name', 'LIKE', $name)->first()) {
            return response()->json([
                'errors' => ['name' => '名称已经存在'],
            ])->setStatusCode(422);
        }

        $perm = new Permission();
        $perm->name = $name;
        $perm->display_name = $display_name;
        $perm->description = $description;

        if (! $perm->save()) {
            return response()->json([
                'errors' => ['保存失败'],
            ])->setStatusCode(400);
        }

        return response()->json($perm)->setStatusCode(201);
    }

    /**
     * 更新权限节点.
     *
     * @param Request    $request
     * @param Permission $perm
     *
     * @return mixed
     *
     * @author <NAME> <<EMAIL>>
     */
    public function updatePerm(Request $request, Permission $perm)
    {
        $key = $request->input('key');
        $value = $request->input('value');

        if (! in_array($key, ['display_name', 'description'])) {
            return response()->json([
                'errors' => ['请求不合法'],
            ])->setStatusCode(422);
        }

        $perm->$key = $value;

        if (! $perm->save()) {
            return response()->json([
                'errors' => ['数据更新失败'],
            ])->setStatusCode(500);
        }

        return response()->json([
            'messages' => ['更新成功'],
        ])->setStatusCode(201);
    }

    /**
     * 删除权限节点.
     *
     * @param Permission $perm
     * @return mixed
     * @author <NAME> <<EMAIL>>
     */
    public function deletePerm(Permission $perm)
    {
        if ($perm->delete()) {
            return response('', 204);
        }

        return response()->json([
            'errors' => ['删除失败'],
        ])->setStatusCode(500);
    }
}

{'fraction_non_alphanumeric': 0.17859375, 'fraction_numerical': 0.0121875, 'mean_word_length': 1.7531182795698925, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3304273', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1997, 'n_words': 358}