-
Notifications
You must be signed in to change notification settings - Fork 1
feat: добавлен API эндпоинт /api/v1/links для управления важными ссылками #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
143332b
6116e56
b058b29
e4bb3cd
099e56b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,125 @@ | ||||||||||
| <?php | ||||||||||
|
|
||||||||||
| declare(strict_types=1); | ||||||||||
|
|
||||||||||
| namespace App\Http\Controllers\Api\V1; | ||||||||||
|
|
||||||||||
| use App\Http\Controllers\Controller; | ||||||||||
| use App\Models\ImportantLink; | ||||||||||
| use Illuminate\Http\Request; | ||||||||||
| use Log; | ||||||||||
|
|
||||||||||
| class ImportantLinkController extends Controller | ||||||||||
| { | ||||||||||
| public function index(Request $request) | ||||||||||
| { | ||||||||||
| $perPage = (int) $request->query('per_page', '15'); | ||||||||||
| $dealershipId = $request->query('dealership_id'); | ||||||||||
| $isActive = $request->query('is_active'); | ||||||||||
|
|
||||||||||
| $query = ImportantLink::with(['creator', 'dealership']); | ||||||||||
|
|
||||||||||
| if ($dealershipId !== null) { | ||||||||||
| $query->where('dealership_id', $dealershipId); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if ($isActive !== null) { | ||||||||||
| $query->where('is_active', (bool) $isActive); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Casting the Prompt for AI agents
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Casting the query parameter to bool makes Prompt for AI agents
Suggested change
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| $links = $query->orderBy('sort_order') | ||||||||||
| ->orderBy('created_at', 'desc') | ||||||||||
| ->paginate($perPage); | ||||||||||
|
|
||||||||||
| return response()->json($links); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public function show($id) | ||||||||||
| { | ||||||||||
| $link = ImportantLink::with(['creator', 'dealership'])->find($id); | ||||||||||
|
|
||||||||||
| if (!$link) { | ||||||||||
| return response()->json([ | ||||||||||
| 'message' => 'Ссылка не найдена' | ||||||||||
| ], 404); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return response()->json($link); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public function store(Request $request) | ||||||||||
| { | ||||||||||
| Log::info("Request ImportantLink Store: " . json_encode($request->all())); | ||||||||||
| $validated = $request->validate([ | ||||||||||
| 'title' => 'required|string|max:255', | ||||||||||
| 'url' => 'required|string|max:1000|url', | ||||||||||
| 'description' => 'nullable|string', | ||||||||||
| 'dealership_id' => 'nullable|integer|exists:auto_dealerships,id', | ||||||||||
| 'sort_order' => 'integer', | ||||||||||
| 'is_active' => 'boolean', | ||||||||||
| ]); | ||||||||||
|
|
||||||||||
| // Устанавливаем creator_id из текущего пользователя | ||||||||||
| $validated['creator_id'] = $request->user()->id; | ||||||||||
|
|
||||||||||
| $link = ImportantLink::create($validated); | ||||||||||
|
|
||||||||||
| // Загружаем связи для ответа | ||||||||||
| $link->load(['creator', 'dealership']); | ||||||||||
|
|
||||||||||
| return response()->json($link, 201); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public function update(Request $request, $id) | ||||||||||
| { | ||||||||||
| $link = ImportantLink::find($id); | ||||||||||
|
|
||||||||||
| if (!$link) { | ||||||||||
| return response()->json([ | ||||||||||
| 'message' => 'Ссылка не найдена' | ||||||||||
| ], 404); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| $validated = $request->validate([ | ||||||||||
| 'title' => 'sometimes|required|string|max:255', | ||||||||||
| 'url' => 'sometimes|required|string|max:1000|url', | ||||||||||
| 'description' => 'nullable|string', | ||||||||||
| 'dealership_id' => 'nullable|integer|exists:auto_dealerships,id', | ||||||||||
| 'sort_order' => 'integer', | ||||||||||
| 'is_active' => 'boolean', | ||||||||||
| ]); | ||||||||||
|
|
||||||||||
| $link->update($validated); | ||||||||||
|
|
||||||||||
| // Загружаем связи для ответа | ||||||||||
| $link->load(['creator', 'dealership']); | ||||||||||
|
|
||||||||||
| return response()->json($link); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| public function destroy($id) | ||||||||||
| { | ||||||||||
| $link = ImportantLink::find($id); | ||||||||||
|
|
||||||||||
| if (!$link) { | ||||||||||
| return response()->json([ | ||||||||||
| 'message' => 'Ссылка не найдена' | ||||||||||
| ], 404); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| try { | ||||||||||
| $link->delete(); | ||||||||||
|
|
||||||||||
| return response()->json([ | ||||||||||
| 'success' => true, | ||||||||||
| 'message' => 'Ссылка успешно удалена' | ||||||||||
| ], 200); | ||||||||||
| } catch (\Exception $e) { | ||||||||||
| return response()->json([ | ||||||||||
| 'success' => false, | ||||||||||
| 'message' => 'Ошибка при удалении ссылки', | ||||||||||
| 'error' => $e->getMessage() | ||||||||||
| ], 500); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Database\Factories; | ||
|
|
||
| use App\Models\AutoDealership; | ||
| use App\Models\ImportantLink; | ||
| use App\Models\User; | ||
| use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
|
||
| class ImportantLinkFactory extends Factory | ||
| { | ||
| protected $model = ImportantLink::class; | ||
|
|
||
| public function definition(): array | ||
| { | ||
| return [ | ||
| 'title' => fake()->sentence(3), | ||
| 'url' => fake()->url(), | ||
| 'description' => fake()->optional()->paragraph(), | ||
| 'dealership_id' => AutoDealership::factory(), | ||
| 'creator_id' => User::factory(), | ||
| 'sort_order' => fake()->numberBetween(0, 100), | ||
| 'is_active' => true, | ||
| ]; | ||
| } | ||
|
|
||
| public function inactive(): static | ||
| { | ||
| return $this->state(fn (array $attributes) => [ | ||
| 'is_active' => false, | ||
| ]); | ||
| } | ||
|
|
||
| public function global(): static | ||
| { | ||
| return $this->state(fn (array $attributes) => [ | ||
| 'dealership_id' => null, | ||
| ]); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Casting the
is_activequery parameter with(bool)causes/api/v1/links?is_active=falseto be treated astrue, so the endpoint returns active links when inactive ones are requested. Please normalize the string viafilter_var(..., FILTER_VALIDATE_BOOL)(or similar) before applying the filter.Prompt for AI agents