Skip to content

Commit

Permalink
Use new Model extender
Browse files Browse the repository at this point in the history
  • Loading branch information
franzliedke committed Apr 24, 2020
1 parent ba637f7 commit a15ff17
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 40 deletions.
11 changes: 9 additions & 2 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
*/

use Flarum\Api\Event\Serializing;
use Flarum\Event\ConfigureModelDates;
use Flarum\Extend;
use Flarum\Flags\Api\Controller\CreateFlagController;
use Flarum\Flags\Api\Controller\DeleteFlagsController;
use Flarum\Flags\Api\Controller\ListFlagsController;
use Flarum\Flags\Flag;
use Flarum\Flags\Listener;
use Flarum\Post\Post;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;

return [
Expand All @@ -29,8 +31,13 @@
->post('/flags', 'flags.create', CreateFlagController::class)
->delete('/posts/{id}/flags', 'flags.delete', DeleteFlagsController::class),

(new Extend\Model(User::class))
->dateAttribute('read_flags_at'),

(new Extend\Model(Post::class))
->hasMany('flags', Flag::class, 'post_id'),

function (Dispatcher $events) {
$events->listen(ConfigureModelDates::class, Listener\AddFlagsApiDates::class);
$events->listen(Serializing::class, Listener\AddFlagsApiAttributes::class);

$events->subscribe(Listener\AddPostFlagsRelationship::class);
Expand Down
23 changes: 0 additions & 23 deletions src/Listener/AddFlagsApiDates.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/Listener/AddPostFlagsRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
use Flarum\Api\Event\WillSerializeData;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Event\GetApiRelationship;
use Flarum\Event\GetModelRelationship;
use Flarum\Flags\Api\Controller\CreateFlagController;
use Flarum\Flags\Api\Serializer\FlagSerializer;
use Flarum\Flags\Flag;
use Flarum\Post\Event\Deleted;
use Flarum\Post\Post;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Eloquent\Collection;

Expand All @@ -30,24 +27,12 @@ class AddPostFlagsRelationship
*/
public function subscribe(Dispatcher $events)
{
$events->listen(GetModelRelationship::class, [$this, 'getModelRelationship']);
$events->listen(Deleted::class, [$this, 'postWasDeleted']);
$events->listen(GetApiRelationship::class, [$this, 'getApiRelationship']);
$events->listen(WillGetData::class, [$this, 'includeFlagsRelationship']);
$events->listen(WillSerializeData::class, [$this, 'prepareApiData']);
}

/**
* @param GetModelRelationship $event
* @return \Illuminate\Database\Eloquent\Relations\HasMany|null
*/
public function getModelRelationship(GetModelRelationship $event)
{
if ($event->isRelationship(Post::class, 'flags')) {
return $event->model->hasMany(Flag::class, 'post_id');
}
}

/**
* @param Deleted $event
*/
Expand Down

0 comments on commit a15ff17

Please sign in to comment.