add find by user method in plan repo
This commit is contained in:
parent
ec4dca87a6
commit
07e34ffd46
3 changed files with 55 additions and 0 deletions
|
|
@ -90,4 +90,29 @@ class JsonPlanRepository implements PlanRepository
|
|||
|
||||
return $maxId + 1;
|
||||
}
|
||||
|
||||
public function findByUser(User $user): array
|
||||
{
|
||||
$plans = array_filter(
|
||||
$this->readPlans(),
|
||||
function ($data) use ($user) {
|
||||
return $data['userId'] === $user->getId();
|
||||
}
|
||||
);
|
||||
|
||||
return array_map(
|
||||
function ($data) {
|
||||
$user = $this->userRepository->find($data['userId']);
|
||||
if ($user === null) {
|
||||
return null;
|
||||
}
|
||||
return new Plan(
|
||||
id: $data['id'],
|
||||
name: $data['name'],
|
||||
user: $user,
|
||||
);
|
||||
},
|
||||
$plans
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,14 @@
|
|||
|
||||
namespace App\Plan;
|
||||
|
||||
use App\User\User;
|
||||
|
||||
interface PlanRepository
|
||||
{
|
||||
public function create(CreatePlanDto $dto): Plan;
|
||||
public function find(int $id): ?Plan;
|
||||
/**
|
||||
* @return Plan[]
|
||||
*/
|
||||
public function findByUser(User $user): array;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,13 @@ namespace Tests\Fakes;
|
|||
use App\Plan\CreatePlanDto;
|
||||
use App\Plan\Plan;
|
||||
use App\Plan\PlanRepository;
|
||||
use App\User\User;
|
||||
|
||||
class FakePlanRepository implements PlanRepository
|
||||
{
|
||||
/**
|
||||
* @var Plan[]
|
||||
*/
|
||||
private array $existingPlans = [];
|
||||
|
||||
public function create(CreatePlanDto $dto): Plan
|
||||
|
|
@ -37,4 +41,24 @@ class FakePlanRepository implements PlanRepository
|
|||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function findByUser(User $user): array
|
||||
{
|
||||
$plans = array_filter(
|
||||
$this->existingPlans,
|
||||
function (Plan $plan) use ($user) {
|
||||
return $plan->getUser()->getId() === $user->getId();
|
||||
}
|
||||
);
|
||||
return array_map(
|
||||
function (Plan $plan) {
|
||||
return new Plan(
|
||||
id: $plan->getId(),
|
||||
name: $plan->getName(),
|
||||
user: $plan->getUser(),
|
||||
);
|
||||
},
|
||||
$plans
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue