trypost/app/Models/Post.php
Paulo Castellano 8689e54e55 refactor: restructure to Actions, subdomain routes (app/api), API tokens
- Extract business logic from controllers into Action classes:
  Post/, Workspace/, Hashtag/, Label/, Invite/, ApiKey/
- Create subdomain routing: app.trypost.test (Inertia dashboard),
  api.trypost.test (REST API with token auth)
- Add ApiToken model with tp_ prefix, token_lookup/hash auth
- Add AuthenticateApiToken middleware for API authentication
- Create Api controllers with JSON Resources for all entities
- Create App controllers that use Actions + Inertia responses
- Organize Form Requests into Api/ and App/ directories
- Add api_tokens migration
- Update all route names with app. prefix
- Update all tests to use new route names (684 passing)
2026-03-29 19:24:28 -03:00

109 lines
2.6 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Post\Status as PostStatus;
use Database\Factories\PostFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Post extends Model
{
/** @use HasFactory<PostFactory> */
use HasFactory, HasUuids;
protected $fillable = [
'workspace_id',
'user_id',
'status',
'synced',
'scheduled_at',
'published_at',
];
protected function casts(): array
{
return [
'status' => PostStatus::class,
'synced' => 'boolean',
'scheduled_at' => 'datetime',
'published_at' => 'datetime',
];
}
public function workspace(): BelongsTo
{
return $this->belongsTo(Workspace::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function postPlatforms(): HasMany
{
return $this->hasMany(PostPlatform::class);
}
public function labels(): BelongsToMany
{
return $this->belongsToMany(WorkspaceLabel::class);
}
public function scopeScheduled(Builder $query): Builder
{
return $query->where('status', PostStatus::Scheduled);
}
public function scopeDue(Builder $query): Builder
{
return $query->scheduled()->where('scheduled_at', '<=', now());
}
public function scopeDraft(Builder $query): Builder
{
return $query->where('status', PostStatus::Draft);
}
public function scopePublished(Builder $query): Builder
{
return $query->where('status', PostStatus::Published);
}
public function scopeFailed(Builder $query): Builder
{
return $query->where('status', PostStatus::Failed);
}
public function markAsPublishing(): void
{
$this->update(['status' => PostStatus::Publishing]);
}
public function markAsPublished(): void
{
$this->update([
'status' => PostStatus::Published,
'published_at' => now(),
]);
}
public function markAsPartiallyPublished(): void
{
$this->update([
'status' => PostStatus::PartiallyPublished,
'published_at' => now(),
]);
}
public function markAsFailed(): void
{
$this->update(['status' => PostStatus::Failed]);
}
}