Adds share feature
This commit is contained in:
parent
287c1f67c5
commit
15a9dcf09b
14 changed files with 379 additions and 23 deletions
59
src/php/Routing/Api/Web/SearchForUsers.php
Normal file
59
src/php/Routing/Api/Web/SearchForUsers.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace GamesShop\Routing\Api\Web;
|
||||
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use GamesShop\Entities\Account\User;
|
||||
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 SearchForUsers
|
||||
{
|
||||
public function __construct(
|
||||
private readonly LoginHandler $loginHandler,
|
||||
private readonly EntityManager $entityManager,
|
||||
) { }
|
||||
|
||||
/**
|
||||
* @throws ForbiddenException
|
||||
* @throws UnauthorizedException
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
$searchQuery = $request->getQueryParams()['query'] ?? '';
|
||||
|
||||
$repo = $this->entityManager->getRepository(User::class);
|
||||
|
||||
$criteria = Criteria::create();
|
||||
$criteria->where(Criteria::expr()->contains('name', $searchQuery));
|
||||
$criteria->setMaxResults(10);
|
||||
|
||||
$values = $repo->matching($criteria);
|
||||
return new JsonResponse(
|
||||
$values
|
||||
->filter(fn ($value) => $value !== $user)
|
||||
->map(function (User $user) {
|
||||
return [
|
||||
'value' => $user->getId(),
|
||||
'label' => $user->getName()
|
||||
];
|
||||
})
|
||||
->toArray()
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue