Skip to content

Commit

Permalink
refact: ev validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Oct 17, 2024
1 parent 91f9ab2 commit 6781af6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
37 changes: 35 additions & 2 deletions src/Domains/Event/Events/Actions/CreateEventAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Kanvas\Event\Events\Actions;

use Baka\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Kanvas\Currencies\Models\Currencies;
use Kanvas\Event\Events\DataTransferObject\Event;
use Kanvas\Event\Events\DataTransferObject\EventVersion;
Expand All @@ -19,7 +21,11 @@ public function __construct(

public function execute(): ModelsEvent
{
$event = DB::connection('event')->transaction(function () {
$slug = $this->event->slug ?? Str::slug($this->event->name);

$this->validateSlug($slug);

$event = DB::connection('event')->transaction(function () use ($slug) {
$event = ModelsEvent::create([
'apps_id' => $this->event->app->getId(),
'companies_id' => $this->event->company->getId(),
Expand All @@ -32,6 +38,7 @@ public function execute(): ModelsEvent
'event_category_id' => $this->event->category->getId(),
'event_class_id' => $this->event->class->getId(),
'description' => $this->event->description,
'slug' => $slug,
]);

$eventVersion = new CreateEventVersionAction(
Expand All @@ -43,7 +50,8 @@ public function execute(): ModelsEvent
version: 1,
description: $this->event->description,
pricePerTicket: 0,
dates: $this->event->dates
dates: $this->event->dates,
slug: $slug
)
);

Expand All @@ -54,4 +62,29 @@ public function execute(): ModelsEvent

return $event;
}

protected function validateSlug(string $slug): void
{
Validator::make(
['slug' => $slug],
[
'slug' => [
'required',
// Custom rule using DB to specify the connection and validate uniqueness.
function ($attribute, $value, $fail) {
$exists = DB::connection('event') // Replace with your DB connection name.
->table('events')
->where('slug', $value)
->where('apps_id', $this->event->app->getId())
->where('companies_id', $this->event->company->getId())
->exists();

if ($exists) {
$fail('The ' . $attribute . ' has already been taken.');
}
},
],
]
)->validate();
}
}
35 changes: 33 additions & 2 deletions src/Domains/Event/Events/Actions/CreateEventVersionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Kanvas\Event\Events\Actions;

use Baka\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Kanvas\Event\Events\DataTransferObject\EventVersion;
use Kanvas\Event\Events\Models\Event as ModelsEvent;
use Kanvas\Event\Events\Models\EventVersion as ModelsEventVersion;

class CreateEventVersionAction
Expand All @@ -17,6 +19,10 @@ public function __construct(

public function execute(): ModelsEventVersion
{
$slug = $this->eventVersion->slug ?? Str::slug($this->eventVersion->name);

$this->validateSlug($slug);

$eventVersion = ModelsEventVersion::create([
'apps_id' => $this->eventVersion->event->app->getId(),
'companies_id' => $this->eventVersion->event->company->getId(),
Expand All @@ -26,14 +32,39 @@ public function execute(): ModelsEventVersion
'name' => $this->eventVersion->name,
'version' => $this->eventVersion->version,
'description' => $this->eventVersion->description,
'slug' => $this->eventVersion->slug ?? null,
'price_per_ticket' => $this->eventVersion->pricePerTicket,
'agenda' => $this->eventVersion->agenda ?? null,
'metadata' => $this->eventVersion->metadata ?? null,
'slug' => $slug,
]);

$eventVersion->addDates($this->eventVersion->dates);

return $eventVersion;
}

protected function validateSlug(string $slug): void
{
Validator::make(
['slug' => $slug],
[
'slug' => [
'required',
// Custom rule using DB to specify the connection and validate uniqueness.
function ($attribute, $value, $fail) {
$exists = DB::connection('event') // Replace with your DB connection name.
->table('event_versions')
->where('slug', $value)
->where('apps_id', $this->eventVersion->event->app->getId())
->where('companies_id', $this->eventVersion->event->company->getId())
->exists();

if ($exists) {
$fail('The ' . $attribute . ' has already been taken.');
}
},
],
]
)->validate();
}
}

0 comments on commit 6781af6

Please sign in to comment.