First Commit
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\PromptTemplate;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\PromptTemplate\StorePromptTemplateRequest;
|
||||
use App\Http\Requests\PromptTemplate\UpdatePromptTemplateRequest;
|
||||
use App\Models\PromptTemplate;
|
||||
use App\Services\PromptTemplateService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class PromptTemplateController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly PromptTemplateService $promptTemplateService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
$this->authorize('viewAny', \App\Models\PromptTemplate::class);
|
||||
|
||||
$result = $this->promptTemplateService->index();
|
||||
$templates = $result['data'] ?? [];
|
||||
|
||||
return view('prompt-templates.index', compact('templates'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
$this->authorize('create', \App\Models\PromptTemplate::class);
|
||||
|
||||
return view('prompt-templates.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(StorePromptTemplateRequest $request): RedirectResponse
|
||||
{
|
||||
$this->authorize('create', \App\Models\PromptTemplate::class);
|
||||
|
||||
$data = $request->validated();
|
||||
|
||||
if (isset($data['template_data']) && is_string($data['template_data'])) {
|
||||
$data['template_data'] = json_decode($data['template_data'], true);
|
||||
}
|
||||
|
||||
$this->promptTemplateService->store($data);
|
||||
|
||||
return redirect()->route('prompt-templates.index')
|
||||
->with('success', 'Template created successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(\App\Models\PromptTemplate $promptTemplate): View
|
||||
{
|
||||
$this->authorize('update', $promptTemplate);
|
||||
|
||||
return view('prompt-templates.edit', compact('promptTemplate'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(UpdatePromptTemplateRequest $request, \App\Models\PromptTemplate $promptTemplate): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', $promptTemplate);
|
||||
|
||||
$data = $request->validated();
|
||||
|
||||
if (isset($data['template_data']) && is_string($data['template_data'])) {
|
||||
$data['template_data'] = json_decode($data['template_data'], true);
|
||||
}
|
||||
|
||||
$this->promptTemplateService->update($promptTemplate, $data);
|
||||
|
||||
return redirect()->route('prompt-templates.index')
|
||||
->with('success', 'Template updated successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(\App\Models\PromptTemplate $promptTemplate): RedirectResponse
|
||||
{
|
||||
$this->authorize('delete', $promptTemplate);
|
||||
|
||||
$this->promptTemplateService->destroy($promptTemplate);
|
||||
|
||||
return redirect()->route('prompt-templates.index')
|
||||
->with('success', 'Template deleted successfully.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all templates as JSON for the dropdown.
|
||||
*/
|
||||
public function api(): JsonResponse
|
||||
{
|
||||
$templates = PromptTemplate::where('is_public', true)
|
||||
->orWhere('user_id', auth()->id())
|
||||
->get(['name', 'template_data']);
|
||||
|
||||
$result = [];
|
||||
|
||||
foreach ($templates as $template) {
|
||||
$data = $template->template_data;
|
||||
$result[$template->name] = [
|
||||
'title' => $data['title'] ?? $template->name,
|
||||
'objective' => $data['objective'] ?? '',
|
||||
'context' => $data['context'] ?? '',
|
||||
'persona' => $data['persona'] ?? '',
|
||||
'tone' => $data['tone'] ?? '',
|
||||
'constraints' => $data['constraints'] ?? '',
|
||||
'output_format' => $data['output_format'] ?? '',
|
||||
'examples' => $data['examples'] ?? '',
|
||||
'variables' => $data['variables'] ?? '',
|
||||
'temperature' => $data['temperature'] ?? 0.7,
|
||||
'max_tokens' => $data['max_tokens'] ?? 1024,
|
||||
];
|
||||
}
|
||||
|
||||
return response()->json($result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user