File size: 9,297 Bytes
5f32fbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | <?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\OpenaiGeneratorChatCategory;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class ChatTemplatesController extends Controller
{
/**
* Display a listing of the chat templates. If id sent thats will show single template
*
* @OA\Get(
* path="/api/aichat/chat-templates",
* operationId="ChatTemplatesController::index",
* tags={"Chat Templates"},
* summary="Get a list of chat templates or single one if id passed",
* description="Get a list of chat templates ordered by name in ascending order. If an ID is provided, it will retrieve a single chat template.",
* security={{ "passport": {} }},
*
* @OA\Response(
* response=200,
* description="Successful operation",
*
* @OA\JsonContent(
* type="object",
* ),
* ),
*
* @OA\Response(
* response=401,
* description="Unauthenticated",
* ),
* )
*/
public function index($id = null)
{
if ($id == 'undefined' || $id == null) {
$list = OpenaiGeneratorChatCategory::orderBy('name', 'asc')->get();
} else {
$list = OpenaiGeneratorChatCategory::find($id);
if (! $list) {
return response()->json(['error' => __('Template Not Found')], 404);
}
}
return response()->json($list, 200);
}
/**
* @OA\Post(
* path="/api/aichat/chat-templates",
* summary="!!PATCH REQUEST!! see payload '_method' Update a chat template - post used only for test",
* operationId="ChatTemplatesController@update",
* tags={"Chat Templates"},
* security={{ "passport": {} }},
* description="Update the specified chat template in storage. If `template_id` is 'undefined' or `null`, a new template will be created.",
*
* @OA\RequestBody(
* required=true,
* description="Chat template data",
*
* @OA\MediaType(
* mediaType="multipart/form-data",
*
* @OA\Schema(
* type="object",
*
* @OA\Property(
* property="_method",
* description="ID of the chat template",
* type="string",
* default="PATCH",
* ),
* @OA\Property(
* property="template_id",
* description="ID of the chat template",
* type="string"
* ),
* @OA\Property(
* property="name",
* description="Name of the chat template",
* type="string"
* ),
* @OA\Property(
* property="short_name",
* description="Short name of the chat template",
* type="string"
* ),
* @OA\Property(
* property="description",
* description="Description of the chat template",
* type="string"
* ),
* @OA\Property(
* property="role",
* description="Role of the chat template",
* type="string"
* ),
* @OA\Property(
* property="human_name",
* description="Human name of the chat template",
* type="string"
* ),
* @OA\Property(
* property="helps_with",
* description="How the chat template helps",
* type="string"
* ),
* @OA\Property(
* property="color",
* description="Color of the chat template",
* type="string"
* ),
* @OA\Property(
* property="chat_completions",
* description="Chat completions",
* type="string"
* ),
* @OA\Property(
* property="avatar",
* description="Avatar image file (allowed types: jpg, jpeg, png, svg, webp; max size: 5MB)",
* type="string",
* format="binary"
* ),
* ),
* ),
* ),
*
* @OA\Response(
* response=200,
* description="Successful operation",
*
* @OA\JsonContent(
* type="object",
*
* @OA\Property(property="message", type="string", example="Template edited successfully"),
* ),
* ),
*
* @OA\Response(
* response=419,
* description="Validation error or unsupported file extension",
*
* @OA\JsonContent(
* type="object",
*
* @OA\Property(property="errors", type="array", @OA\Items(type="string")),
* ),
* ),
* )
*/
public function update(Request $request)
{
if ($request->template_id == 'undefined' || $request->template_id == null) {
$template = new OpenaiGeneratorChatCategory;
$res = __('Template added successfully');
} else {
$template = OpenaiGeneratorChatCategory::where('id', $request->template_id)->firstOrFail();
$res = __('Template updates successfully');
}
if ($request->hasFile('avatar')) {
$path = 'upload/images/chatbot/';
$image = $request->file('avatar');
$image_name = Str::random(4) . '-' . Str::slug($request->name) . '-avatar.' . $image->getClientOriginalExtension();
//Resim uzantı kontrolü
$imageTypes = ['jpg', 'jpeg', 'png', 'svg', 'webp'];
if (! in_array(Str::lower($image->getClientOriginalExtension()), $imageTypes)) {
$data = [
'errors' => ['The file extension must be jpg, jpeg, png, webp or svg.'],
];
return response()->json($data, 419);
}
$image->move($path, $image_name);
$template->image = $path . $image_name;
}
$template->name = $request->name;
$template->slug = Str::slug($request->name) . '-' . Str::random(5);
$template->short_name = $request->short_name;
$template->description = $request->description;
$template->role = $request->role;
$template->human_name = $request->human_name;
$template->helps_with = $request->helps_with;
$template->color = $request->color;
$template->chat_completions = $request->chat_completions;
$template->prompt_prefix = 'As a ' . $request->role . ', ';
$template->save();
return response()->json(['message' => $res], 200);
}
/**
* Remove the specified chat templates from storage.
*
* @OA\Delete(
* path="/api/aichat/chat-templates/{id}",
* summary="Remove a chat template",
* operationId="ChatTemplatesController@destroy",
* tags={"Chat Templates"},
* security={{ "passport": {} }},
* description="Remove the specified chat template from storage.",
*
* @OA\Parameter(
* name="id",
* in="path",
* description="ID of the chat template",
* required=true,
*
* @OA\Schema(type="string")
* ),
*
* @OA\Response(
* response=204,
* description="Chat template deleted successfully",
* ),
* @OA\Response(
* response=404,
* description="Chat template not found",
*
* @OA\JsonContent(
* type="object",
*
* @OA\Property(property="message", type="string", example="Chat template not found"),
* ),
* ),
*
* @OA\Response(
* response=500,
* description="Server error",
*
* @OA\JsonContent(
* type="object",
*
* @OA\Property(property="message", type="string", example="Internal Server Error"),
* ),
* ),
* )
*/
public function destroy($id = null)
{
if ($id == null) {
return response()->json(['error' => __('ID required')], 412);
}
$template = OpenaiGeneratorChatCategory::where('id', $id)->firstOrFail();
$template->delete();
return response()->json(['message' => 'Deleted Successfully'], 200);
}
}
|