Skip to content

Instantly share code, notes, and snippets.

@srph
Created August 30, 2014 06:57
Show Gist options
  • Save srph/f4a8d72c1cfb7175286c to your computer and use it in GitHub Desktop.
Save srph/f4a8d72c1cfb7175286c to your computer and use it in GitHub Desktop.
app/
Core/
Common/
UserManagement/
Bindings/
Command/
Exceptions/
Queries/
Repositories/
Transformers/
Validations/
Interface
Service
Provider
ItemManagement/
...
NewsManagement/
...
OtherShitManagement/
...
<?php
namespace Galactus\UserManagement\Queries;
use Galactus\Common\ValidableTrait;
use Galactus\Common\Paginators\PaginatableTrait;
use Galactus\UserManagement\Repositories\UserRepositoryInterface;
use Illuminate\Support\Collection;
use User;
class GetUsers {
use ValidableTrait, PaginatableTrait;
/**
* @var array
*/
protected $rules = array();
/**
* @var UserRepositoryInterface
*/
private $userRepository;
function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* @param $inputs
* @return Collection
*/
function handle(array $inputs)
{
$query = User::where('username', '!=', 'admin');
$this->buildPagination($query, $inputs);
return $this->userRepository->query($query);
}
}
<?php
use Galactus\UserManagement\Transformers\UserTransformer;
use Galactus\UserManagement\UserManagementInterface;
class Api_UsersController extends ApiController {
/**
* @var UserManagementInterface
*/
private $userService;
/**
* @var UserTransformer
*/
private $userTransformer;
function __construct(UserManagementInterface $userService, UserTransformer $userTransformer)
{
$this->userService = $userService;
$this->userTransformer = $userTransformer;
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$inputs = Input::all();
$inputs['limit'] = isset($inputs['limit']) ? $inputs['limit'] : UserTransformer::$limit;
$users = $this->userService->query('GetUsers', $inputs);
return $this->respond($this->userTransformer->transformCollection($users));
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$inputs = Input::only('username', 'password', 'group_id');
$user = $this->userService->execute('AddUser', $inputs);
return $this->respondData($this->userTransformer->transform($user));
}
/**
* Display the specified resource.
*
* @param User $user
* @return Response
*/
public function show(User $user)
{
return $this->userTransformer->transform($user);
}
/**
* Show the form for editing the specified resource.
*
* @param User $user
* @return Response
*/
public function edit(User $user)
{
//
}
/**
* Update the specified resource in storage.
*
* @param User $user
* @return Response
*/
public function update(User $user)
{
$inputs = Input::all();
$user = $this->userService->execute('UpdateUser', $inputs, $user);
return $this->respondData($this->userTransformer->transform($user));
}
/**
* Remove the specified resource from storage.
*
* @param User $user
* @return Response
*/
public function destroy(User $user)
{
$inputs = Input::all();
$this->userService->execute('RemoveUser', $inputs, $user);
return $this->respondOk('User deleted');
}
}
<?php
namespace Galactus\UserManagement\Commands;
use Galactus\Common\ValidableTrait;
use Galactus\UserManagement\Repositories\UserRepositoryInterface;
use User;
class AddUser{
use ValidableTrait;
protected $rules = array(
'username' => array('required'),
'password' => array('required'),
'group_id' => array('required', 'is_group')
);
protected $messages = array(
'is_group' => 'Invalid group'
);
/**
* @var UserRepositoryInterface
*/
private $userRepository;
/**
* @param UserRepositoryInterface $userRepository
*/
function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* @param array $inputs
* @return User
*/
function handle(array $inputs)
{
$user = new User;
$user->fill($inputs);
$this->userRepository->create($user);
return $user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment