implermented key display

This commit is contained in:
Michel Fedde 2024-07-06 20:49:50 +02:00
parent 74e1b25fcf
commit 3218253076
14 changed files with 375 additions and 19 deletions

View file

@ -16,6 +16,16 @@ final class Game
#[ORM\Column]
private string $name;
public function getName(): string
{
return $this->name;
}
public function getId(): ?int
{
return $this->id;
}
/**
* @param string $name
*/

View file

@ -26,6 +26,8 @@ final class Key
private string|null $storeLink;
#[ORM\Column]
private string|null $fromWhere;
#[ORM\Column(type: 'integer', enumType: KeyState::class)]
private KeyState $state;
public function __construct(Game $game, User $contributedUser, string $key, Store $store, ?string $storeLink, ?string $fromWhere)
{
@ -35,5 +37,46 @@ final class Key
$this->store = $store;
$this->storeLink = $storeLink;
$this->fromWhere = $fromWhere;
$this->state = KeyState::AVAILABLE;
}
public function getId(): ?int
{
return $this->id;
}
public function getGame(): Game
{
return $this->game;
}
public function getContributedUser(): User
{
return $this->contributedUser;
}
public function getKey(): string
{
return $this->key;
}
public function getStore(): Store
{
return $this->store;
}
public function getStoreLink(): ?string
{
return $this->storeLink;
}
public function getFromWhere(): ?string
{
return $this->fromWhere;
}
public function getState(): KeyState
{
return $this->state;
}
}

View file

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace GamesShop\Entities\Games;
enum KeyState: int
{
case AVAILABLE = 1;
case UNKNOWN = 0;
case RESERVED_FOR_GIFT = -1;
case CLAIMED = -10;
}

View file

@ -10,5 +10,7 @@ final class DataTablesAPIRoutes
{
public static function setupRoutes(RouteGroup $group): void {
AccountsEndpoint::applyRoutes($group);
$group->get('/keys/provider', ProviderKeysEndpoint::class);
}
}

View file

@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace GamesShop\Routing\Api\DataTables;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Order;
use Doctrine\ORM\EntityManager;
use GamesShop\Entities\Account\User;
use GamesShop\Entities\Games\Game;
use GamesShop\Entities\Games\Key;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
final class ProviderKeysEndpoint
{
public function __construct(
private readonly LoginHandler $loginHandler,
private readonly EntityManager $entityManager,
)
{
}
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
if (!$this->loginHandler->isLoggedIn()) {
throw new UnauthorizedException();
}
$user = $this->loginHandler->getCurrentUser();
if (!$user->getPermission()->hasLevel(UserPermission::PROVIDER)) {
throw new ForbiddenException();
}
$params = $request->getQueryParams();
$draw = $params['draw'];
$start = $params['start'];
$length = $params['length'];
$searchValue = $params['search']['value'];
$repo = $this->entityManager->getRepository(Game::class);
$total = $repo->count();
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->contains('name', $searchValue));
$criteria->setFirstResult((int)$start);
$criteria->setMaxResults((int)$length);
$criteria->orderBy([ 'name' => Order::Ascending ]);
$values = $repo->matching($criteria);
$filteredCount = $values->count();
$entityManager = $this->entityManager;
return new JsonResponse([
'draw' => $draw,
'recordsTotal' => $total,
'recordsFiltered' => $filteredCount,
'data' =>
array_filter($values->map(function (Game $game) use ($entityManager, $user) {
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('game', $game));
$criteria->andWhere(Criteria::expr()->eq('contributedUser', $user));
$criteria->orderBy(['state' => Order::Ascending]);
$keys = $entityManager->getRepository(Key::class)->matching($criteria);
$keysAmount = $keys->count();
if ($keysAmount < 1) {
return null;
}
return [
'gamePicture' => '',
'name' => $game->getName(),
'keysAmount' => $keysAmount,
'igdbState' => 'yet to be implermented',
'keys' => $keys->map(function (Key $key) use ($entityManager) {
return [
'keyState' => $key->getState()->value,
'key' => $key->getKey(),
'store' => $key->getStore()->value,
'store_link' => $key->getStoreLink(),
'from' => $key->getFromWhere(),
];
})->toArray()
];
})->toArray())
]);
}
}