Created
December 7, 2017 15:05
-
-
Save PrinceDavis/81f93ed1a17bdc16b4304a25304483cd 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 | |
namespace App\Http\Controllers; | |
use App\Contracts\ItemRepository; | |
use App\Contracts\UserRepository; | |
use App\Jobs\CreateStockItem; | |
use Illuminate\Http\Request; | |
class StocksController extends Controller | |
{ | |
protected $stockItems; | |
protected $user; | |
public function __construct(ItemRepository $stockItems, UserRepository $user) | |
{ | |
$this->stockItems = $stockItems; | |
$this->user = $user; | |
$this->middleware('auth'); | |
} | |
public function index($userId) | |
{ | |
return $this->createSuccessResponse( | |
$this->stockItems->allForUser($userId) | |
); | |
} | |
function store(Request $request) | |
{ | |
$this->validate($request, [ | |
'label' => 'required', | |
'user_id' => 'required', | |
'quantity' => 'required|numeric', | |
'selling_price' => 'required|numeric' | |
]); | |
dispatch(new CreateStockItem($request->all())); | |
return $this->createSuccessResponse(['created' => true]); | |
} | |
} | |
<?php | |
namespace App\Jobs; | |
use App\Contracts\InventoryRepository; | |
use App\Contracts\ItemRepository; | |
class CreateStockItem extends Job | |
{ | |
public $request; | |
/** | |
* Create a new job instance. | |
* | |
* @return void | |
*/ | |
public function __construct(Array $request) | |
{ | |
$this->request = $request; | |
} | |
/** | |
* Execute the job. | |
* | |
* @return void | |
*/ | |
public function handle(ItemRepository $item, InventoryRepository $inventory) | |
{ | |
$item = $item->create($this->request); | |
$inventory = $inventory->create(array_merge($this->request, ['user_id' => $user->id])); | |
event(new StockItemCreated($item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment