Skip to content

Commit

Permalink
feat(activity): store when created items
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Groza committed Oct 13, 2019
1 parent 68a5a0d commit 6a6b696
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 1 deletion.
18 changes: 18 additions & 0 deletions app/Activity.php
@@ -0,0 +1,18 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Activity extends Model
{
protected $fillable = [
'user_id',
'type',
];

public function subject()
{
return $this->morphTo();
}
}
11 changes: 10 additions & 1 deletion app/Http/Controllers/UserChapterController.php
Expand Up @@ -10,7 +10,16 @@ class UserChapterController extends Controller
public function store(SaveChapterRequest $request, User $user)
{
//TODO Добавить guard, авторизованный польтзователь может изменять только свой список глав
$user->chapters()->sync($request->get('chapters_id', []));
$completeChapters = $request->get('chapters_id', []);

$user->chapters()->detach();

foreach ($completeChapters as $chapterId) {
$user->readChapters()->create([
'chapter_id' => $chapterId
]);
}


return redirect(route('my'));
}
Expand Down
5 changes: 5 additions & 0 deletions app/ReadChapter.php
Expand Up @@ -2,9 +2,14 @@

namespace App;

use App\Traits\RecordActivity;
use Illuminate\Database\Eloquent\Model;

class ReadChapter extends Model
{
use RecordActivity;

protected $fillable = [
'chapter_id',
];
}
51 changes: 51 additions & 0 deletions app/Traits/RecordActivity.php
@@ -0,0 +1,51 @@
<?php

namespace App\Traits;

use App\Activity;
use Illuminate\Support\Facades\Auth;
use ReflectionClass;

trait RecordActivity
{
protected static function bootRecordActivity()
{
foreach (static::getEventsToRecord() as $event) {
static::$event(function ($model) use ($event) {
//TODO Проверка есть ли уже активность по этой главе
$model->recordActivity($event);
});
}
}

protected static function getEventsToRecord()
{
return ['created'];
}

public function recordActivity($event)
{
$this->activity()->create([
'user_id' => Auth::id(),
'type' => "{$event}_{$this->getTypeName()}",
]);
}

/**
* @return string
* @throws \ReflectionException
*/
protected function getTypeName()
{
try {
return strtolower((new ReflectionClass($this))->getShortName());
} catch (\ReflectionException $e) {
throw $e;
}
}

public function activity()
{
return $this->morphMany(Activity::class, 'subject');
}
}
35 changes: 35 additions & 0 deletions database/migrations/2019_10_12_084709_create_activities_table.php
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateActivitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('activities', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('user_id')->index();
$table->unsignedInteger('subject_id')->index();
$table->string('subject_type', 50);
$table->string('type', 50);
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('activities');
}
}
1 change: 1 addition & 0 deletions tests/Feature/Http/Controllers/RatingControllerTest.php
Expand Up @@ -13,6 +13,7 @@ public function testIndex()
factory(User::class, 10)
->create()
->each(function ($user) {
$this->actingAs($user);
factory(ReadChapter::class, mt_rand(0, 10))->create([
'user_id' => $user->id,
]);
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/ActivityTest.php
@@ -0,0 +1,26 @@
<?php

namespace Tests\Unit;

use App\ReadChapter;
use App\User;
use Tests\TestCase;

class ActivityTest extends TestCase
{
public function testCreateActivityWhenUserCompleteChapter()
{
$user = factory(User::class)->create();

$this->actingAs($user);

$readChapter = factory(ReadChapter::class)->create();

$this->assertDatabaseHas('activities', [
'user_id' => auth()->id(),
'subject_id' => $readChapter->id,
'subject_type' => ReadChapter::class,
'type' => 'created_readchapter',
]);
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Model/UserTest.php
Expand Up @@ -14,6 +14,8 @@ public function testChapter()
{
$user = factory(User::class)->create();

$this->actingAs($user);

$chapter = factory(Chapter::class)->create();

factory(ReadChapter::class)->create([
Expand Down

0 comments on commit 6a6b696

Please sign in to comment.