@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\FolderFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property string $name
|
||||
* @property int|null $parent_id
|
||||
* @property Folder|null $parent
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
#[Fillable(['user_id', 'name', 'parent_id'])]
|
||||
class Folder extends Model
|
||||
{
|
||||
/** @use HasFactory<FolderFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/** @return BelongsTo<User, $this> */
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Folder, $this> */
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Folder::class, 'parent_id');
|
||||
}
|
||||
|
||||
/** @return HasMany<Folder, $this> */
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this->hasMany(Folder::class, 'parent_id');
|
||||
}
|
||||
|
||||
/** @return HasMany<Todo, $this> */
|
||||
public function todos(): HasMany
|
||||
{
|
||||
return $this->hasMany(Todo::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope the query to folders owned by the given user.
|
||||
*
|
||||
* @param Builder<Folder> $query
|
||||
* @return Builder<Folder>
|
||||
*/
|
||||
public function scopeForUser(Builder $query, User|int $user): Builder
|
||||
{
|
||||
return $query->where('user_id', $user instanceof User ? $user->id : $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope the query to top-level folders (no parent).
|
||||
*
|
||||
* @param Builder<Folder> $query
|
||||
* @return Builder<Folder>
|
||||
*/
|
||||
public function scopeToplevel(Builder $query): Builder
|
||||
{
|
||||
return $query->whereNull('parent_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a flattened, depth-aware tree of the user's folders
|
||||
* ordered top-down, with the full path (e.g. "Work / Acme Corp").
|
||||
*
|
||||
* @return Collection<int, array{folder: Folder, depth: int, path: string}>
|
||||
*/
|
||||
public static function treeForUser(User|int $user): Collection
|
||||
{
|
||||
$folders = Folder::query()
|
||||
->forUser($user)
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$tree = collect();
|
||||
$visit = function (Folder $folder, int $depth, string $prefix) use (&$visit, &$tree, $folders): void {
|
||||
$path = $prefix === '' ? $folder->name : $prefix.' / '.$folder->name;
|
||||
|
||||
$tree->push([
|
||||
'folder' => $folder,
|
||||
'depth' => $depth,
|
||||
'path' => $path,
|
||||
]);
|
||||
|
||||
foreach ($folders->where('parent_id', $folder->id)->values() as $child) {
|
||||
$visit($child, $depth + 1, $path);
|
||||
}
|
||||
};
|
||||
|
||||
foreach ($folders->whereNull('parent_id')->values() as $topLevel) {
|
||||
$visit($topLevel, 0, '');
|
||||
}
|
||||
|
||||
return $tree;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\TodoFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property int $folder_id
|
||||
* @property string $description
|
||||
* @property string|null $notes
|
||||
* @property Carbon|null $due_date
|
||||
* @property Carbon|null $completed_at
|
||||
* @property Folder $folder
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
#[Fillable(['user_id', 'folder_id', 'description', 'notes', 'due_date', 'completed_at'])]
|
||||
class Todo extends Model
|
||||
{
|
||||
/** @use HasFactory<TodoFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/** @return BelongsTo<User, $this> */
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Folder, $this> */
|
||||
public function folder(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Folder::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'due_date' => 'date',
|
||||
'completed_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope the query to todos owned by the given user.
|
||||
*
|
||||
* @param Builder<Todo> $query
|
||||
* @return Builder<Todo>
|
||||
*/
|
||||
public function scopeForUser(Builder $query, User|int $user): Builder
|
||||
{
|
||||
return $query->where('user_id', $user instanceof User ? $user->id : $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope the query to todos that have not been completed.
|
||||
*
|
||||
* @param Builder<Todo> $query
|
||||
* @return Builder<Todo>
|
||||
*/
|
||||
public function scopeIncomplete(Builder $query): Builder
|
||||
{
|
||||
return $query->whereNull('completed_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope the query to todos that have been completed.
|
||||
*
|
||||
* @param Builder<Todo> $query
|
||||
* @return Builder<Todo>
|
||||
*/
|
||||
public function scopeCompleted(Builder $query): Builder
|
||||
{
|
||||
return $query->whereNotNull('completed_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope the query to open todos whose due date has already passed.
|
||||
*
|
||||
* @param Builder<Todo> $query
|
||||
* @return Builder<Todo>
|
||||
*/
|
||||
public function scopeOverdue(Builder $query): Builder
|
||||
{
|
||||
return $query->incomplete()->whereDate('due_date', '<', now());
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope the query to open todos due today.
|
||||
*
|
||||
* @param Builder<Todo> $query
|
||||
* @return Builder<Todo>
|
||||
*/
|
||||
public function scopeDueToday(Builder $query): Builder
|
||||
{
|
||||
return $query->incomplete()->whereDate('due_date', today());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Fortify\Contracts\PasskeyUser;
|
||||
use Laravel\Fortify\PasskeyAuthenticatable;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $email
|
||||
* @property Carbon|null $email_verified_at
|
||||
* @property string $password
|
||||
* @property string|null $two_factor_secret
|
||||
* @property string|null $two_factor_recovery_codes
|
||||
* @property Carbon|null $two_factor_confirmed_at
|
||||
* @property string|null $remember_token
|
||||
* @property bool $is_admin
|
||||
* @property int $notes_limit
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
#[Fillable(['name', 'email', 'password', 'is_admin', 'notes_limit'])]
|
||||
#[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token'])]
|
||||
class User extends Authenticatable implements PasskeyUser
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable, PasskeyAuthenticatable, TwoFactorAuthenticatable;
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user's initials
|
||||
*/
|
||||
public function initials(): string
|
||||
{
|
||||
$initials = Str::initials($this->name, true);
|
||||
|
||||
return Str::length($initials) > 1
|
||||
? Str::substr($initials, 0, 1).Str::substr($initials, -1)
|
||||
: $initials;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user is an administrator.
|
||||
*/
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return (bool) $this->is_admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the maximum length allowed for todo notes.
|
||||
*/
|
||||
public function notesLimit(): int
|
||||
{
|
||||
return $this->notes_limit ?? 1000;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user