<?php


namespace Mehrab\RolePermission\Repositories;


use Spatie\Permission\Models\Role;

class RoleRepo
{
    public function get()
    {
        return Role::query()->with('permissions')->get();
    }

    public function all()
    {
        return Role::query()->with('permissions')->paginate();
    }

    public function create($request){

        return Role::create(['name' => $request->name])
                    ->givePermissionTo($request->permissions);

    }

    public function findByID($id){
        return Role::findById($id);
    }

    public function update($request,$id)
    {
       $role = $this->findByID($id);
       $role->syncPermissions($request->permissions);
    }

    public function delete($id){
       return  Role::query()
           ->where('id',$id)
           ->where('name','!=',\Mehrab\RolePermission\Models\Role::ROLE_USER)
           ->delete() ? true : false;
    }

}
