Move trip and operator repository to fluent pattern
This commit is contained in:
parent
847e3a078f
commit
950e310096
@ -4,6 +4,7 @@ namespace App\Controller\Api\v1;
|
||||
|
||||
use App\Controller\Controller;
|
||||
use App\Model\Trip;
|
||||
use App\Modifier\IdFilter;
|
||||
use App\Provider\TripRepository;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
@ -18,7 +19,7 @@ class TripController extends Controller
|
||||
*/
|
||||
public function one($id, TripRepository $repository)
|
||||
{
|
||||
$trip = $repository->getById($id);
|
||||
$trip = $repository->all(new IdFilter($id));
|
||||
|
||||
return $this->json($trip, Response::HTTP_OK, [], $this->serializerContextFactory->create(Trip::class));
|
||||
}
|
||||
|
@ -2,37 +2,26 @@
|
||||
|
||||
namespace App\Provider\Database;
|
||||
|
||||
use App\Entity\OperatorEntity;
|
||||
use App\Model\Operator;
|
||||
use App\Modifier\Modifier;
|
||||
use App\Provider\OperatorRepository;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class GenericOperatorRepository extends DatabaseRepository implements OperatorRepository
|
||||
{
|
||||
public function getAll(): Collection
|
||||
public function all(Modifier ...$modifiers): Collection
|
||||
{
|
||||
$repository = $this->em->getRepository(Operator::class);
|
||||
$operators = $repository->findAll();
|
||||
$builder = $this->em
|
||||
->createQueryBuilder()
|
||||
->from(OperatorEntity::class, 'operator')
|
||||
->select('operator')
|
||||
;
|
||||
|
||||
return collect($operators);
|
||||
}
|
||||
|
||||
public function getById($id): ?Operator
|
||||
{
|
||||
$repository = $this->em->getRepository(Operator::class);
|
||||
|
||||
return $repository->find($id);
|
||||
}
|
||||
|
||||
public function getManyById($ids): Collection
|
||||
{
|
||||
$repository = $this->em->getRepository(Operator::class);
|
||||
$operators = $repository->findBy(['id' => $ids]);
|
||||
|
||||
return collect($operators);
|
||||
}
|
||||
|
||||
protected static function getHandlers()
|
||||
{
|
||||
return [];
|
||||
return $this->allFromQueryBuilder($builder, $modifiers, [
|
||||
'alias' => 'operator',
|
||||
'entity' => OperatorEntity::class,
|
||||
'type' => Operator::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -4,30 +4,25 @@ namespace App\Provider\Database;
|
||||
|
||||
use App\Entity\TripEntity;
|
||||
use App\Model\Trip;
|
||||
use App\Modifier\Modifier;
|
||||
use App\Provider\TripRepository;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class GenericTripRepository extends DatabaseRepository implements TripRepository
|
||||
{
|
||||
public function getById(string $id): Trip
|
||||
public function all(Modifier ...$modifiers): Collection
|
||||
{
|
||||
$id = $this->id->generate($this->provider, $id);
|
||||
|
||||
$trip = $this->em
|
||||
$builder = $this->em
|
||||
->createQueryBuilder()
|
||||
->from(TripEntity::class, 't')
|
||||
->join('t.stops', 'ts')
|
||||
->from(TripEntity::class, 'trip')
|
||||
->join('trip.stops', 'ts')
|
||||
->join('ts.stop', 's')
|
||||
->select('t', 'ts')
|
||||
->where('t.id = :id')
|
||||
->getQuery()
|
||||
->setParameter('id', $id)
|
||||
->getOneOrNullResult();
|
||||
->select('t', 'ts');
|
||||
|
||||
return $this->convert($trip);
|
||||
}
|
||||
|
||||
protected static function getHandlers()
|
||||
{
|
||||
return [];
|
||||
return $this->allFromQueryBuilder($builder, $modifiers, [
|
||||
'alias' => 'operator',
|
||||
'entity' => TripEntity::class,
|
||||
'type' => Trip::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,6 @@ namespace App\Provider;
|
||||
use App\Model\Operator;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
interface OperatorRepository
|
||||
interface OperatorRepository extends FluentRepository
|
||||
{
|
||||
public function getAll(): Collection;
|
||||
public function getById($id): ?Operator;
|
||||
public function getManyById($ids): Collection;
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ namespace App\Provider;
|
||||
|
||||
use App\Model\Trip;
|
||||
|
||||
interface TripRepository
|
||||
interface TripRepository extends FluentRepository
|
||||
{
|
||||
public function getById(string $id): Trip;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user