From d341c616f0f57afb21f15625f177b6bd77285619 Mon Sep 17 00:00:00 2001 From: myrmidex Date: Sun, 29 Jun 2025 19:46:50 +0200 Subject: [PATCH] Adapt validation --- app/Listeners/CheckArticleKeywords.php | 30 ------------------- app/Listeners/ValidateArticle.php | 28 +++++++++++++++++ app/Models/Article.php | 26 ++++++++++++++-- app/Providers/AppServiceProvider.php | 4 +-- app/Services/Article/ValidationService.php | 20 +++++++++++++ ...025_06_29_072202_create_articles_table.php | 3 +- 6 files changed, 76 insertions(+), 35 deletions(-) delete mode 100644 app/Listeners/CheckArticleKeywords.php create mode 100644 app/Listeners/ValidateArticle.php create mode 100644 app/Services/Article/ValidationService.php diff --git a/app/Listeners/CheckArticleKeywords.php b/app/Listeners/CheckArticleKeywords.php deleted file mode 100644 index 027041a..0000000 --- a/app/Listeners/CheckArticleKeywords.php +++ /dev/null @@ -1,30 +0,0 @@ -article->id); - - // TODO: Add actual keyword checking logic here - // For now, mark all articles as relevant - - $isRelevant = true; - - $event->article->update(['is_relevant' => $isRelevant]); - - if ($isRelevant) { - event(new ArticleReadyToPublish($event->article)); - } - } -} diff --git a/app/Listeners/ValidateArticle.php b/app/Listeners/ValidateArticle.php new file mode 100644 index 0000000..5cc03a9 --- /dev/null +++ b/app/Listeners/ValidateArticle.php @@ -0,0 +1,28 @@ +article; + + if (! is_null($article->validated_at)) { + return; + } + + $article = ValidationService::validate($article); + + if ($article->isValid()) { + event(new ArticleReadyToPublish($event->article)); + } + } +} diff --git a/app/Models/Article.php b/app/Models/Article.php index 90f605a..480f219 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -6,9 +6,16 @@ use Database\Factories\ArticleFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Carbon; /** * @method static firstOrCreate(string[] $array) + * @property integer $id + * @property string $url + * @property bool|null $is_valid + * @property Carbon|null $validated_at + * @property Carbon $created_at + * @property Carbon $updated_at */ class Article extends Model { @@ -17,18 +24,33 @@ class Article extends Model protected $fillable = [ 'url', - 'is_relevant', + 'is_valid', + 'validated_at', ]; public function casts(): array { return [ + 'validated_at' => 'datetime', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; } - protected static function booted() + public function isValid(): bool + { + if (is_null($this->validated_at)) { + return false; + } + + if (is_null($this->is_valid)) { + return false; + } + + return $this->is_valid; + } + + protected static function booted(): void { static::created(function ($article) { event(new ArticleFetched($article)); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index a8c3aeb..e9dcdba 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -5,7 +5,7 @@ use App\Events\ArticleFetched; use App\Events\ArticleReadyToPublish; use App\Events\ExceptionOccurred; -use App\Listeners\CheckArticleKeywords; +use App\Listeners\ValidateArticle; use App\Listeners\LogExceptionToDatabase; use App\Listeners\PublishArticle; use App\LogLevelEnum; @@ -25,7 +25,7 @@ public function boot(): void { Event::listen( ArticleFetched::class, - CheckArticleKeywords::class, + ValidateArticle::class, ); Event::listen( diff --git a/app/Services/Article/ValidationService.php b/app/Services/Article/ValidationService.php new file mode 100644 index 0000000..aa085a7 --- /dev/null +++ b/app/Services/Article/ValidationService.php @@ -0,0 +1,20 @@ +id); + + $article->update([ + 'is_valid' => true, + 'validated_at' => now(), + ]); + + return $article->refresh(); + } +} diff --git a/database/migrations/2025_06_29_072202_create_articles_table.php b/database/migrations/2025_06_29_072202_create_articles_table.php index 21998d0..5fcbae7 100644 --- a/database/migrations/2025_06_29_072202_create_articles_table.php +++ b/database/migrations/2025_06_29_072202_create_articles_table.php @@ -11,7 +11,8 @@ public function up(): void Schema::create('articles', function (Blueprint $table) { $table->id(); $table->string('url'); - $table->boolean('is_relevant')->nullable(); + $table->boolean('is_valid')->nullable(); + $table->timestamp('validated_at')->nullable(); $table->timestamps(); }); }