add find by user method in json scheduled node repo
This commit is contained in:
parent
07e34ffd46
commit
f2840a3eb1
1 changed files with 39 additions and 2 deletions
|
|
@ -2,12 +2,19 @@
|
|||
|
||||
namespace App\ScheduledNode;
|
||||
|
||||
use App\Node\NodeRepository;
|
||||
use App\Plan\PlanRepository;
|
||||
use App\User\User;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class JsonScheduledNodeRepository implements ScheduledNodeRepository
|
||||
{
|
||||
private string $filePath;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
public function __construct(
|
||||
private PlanRepository $planRepo,
|
||||
private NodeRepository $nodeRepo,
|
||||
) {
|
||||
$this->filePath = __DIR__ . '/../../data/scheduledNodes.json';
|
||||
}
|
||||
|
||||
|
|
@ -66,4 +73,34 @@ class JsonScheduledNodeRepository implements ScheduledNodeRepository
|
|||
|
||||
return $maxId + 1;
|
||||
}
|
||||
|
||||
public function findByUser(User $user): array
|
||||
{
|
||||
$allScheduledNodes = $this->readScheduledNodes();
|
||||
$planIds = array_map(
|
||||
function ($plan) {
|
||||
return $plan->getId();
|
||||
},
|
||||
$this->planRepo->findByUser($user)
|
||||
);
|
||||
$usersScheduledNodes = array_filter(
|
||||
$allScheduledNodes,
|
||||
function ($node) use ($planIds) {
|
||||
return in_array($node['planId'], $planIds);
|
||||
}
|
||||
);
|
||||
|
||||
return array_map(
|
||||
function ($data) {
|
||||
return new ScheduledNode(
|
||||
id: $data['id'],
|
||||
date: new DateTimeImmutable($data['date']),
|
||||
plan: $this->planRepo->find($data['planId']),
|
||||
node: $this->nodeRepo->find($data['nodeId']),
|
||||
completed: $data['completed']
|
||||
);
|
||||
},
|
||||
$usersScheduledNodes
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue