Add generic api schema and scripts model

This commit is contained in:
2025-08-30 18:20:24 +02:00
parent 774d1cf45f
commit f6993d463d
18 changed files with 493 additions and 192 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Livewire;
use App\Bots\BotContract;
use App\Models\Bot;
use Cron\CronExpression;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
@@ -14,6 +15,8 @@ use Lorisleiva\CronTranslator\CronTranslator;
class CreateEditBot extends Component
{
public ?Bot $bot = null;
public string $name = '';
public string $class = '';
@@ -24,7 +27,7 @@ class CreateEditBot extends Component
public string $cron_text = 'Every minute';
public string $config = '{}';
public array $config = [];
public array $classList = [];
@@ -32,17 +35,18 @@ class CreateEditBot extends Component
public string $routeName = '';
public function mount(?int $bot = null)
public function mount()
{
$this->routeName = URL::current() === route('bots.create') ? 'Create Bot' : 'Edit Bot';
$bot = \App\Models\Bot::find($bot);
if ($bot) {
$this->name = $bot->name;
$this->class = $bot->class;
$this->schedule = $bot->schedule;
$this->enabled = $bot->enabled;
$this->cron_text = \Lorisleiva\CronTranslator\CronTranslator::translate($bot->schedule);
if ($this->bot) {
$this->name = $this->bot->name;
$this->class = $this->bot->class;
$this->config = $this->bot->config ?? [];
$this->schedule = $this->bot->schedule;
$this->enabled = $this->bot->enabled;
$this->cron_text = CronTranslator::translate($this->bot->schedule);
$this->configSchema = $this->bot->class::configSchema();
}
$basePath = $basePath ?? app_path('Bots');
@@ -60,7 +64,7 @@ class CreateEditBot extends Component
? $class::label()
: class_basename($class);
$this->classList[$label] = $class;
$this->classList[$class] = $label;
}
}
}
@@ -119,32 +123,31 @@ class CreateEditBot extends Component
return;
}
if (URL::current() !== route('bots.create')) {
$bot = \App\Models\Bot::where('name', $this->name)->first();
if ($bot) {
$bot->update([
'name' => $this->name,
'class' => $this->class,
'enabled' => $this->enabled,
'schedule' => $this->schedule,
]);
if ($this->routeName !== 'Create Bot') {
$this->bot->update([
'name' => $this->name,
'class' => $this->class,
'enabled' => $this->enabled,
'schedule' => $this->schedule,
'config' => $this->config,
]);
flash()->success('Bot updated successfully.');
flash()->success('Bot updated successfully.');
return;
}
return;
} else {
\App\Models\Bot::create([
'name' => $this->name,
'class' => $this->class,
'enabled' => $this->enabled,
'schedule' => $this->schedule,
'config' => $this->config,
]);
}
flash()->success('Bot created successfully.');
$this->reset(['name', 'namespace', 'class', 'enabled', 'schedule']);
$this->reset(['name', 'class', 'enabled', 'schedule', 'config', 'configSchema']);
}
public function updatedSchedule($value)
@@ -157,6 +160,34 @@ class CreateEditBot extends Component
}
}
protected function validationAttributes(): array
{
$attributes = [];
foreach ($this->configSchema as $field => $meta) {
$attributes["config.$field"] = $meta['label'];
}
$attributes['name'] = 'Bot Name';
$attributes['schedule'] = 'Schedule';
$attributes['class'] = 'Bot Schema';
return $attributes;
}
public function prettify(string $field): void
{
if (isset($this->config[$field])) {
if (json_validate($this->config[$field])) {
$decoded = json_decode($this->config[$field], true);
$this->config[$field] = json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$this->resetErrorBag("config.$field");
} else {
$this->addError("config.$field", 'Invalid JSON format.');
}
}
}
public function render()
{
return view('livewire.create-edit-bot');