93 lines
3.2 KiB
PHP
93 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Api\V1\TestimonialRequest;
|
|
use App\Http\Resources\TestimonialResource;
|
|
use App\Models\Testimonial;
|
|
use App\Support\TranslatablePayload;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
|
|
/**
|
|
* What people the collective worked with said about it.
|
|
*/
|
|
class TestimonialController extends Controller
|
|
{
|
|
protected const WITH = ['avatarMedia'];
|
|
|
|
/**
|
|
* Filters: is_public, client_id, project_id, service_id, search (author), trashed.
|
|
*/
|
|
public function index(Request $request): AnonymousResourceCollection
|
|
{
|
|
$this->authorize('viewAny', Testimonial::class);
|
|
|
|
$testimonials = Testimonial::query()
|
|
->with(self::WITH)
|
|
->when($request->has('is_public'), fn ($q) => $q->where('is_public', $request->boolean('is_public')))
|
|
->when($request->filled('client_id'), fn ($q) => $q->where('client_id', $request->integer('client_id')))
|
|
->when($request->filled('project_id'), fn ($q) => $q->where('project_id', $request->integer('project_id')))
|
|
->when($request->filled('service_id'), fn ($q) => $q->where('service_id', $request->integer('service_id')))
|
|
->when($request->filled('search'), fn ($q) => $q->where('author_name', 'like', '%'.$request->string('search').'%'))
|
|
->when($request->boolean('trashed'), fn ($q) => $q->onlyTrashed())
|
|
->orderBy('position')
|
|
->latest('id')
|
|
->paginate(perPage: min($request->integer('per_page', 25), 100))
|
|
->withQueryString();
|
|
|
|
return TestimonialResource::collection($testimonials);
|
|
}
|
|
|
|
public function store(TestimonialRequest $request): JsonResponse
|
|
{
|
|
$this->authorize('create', Testimonial::class);
|
|
|
|
return TestimonialResource::make($this->save(new Testimonial, $request))->response()->setStatusCode(201);
|
|
}
|
|
|
|
public function show(Testimonial $testimonial): TestimonialResource
|
|
{
|
|
$this->authorize('view', $testimonial);
|
|
|
|
return TestimonialResource::make($testimonial->load(self::WITH));
|
|
}
|
|
|
|
public function update(TestimonialRequest $request, Testimonial $testimonial): TestimonialResource
|
|
{
|
|
$this->authorize('update', $testimonial);
|
|
|
|
return TestimonialResource::make($this->save($testimonial, $request));
|
|
}
|
|
|
|
public function destroy(Testimonial $testimonial): JsonResponse
|
|
{
|
|
$this->authorize('delete', $testimonial);
|
|
|
|
$testimonial->delete();
|
|
|
|
return response()->json(status: 204);
|
|
}
|
|
|
|
public function restore(Testimonial $testimonial): TestimonialResource
|
|
{
|
|
$this->authorize('restore', $testimonial);
|
|
|
|
$testimonial->restore();
|
|
|
|
return TestimonialResource::make($testimonial->load(self::WITH));
|
|
}
|
|
|
|
protected function save(Testimonial $testimonial, TestimonialRequest $request): Testimonial
|
|
{
|
|
$testimonial->fill(TranslatablePayload::merge($testimonial, $request->validated(), $testimonial->translatable));
|
|
$testimonial->save();
|
|
|
|
return $testimonial->load(self::WITH);
|
|
}
|
|
}
|