implemented user lists

This commit is contained in:
Michel Fedde 2024-07-06 22:18:37 +02:00
parent 3218253076
commit c3e81ce6ea
16 changed files with 365 additions and 74 deletions

View file

@ -9,9 +9,11 @@ use Doctrine\ORM\EntityManager;
use GamesShop\Entities\Account\User;
use GamesShop\Entities\Games\Game;
use GamesShop\Entities\Games\Key;
use GamesShop\Entities\GamesList;
use GamesShop\Login\LoginHandler;
use GamesShop\Login\UserPermission;
use Laminas\Diactoros\Response\JsonResponse;
use League\Route\Http\Exception\BadRequestException;
use League\Route\Http\Exception\ForbiddenException;
use League\Route\Http\Exception\UnauthorizedException;
use Psr\Http\Message\ResponseInterface;
@ -37,62 +39,40 @@ final class ProviderKeysEndpoint
throw new ForbiddenException();
}
$body = $request->getQueryParams();
if (!array_key_exists('listid', $body)) {
throw new BadRequestException();
}
$params = $request->getQueryParams();
$draw = $params['draw'];
$start = $params['start'];
$length = $params['length'];
$list = $this->entityManager->getRepository(GamesList::class)->findOneBy([ 'owner' => $user, 'id' => $body['listid'] ]);
if (!$list instanceof GamesList) {
throw new BadRequestException();
}
$searchValue = $params['search']['value'];
$keys = $this->entityManager->getRepository(Key::class)->findBy(['list' => $list]);
$gameToKeyArray = [];
foreach ($keys as $key) {
$game = $key->getGame();
$id = $game->getId();
$repo = $this->entityManager->getRepository(Game::class);
$total = $repo->count();
if (!array_key_exists($id, $gameToKeyArray)) {
$gameToKeyArray[$id] = [ $game, [] ];
}
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->contains('name', $searchValue));
$criteria->setFirstResult((int)$start);
$criteria->setMaxResults((int)$length);
$criteria->orderBy([ 'name' => Order::Ascending ]);
$gameToKeyArray[$id][1][] = $key;
}
$values = $repo->matching($criteria);
$filteredCount = $values->count();
$result = [];
foreach ($gameToKeyArray as [$game, $keys]) {
$result[] = [
'gamePicture' => '',
'name' => $game->getName(),
'keysAmount' => count($keys),
'igdbState' => 'not implermented',
'keys' => $keys,
];
}
$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())
]);
return new JsonResponse([ 'data' => $result ]);
}
}