Last active
June 9, 2016 11:57
-
-
Save Tjoosten/43ebf12ff689a674c55a386e1a233518 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
// error: | |
// Call to a member function withPaginator() on a non-object | |
namespace Idevelopment\Timecontrol\Api\Controllers; | |
use App\User; | |
use App\Http\Requests\accountManagementValidator; | |
use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController; | |
use Idevelopment\Timecontrol\Api\Transformers\StaffTransformer; | |
use Illuminate\Http\Request; | |
/** | |
* Class StaffController | |
* @package Idevelopment\Timecontrol\Api\Controllers | |
*/ | |
class StaffController extends ApiGuardController | |
{ | |
// TODO: Finish the employee transformer. | |
// TODO: Add phpunit tests. | |
/** | |
* Employee relations. | |
* @var array | |
*/ | |
protected $relations; | |
/** | |
* StaffController constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->relations = ['departmentManager', 'teams']; | |
} | |
/** | |
* Display all the employee. | |
* | |
* @return mixed | |
*/ | |
public function all() | |
{ | |
try { | |
$employees = User::with($this->relations)->paginate(15); | |
return $this->response->withPaginator($employees, new StaffTransformer); | |
} catch(ModelNotFoundException $e) { | |
return $this->response->errorNotFound(); | |
} | |
} | |
/** | |
* Show a apecific employee. | |
* | |
* @param int $id the id of the staff member. | |
* @return mixed | |
*/ | |
public function specific($id) | |
{ | |
try { | |
$user = User::with($this->relations)->findOrfail($id); | |
if (count($user) === 0) { | |
return $this->response->errorNotFound('No employee with this id found'); | |
} | |
return $this->response->withItem($user, new StaffTransformer); | |
} catch (ModelNotFoundException $e) { | |
return $this->response->errorNotFound(); | |
} | |
} | |
/** | |
* Create a ew employee. | |
* | |
* @param Requests\accountManagementValidator $input | |
* @return mixed | |
*/ | |
public function create(accountManagementValidator $input) | |
{ | |
try { | |
} catch(ModelNotFoundException $e) { | |
return $this->response->errorNotFound(); | |
} | |
} | |
/** | |
* Update a employee. | |
* | |
* @param App\user $user the employee database model. | |
* @param Requests\accountManagementValidator $input | |
* @param Int $id the id of the staff member. | |
* @return Mixed | |
*/ | |
public function update(User $user, accountManagementValidator $input, $id) | |
{ | |
try { | |
$user->find($id); | |
$user->update($input->except('_token')); | |
} catch(ModelNotFoundException $e) { | |
return $this->response->errorNotFound(); | |
} | |
} | |
/** | |
* Destroy a employee out off timecontrol. | |
* | |
* @param User $user User database model instance. | |
* @param int $id The id of the staff member. | |
* @return mixex | |
*/ | |
public function destroy(User $user, $id) | |
{ | |
try { | |
$user->find($id); | |
$user->teams()->sync([]); | |
} catch(ModelNotFoundException $e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment