/api/v1/projects, /members and /services: create, list, show, update, delete and restore, with the panel's own rules — so the MCP server can load the collective's work, people and offer without anyone retyping them into forms. The MCP server picks the tools up from openapi.yaml. - Relations travel as ids and are replaced whole when sent, left alone when not: a project's technology_ids, service_ids and members with each one's role; a member's technology_ids; a service's technology_ids and project_ids. Record and relations are saved in one transaction. - Publishing stays a permission of its own. Without projects.publish or services.publish, published_at, unpublish_at and is_published are refused — the fields the panel disables for the same people. - MemberPolicy still lets someone edit their own profile. - Uploads accept `for: projects` and `for: members`, landing where the panel's own upload fields put them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
3.2 KiB
PHP
108 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\ServiceRequest;
|
|
use App\Http\Resources\ServiceResource;
|
|
use App\Models\Service;
|
|
use App\Support\TranslatablePayload;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* What the collective offers, as the panel edits it.
|
|
*/
|
|
class ServiceController extends Controller
|
|
{
|
|
protected const WITH = ['technologies', 'projects'];
|
|
|
|
/**
|
|
* Filters: is_published, search (title in the active language), trashed.
|
|
*/
|
|
public function index(Request $request): AnonymousResourceCollection
|
|
{
|
|
$this->authorize('viewAny', Service::class);
|
|
|
|
$locale = app()->getLocale();
|
|
|
|
$services = Service::query()
|
|
->with(self::WITH)
|
|
->when($request->has('is_published'), fn ($q) => $q->where('is_published', $request->boolean('is_published')))
|
|
->when($request->filled('search'), fn ($q) => $q->where("title->{$locale}", 'like', '%'.$request->string('search').'%'))
|
|
->when($request->boolean('trashed'), fn ($q) => $q->onlyTrashed())
|
|
->orderBy('position')
|
|
->paginate(perPage: min($request->integer('per_page', 25), 100))
|
|
->withQueryString();
|
|
|
|
return ServiceResource::collection($services);
|
|
}
|
|
|
|
public function store(ServiceRequest $request): JsonResponse
|
|
{
|
|
$this->authorize('create', Service::class);
|
|
|
|
return ServiceResource::make($this->save(new Service, $request))->response()->setStatusCode(201);
|
|
}
|
|
|
|
public function show(Service $service): ServiceResource
|
|
{
|
|
$this->authorize('view', $service);
|
|
|
|
return ServiceResource::make($service->load(self::WITH));
|
|
}
|
|
|
|
public function update(ServiceRequest $request, Service $service): ServiceResource
|
|
{
|
|
$this->authorize('update', $service);
|
|
|
|
return ServiceResource::make($this->save($service, $request));
|
|
}
|
|
|
|
public function destroy(Service $service): JsonResponse
|
|
{
|
|
$this->authorize('delete', $service);
|
|
|
|
$service->delete();
|
|
|
|
return response()->json(status: 204);
|
|
}
|
|
|
|
public function restore(Service $service): ServiceResource
|
|
{
|
|
$this->authorize('restore', $service);
|
|
|
|
$service->restore();
|
|
|
|
return ServiceResource::make($service->load(self::WITH));
|
|
}
|
|
|
|
protected function save(Service $service, ServiceRequest $request): Service
|
|
{
|
|
$data = $request->validated();
|
|
|
|
DB::transaction(function () use ($service, $data): void {
|
|
$service->fill(TranslatablePayload::merge(
|
|
$service,
|
|
collect($data)->except(['technology_ids', 'project_ids'])->all(),
|
|
$service->translatable,
|
|
));
|
|
$service->save();
|
|
|
|
if (array_key_exists('technology_ids', $data)) {
|
|
$service->technologies()->sync($data['technology_ids']);
|
|
}
|
|
|
|
if (array_key_exists('project_ids', $data)) {
|
|
$service->projects()->sync($data['project_ids']);
|
|
}
|
|
});
|
|
|
|
return $service->load(self::WITH);
|
|
}
|
|
}
|