-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1618 from fey/rename-readChapter-to-ChapterMember
Rename read chapter to chapter member
- Loading branch information
Showing
40 changed files
with
416 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Chapter; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Chapter; | ||
use App\Models\ChapterMember; | ||
use App\Models\User; | ||
use App\Services\ActivityService; | ||
use Flash; | ||
use Illuminate\Http\RedirectResponse; | ||
|
||
class ChapterMemberController extends Controller | ||
{ | ||
public function finish(Chapter $chapter, ActivityService $activityService): RedirectResponse | ||
{ | ||
$user = auth()->user(); | ||
$currentChapterMember = $this->getMember($user, $chapter); | ||
$currentChapterMember->finish(); | ||
$currentChapterMember->save(); | ||
|
||
// TODO: add points for finishing | ||
flash()->info(__('layout.flash.success'))->success(); | ||
$activityService->logChapterMemberFinished($currentChapterMember); | ||
|
||
return back(); | ||
} | ||
|
||
private function getMember(User $user, Chapter $chapter): ChapterMember | ||
{ | ||
$currentChapterMember = $chapter | ||
->members() | ||
->whereUserId($user->id) | ||
->firstOr(function () use ($chapter, $user): ChapterMember { | ||
$chapterMember = new ChapterMember([]); | ||
|
||
$chapterMember->user()->associate($user); | ||
$chapterMember->chapter()->associate($chapter); | ||
|
||
return $chapterMember; | ||
}); | ||
|
||
return $currentChapterMember; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use AllowDynamicProperties; | ||
use Database\Factories\ChapterMemberFactory; | ||
use Iben\Statable\Statable; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Casts\Attribute; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
/** | ||
* @method static ChapterMemberFactory factory(...$parameters) | ||
* @property string $state | ||
*/ | ||
class ChapterMember extends Model | ||
{ | ||
use HasFactory; | ||
use Statable; | ||
|
||
public const INITIAL_STATE = 'started'; | ||
|
||
protected function getGraph(): string | ||
{ | ||
return 'chapter_member'; | ||
} | ||
|
||
protected function saveBeforeTransition(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function mayFinish(): bool | ||
{ | ||
return $this->canApply('finish'); | ||
} | ||
|
||
public function isFinished(): bool | ||
{ | ||
return $this->state === 'finished'; | ||
} | ||
|
||
public function isNotFinished(): bool | ||
{ | ||
return !$this->isFinished(); | ||
} | ||
|
||
public function finish(): void | ||
{ | ||
$this->apply('finish'); | ||
} | ||
|
||
public function isStarted(): bool | ||
{ | ||
return $this->state === 'started'; | ||
} | ||
|
||
public function scopeFinished(Builder $builder): Builder | ||
{ | ||
return $builder->where('state', '=', 'finished'); | ||
} | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
public function chapter(): BelongsTo | ||
{ | ||
return $this->belongsTo(Chapter::class); | ||
} | ||
|
||
protected function state(): Attribute | ||
{ | ||
return Attribute::make( | ||
get:fn($state) => $state ?? self::INITIAL_STATE | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.