Repo rebuild
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\ChatGPT\Http\Controllers;
|
||||
|
||||
use Illuminate\Contracts\Support\Renderable;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Routing\Controller;
|
||||
use Modules\ChatGPT\Http\Requests\StoreChatGptApiSettingRequest;
|
||||
use Modules\ChatGPT\Http\Requests\UpdateChatGptApiSettingRequest;
|
||||
use Modules\ChatGPT\Models\ChatGptApiSetting;
|
||||
|
||||
class ChatGPTController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the ChatGPT module settings page.
|
||||
*/
|
||||
public function index(): Renderable
|
||||
{
|
||||
return view('chatgpt::index', [
|
||||
'settings' => ChatGptApiSetting::query()->latest('id')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a new encrypted OpenAI API key setting.
|
||||
*/
|
||||
public function store(StoreChatGptApiSettingRequest $request): RedirectResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
ChatGptApiSetting::query()->update(['is_active' => false]);
|
||||
|
||||
ChatGptApiSetting::query()->create([
|
||||
'name' => $data['name'] ?: 'OpenAI API Key',
|
||||
'api_key' => $data['api_key'],
|
||||
'is_active' => (bool) ($data['is_active'] ?? true),
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->route('chatgpt.index')
|
||||
->with('status', 'OpenAI API key saved.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an encrypted OpenAI API key setting.
|
||||
*/
|
||||
public function update(UpdateChatGptApiSettingRequest $request, ChatGptApiSetting $setting): RedirectResponse
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
if ((bool) ($data['is_active'] ?? false)) {
|
||||
ChatGptApiSetting::query()
|
||||
->whereKeyNot($setting->getKey())
|
||||
->update(['is_active' => false]);
|
||||
}
|
||||
|
||||
$setting->name = $data['name'] ?: $setting->name;
|
||||
$setting->is_active = (bool) ($data['is_active'] ?? false);
|
||||
|
||||
if (! empty($data['api_key'])) {
|
||||
$setting->api_key = $data['api_key'];
|
||||
}
|
||||
|
||||
$setting->save();
|
||||
|
||||
return redirect()
|
||||
->route('chatgpt.index')
|
||||
->with('status', 'OpenAI API key updated.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an encrypted OpenAI API key setting.
|
||||
*/
|
||||
public function destroy(ChatGptApiSetting $setting): RedirectResponse
|
||||
{
|
||||
$setting->delete();
|
||||
|
||||
return redirect()
|
||||
->route('chatgpt.index')
|
||||
->with('status', 'OpenAI API key deleted.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\ChatGPT\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreChatGptApiSettingRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['nullable', 'string', 'max:255'],
|
||||
'api_key' => ['required', 'string', 'min:20', 'max:500'],
|
||||
'is_active' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\ChatGPT\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateChatGptApiSettingRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['nullable', 'string', 'max:255'],
|
||||
'api_key' => ['nullable', 'string', 'min:20', 'max:500'],
|
||||
'is_active' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user