Skip to content

Commit

Permalink
Merge pull request #74 from mchekin/buying-from-character-store
Browse files Browse the repository at this point in the history
Buying from character store (Basic)
  • Loading branch information
mchekin authored Mar 30, 2021
2 parents 361e0ed + aab6fc6 commit c4c4881
Show file tree
Hide file tree
Showing 52 changed files with 11,865 additions and 2,292 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Homestead.yaml
npm-debug.log
yarn-error.log
/.vagrant
/public/mix-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Modules\Trade\Application\Services\StoreService;
use App\Modules\Trade\Application\Services\ManageStoreService;
use App\Modules\Trade\UI\Http\CommandMappers\ChangeItemPriceCommandMapper;
use App\Modules\Trade\UI\Http\CommandMappers\MoveItemToContainerCommandMapper;
use App\Modules\Trade\UI\Http\CommandMappers\MoveMoneyToContainerCommandMapper;
Expand All @@ -12,14 +12,14 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StoreController extends Controller
class ManageStoreController extends Controller
{
/**
* @var StoreService
* @var ManageStoreService
*/
private $service;

public function __construct(StoreService $service)
public function __construct(ManageStoreService $service)
{
$this->service = $service;
}
Expand Down
65 changes: 65 additions & 0 deletions app/Http/Controllers/Api/TradeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Modules\Trade\Application\Services\TradeService;
use App\Modules\Trade\UI\Http\CommandMappers\BuyItemCommandMapper;
use App\Modules\Trade\UI\Http\CommandMappers\SellItemCommandMapper;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TradeController extends Controller
{
/**
* @var TradeService
*/
private $service;

public function __construct(TradeService $service)
{
$this->service = $service;
}

public function buyItem(Request $request, BuyItemCommandMapper $commandMapper): JsonResponse
{
$command = $commandMapper->map($request);

try {

DB::transaction(function () use ($command) {
$this->service->buyItem($command);
});

} catch (Exception $exception) {

return response()->json([
'message' => 'Error buying item: ' . $exception->getMessage()
], 500);
}

return response()->json(['message' => 'Item bought']);
}

public function sellItem(Request $request, SellItemCommandMapper $commandMapper): JsonResponse
{
$command = $commandMapper->map($request);

try {

DB::transaction(function () use ($command) {
$this->service->sellItem($command);
});

} catch (Exception $exception) {

return response()->json([
'message' => 'Error buying item: ' . $exception->getMessage()
], 500);
}

return response()->json(['message' => 'Item bought']);
}
}
27 changes: 27 additions & 0 deletions app/Http/Controllers/CharacterStoreController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers;

use App\Character;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class CharacterStoreController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('has.character');
}

