Files
law-api/app/Http/Controllers/api/SectionController.php

60 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\SectionRequest;
use App\Models\Law;
use App\Models\LikeSection;
use App\Models\Order;
use App\Models\Section;
use App\Traits\BaseApiResponse;
class SectionController extends Controller
{
use BaseApiResponse;
public function index(SectionRequest $request)
{
$validatedData = $request->validated();
$perPage = $validatedData['per_page'] ?? 15;
$page = $validatedData['page'] ?? 1;
$section = Section::where('book_id', $validatedData['book_id'])->paginate($perPage, ['*'], 'page', $page);
$section->getCollection()->transform(function ($section) {
unset($section['book_id']);
$section['is_locked'] = auth()->user()->isSubscriber() !== false ? false : Law::where('is_locked',$section['law_id'])->first()?->is_locked;
unset($section['law_id']);
return $section;
});
return $this->success($section->items(), 'Success');
}
public function like(Section $section)
{
$userId = auth()->id();
$existingLike = LikeSection::query()->where('section_id', $section->id)
->where('user_id', $userId)
->first();
if ($existingLike) {
$existingLike->delete();
return $this->success(null, 'Success', 'Section unliked successfully');
}
LikeSection::create([
'section_id' => $section->id,
'user_id' => $userId,
]);
return $this->success(null, 'Success', 'Section liked successfully');
}
}