Skip to content

Commit

Permalink
feat: Use EventProvider in all getters in EventController
Browse files Browse the repository at this point in the history
  • Loading branch information
kachnitel committed Feb 8, 2020
1 parent b61732b commit b0a27aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
29 changes: 14 additions & 15 deletions src/API/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public function add(Request $request, Response $response, array $args): Response
*/
public function get(Request $request, Response $response, array $args): Response
{
$provider = new EventProvider($this->getEventRepository());
$provider->setUser($request->getAttribute('currentUser'));
$event = $provider->get($args['id']);
$event = $this->getEvent($args['id'], $request->getAttribute('currentUser'));

return $response->withJson((object) [
'result' => $event->getDetail(),
Expand Down Expand Up @@ -162,8 +160,8 @@ protected function listOwnEvents(string $status, User $user): array
public function invite(Request $request, Response $response, array $args): Response
{
$currentUser = $request->getAttribute('currentUser');
/** @var Event $event */
$event = $this->getEventRepository()->get($args['id']);

$event = $this->getEvent($args['id'], $currentUser);

/** @var User $user */
$user = $this->getUserRepository()
Expand Down Expand Up @@ -202,8 +200,8 @@ public function join(Request $request, Response $response, array $args): Respons
{
/** @var User $currentUser */
$currentUser = $request->getAttribute('currentUser');
/** @var Event $event */
$event = $this->getEventRepository()->get($args['id']);

$event = $this->getEvent($args['id'], $currentUser);

$membership = $this->getMembershipManager()->join($event, $currentUser);
$this->getEventRepository()->saveEntity($membership);
Expand Down Expand Up @@ -341,8 +339,7 @@ public function addComment(Request $request, Response $response, array $args): R
{
/** @var User $currentUser */
$currentUser = $request->getAttribute('currentUser');
/** @var Event $event */
$event = $this->getEventRepository()->get($args['id']);
$event = $this->getEvent($args['id'], $currentUser);

if (!$event->isMember($currentUser)) {
throw new UserException(
Expand Down Expand Up @@ -389,19 +386,14 @@ public function getComments(Request $request, Response $response, array $args):
$provider->setUser($request->getAttribute('currentUser'));
$event = $provider->get($args['id']);

if (!$event->isVisible($request->getAttribute('currentUser'))) {
throw new UserException("Event {$args['id']} is not visible to current user", 403);
}

return $response->withJson((object) [
'results' => $this->extractDetails($event->getComments()->getValues())
]);
}

public function getEventRequests(Request $request, Response $response, array $args): Response
{
/** @var Event $event */
$event = $this->getEventRepository()->get($args['id']);
$event = $this->getEvent($args['id'], $request->getAttribute('currentUser'));

if (!$event->isMember($request->getAttribute('currentUser'))) {
throw new UserException("Current user is not a member of {$args['id']}", 403);
Expand Down Expand Up @@ -445,4 +437,11 @@ protected function getMembershipManager(): MembershipManager
{
return new MembershipManager();
}

protected function getEvent(int $id, User $currentUser): Event
{
$provider = new EventProvider($this->getEventRepository());
$provider->setUser($currentUser);
return $provider->get($id);
}
}
10 changes: 0 additions & 10 deletions src/API/Providers/EventProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
use RideTimeServer\Exception\RTException;
use RideTimeServer\Exception\UserException;

/**
* TODO:
* + change list in ctrlr to use filter
* - deprecate list route and use new filter in app update
* + change get and filter in ctrlr to use provider
* + use id in ALL relationship lists
* - use provider in ctrlr to fetch related if needed
* + location
* - user (getRelated!)
*/
class EventProvider
{
/**
Expand Down

0 comments on commit b0a27aa

Please sign in to comment.