public function index(Request $request, string $characterId): View
{
/** @var Character $customer */
$customer = $request->user()->character;

/** @var Character $trader */
$trader = Character::query()->findOrFail($characterId);

return view('trade.store.index', compact('customer', 'trader'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
namespace App\Http\Controllers;

use App\Character;
use App\Modules\Trade\Application\Services\StoreService;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;

class StoreController extends Controller
class OwnStoreController extends Controller
{
public function __construct(StoreService $service)
public function __construct()
{
$this->middleware('auth');
$this->middleware('has.character');
}

public function index(Request $request): View
{
/** @var Character $character */
$character = $request->user()->character;

return view('trade.store.index', compact('character'));
return view('trade.own_store.index', compact('character'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use App\Modules\Image\Domain\Image;
use App\Modules\Level\Application\Services\LevelService;
use App\Modules\Trade\Application\Commands\CreateStoreCommand;
use App\Modules\Trade\Application\Services\StoreService;
use App\Modules\Trade\Application\Services\CreateStoreService;
use Illuminate\Support\Facades\DB;

class CharacterService
Expand Down Expand Up @@ -52,7 +52,7 @@ class CharacterService
*/
private $inventoryService;
/**
* @var StoreService
* @var CreateStoreService
*/
private $storeService;

Expand All @@ -63,7 +63,7 @@ public function __construct(
BattleRepositoryInterface $battleRepository,
LevelService $levelService,
InventoryService $inventoryService,
StoreService $storeService
CreateStoreService $storeService
)
{
$this->characterFactory = $characterFactory;
Expand Down
45 changes: 45 additions & 0 deletions app/Modules/Trade/Application/Commands/BuyItemCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Modules\Trade\Application\Commands;

use App\Modules\Character\Domain\CharacterId;
use App\Modules\Equipment\Domain\ItemId;
use App\Modules\Trade\Domain\StoreId;

class BuyItemCommand
{
/**
* @var CharacterId
*/
private $customerId;
/**
* @var StoreId
*/
private $storeId;
/**
* @var ItemId
*/
private $itemId;

public function __construct(CharacterId $customerId, StoreId $storeId, ItemId $itemId)
{
$this->customerId = $customerId;
$this->storeId = $storeId;
$this->itemId = $itemId;
}

public function getCustomerId(): CharacterId
{
return $this->customerId;
}

public function getStoreId(): StoreId
{
return $this->storeId;
}

public function getItemId(): ItemId
{
return $this->itemId;
}
}
45 changes: 45 additions & 0 deletions app/Modules/Trade/Application/Commands/SellItemCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Modules\Trade\Application\Commands;

use App\Modules\Character\Domain\CharacterId;
use App\Modules\Equipment\Domain\ItemId;
use App\Modules\Trade\Domain\StoreId;

class SellItemCommand
{
/**
* @var CharacterId
*/
private $customerId;
/**
* @var StoreId
*/
private $storeId;
/**
* @var ItemId
*/
private $itemId;

public function __construct(CharacterId $customerId, StoreId $storeId, ItemId $itemId)
{
$this->customerId = $customerId;
$this->storeId = $storeId;
$this->itemId = $itemId;
}

public function getCustomerId(): CharacterId
{
return $this->customerId;
}

public function getStoreId(): StoreId
{
return $this->storeId;
}

public function getItemId(): ItemId
{
return $this->itemId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public function nextIdentity(): StoreId;

public function add(Store $store): void;

public function getOne(StoreId $storeId): Store;

public function forCharacter(CharacterId $characterId): Store;

public function update(Store $store): void;
Expand Down
35 changes: 35 additions & 0 deletions app/Modules/Trade/Application/Services/CreateStoreService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);


namespace App\Modules\Trade\Application\Services;

use App\Modules\Equipment\Domain\Money;
use App\Modules\Trade\Application\Commands\CreateStoreCommand;
use App\Modules\Trade\Application\Contracts\StoreRepositoryInterface;
use App\Modules\Trade\Domain\Store;
use App\Modules\Trade\Domain\StoreType;
use Illuminate\Support\Collection;

class CreateStoreService
{
/**
* @var StoreRepositoryInterface
*/
private $storeRepository;

public function __construct(StoreRepositoryInterface $storeRepository)
{
$this->storeRepository = $storeRepository;
}

public function create(CreateStoreCommand $command): Store
{
$id = $this->storeRepository->nextIdentity();

$store = new Store($id, $command->getCharacterId(), StoreType::sellOnly(), Collection::make(), new Money(0));

$this->storeRepository->add($store);

return $store;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@
namespace App\Modules\Trade\Application\Services;

use App\Modules\Equipment\Application\Contracts\InventoryRepositoryInterface;
use App\Modules\Equipment\Domain\Money;
use App\Modules\Equipment\Infrastructure\Repositories\ItemRepository;
use App\Modules\Generic\Domain\Container\ItemNotInContainer;
use App\Modules\Trade\Application\Commands\ChangeItemPriceCommand;
use App\Modules\Trade\Application\Commands\CreateStoreCommand;
use App\Modules\Trade\Application\Commands\MoveItemToContainerCommand;
use App\Modules\Trade\Application\Commands\MoveMoneyToContainerCommand;
use App\Modules\Trade\Application\Contracts\StoreRepositoryInterface;
use App\Modules\Trade\Domain\Store;
use App\Modules\Trade\Domain\StoreType;
use Illuminate\Support\Collection;

class StoreService
class ManageStoreService
{
/**
* @var StoreRepositoryInterface
Expand All @@ -42,17 +37,6 @@ public function __construct(
$this->itemRepository = $itemRepository;
}

public function create(CreateStoreCommand $command): Store
{
$id = $this->storeRepository->nextIdentity();

$store = new Store($id, $command->getCharacterId(), StoreType::sellOnly(), Collection::make(), new Money(0));

$this->storeRepository->add($store);

return $store;
}

public function moveItemToStore(MoveItemToContainerCommand $command): void
{
$inventory = $this->inventoryRepository->forCharacter($command->getCharacterId());
Expand Down
Loading

0 comments on commit c4c4881

Please sign in to comment.