Skip to content

Commit f312459

Browse files
author
Alexander Miertsch
committed
Merge pull request #28 from codeliner/update_user_read_table
Update user view on TodoWasMarkedAsDone
2 parents 8ca6f81 + f034df2 commit f312459

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

config/prooph.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
],
4444
\Prooph\Proophessor\Model\Todo\Event\TodoWasMarkedAsDone::class => [
4545
\Prooph\Proophessor\Projection\Todo\TodoProjector::class,
46+
\Prooph\Proophessor\Projection\User\UserProjector::class,
4647
],
4748
]
4849
]

src/Container/Projection/User/UserProjectorFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Prooph\Proophessor\Container\Projection\User;
1212

1313
use Interop\Container\ContainerInterface;
14+
use Prooph\Proophessor\Projection\User\UserFinder;
1415
use Prooph\Proophessor\Projection\User\UserProjector;
1516

1617
/**
@@ -27,6 +28,9 @@ final class UserProjectorFactory
2728
*/
2829
public function __invoke(ContainerInterface $container)
2930
{
30-
return new UserProjector($container->get('doctrine.connection.default'));
31+
return new UserProjector(
32+
$container->get('doctrine.connection.default'),
33+
$container->get(UserFinder::class)
34+
);
3135
}
3236
}

src/Projection/User/UserFinder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,20 @@ public function findById($userId)
5151
$stmt->execute();
5252
return $stmt->fetch();
5353
}
54+
55+
/**
56+
* @param $todoId
57+
* @return null|\stdClass containing userData
58+
*/
59+
public function findUserOfTodo($todoId)
60+
{
61+
$stmt = $this->connection->prepare(sprintf(
62+
"SELECT u.* FROM %s as u JOIN %s as t ON u.id = t.assignee_id where t.id = :todo_id",
63+
Table::USER,
64+
Table::TODO
65+
));
66+
$stmt->bindValue('todo_id', $todoId);
67+
$stmt->execute();
68+
return $stmt->fetch();
69+
}
5470
}

src/Projection/User/UserProjector.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
namespace Prooph\Proophessor\Projection\User;
1212

13+
use Prooph\Proophessor\Model\Todo\Event\TodoWasMarkedAsDone;
1314
use Prooph\Proophessor\Model\Todo\Event\TodoWasPosted;
1415
use Prooph\Proophessor\Model\User\Event\UserWasRegistered;
1516
use Prooph\Proophessor\Projection\Table;
@@ -28,12 +29,19 @@ final class UserProjector
2829
*/
2930
private $connection;
3031

32+
/**
33+
* @var UserFinder
34+
*/
35+
private $userFinder;
36+
3137
/**
3238
* @param Connection $connection
39+
* @param UserFinder $userFinder
3340
*/
34-
public function __construct(Connection $connection)
41+
public function __construct(Connection $connection, UserFinder $userFinder)
3542
{
3643
$this->connection = $connection;
44+
$this->userFinder = $userFinder;
3745
}
3846

3947
/**
@@ -61,4 +69,28 @@ public function onTodoWasPosted(TodoWasPosted $event)
6169

6270
$stmt->execute();
6371
}
72+
73+
/**
74+
* @param TodoWasMarkedAsDone $event
75+
* @throws \RuntimeException if data of the the assigned user can not be found
76+
*/
77+
public function onTodoWasMarkedAsDone(TodoWasMarkedAsDone $event)
78+
{
79+
$user = $this->userFinder->findUserOfTodo($event->todoId()->toString());
80+
81+
if (! $user) {
82+
throw new \RuntimeException(
83+
sprintf(
84+
"Data of the assigned user of the todo %s cannot be found",
85+
$event->todoId()->toString()
86+
)
87+
);
88+
}
89+
90+
$stmt = $this->connection->prepare(sprintf('UPDATE %s SET open_todos = open_todos - 1, done_todos = done_todos + 1 WHERE id = :assignee_id', Table::USER));
91+
92+
$stmt->bindValue('assignee_id', $user->id);
93+
94+
$stmt->execute();
95+
}
6496
}

0 commit comments

Comments
 (0)