Pages

Calendar API

Calendar API 

Here are the specific php artisan commands and the strictly scoped code reflecting exactly the pipeline you requested. No extra traits, global exception handlers, or enums have been added.

(Note: I have included the Model and Migration commands simply because the Repository layer requires a database target to function).

1. Artisan Commands

Run these commands in your terminal to generate the exact files for this pipeline:

Bash
# Model & Database
php artisan make:model Task -m

# FormRequest Validation
php artisan make:request TaskRequest

# DTO
php artisan make:class DTOs/TaskDTO

# Service Layer
php artisan make:class Services/TaskService

# Repository Layer
php artisan make:interface Repositories/TaskRepositoryInterface
php artisan make:class Repositories/TaskRepository

# Transformer (API Resource)
php artisan make:resource TaskResource

# Controller
php artisan make:controller TaskController --api

2. The Implementation Code (Following Strict Flow)

Step 1: FormRequest Validation

app/Http/Requests/TaskRequest.php

PHP
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class TaskRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'title'       => ['required', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
        ];
    }
}

Step 2: DTO Creation

app/DTOs/TaskDTO.php

PHP
<?php

namespace App\DTOs;

readonly class TaskDTO
{
    public function __construct(
        public string $title,
        public ?string $description
    ) {}
}

Step 3: Repository (Database)

app/Repositories/TaskRepositoryInterface.php

PHP
<?php

namespace App\Repositories;

use App\Models\Task;

interface TaskRepositoryInterface
{
    public function create(array $data): Task;
}

app/Repositories/TaskRepository.php

PHP
<?php

namespace App\Repositories;

use App\Models\Task;

class TaskRepository implements TaskRepositoryInterface
{
    public function create(array $data): Task
    {
        return Task::create($data);
    }
}

(Remember to bind this interface to the implementation in your AppServiceProvider):

PHP
$this->app->bind(\App\Repositories\TaskRepositoryInterface::class, \App\Repositories\TaskRepository::class);

Step 4: Service Layer (Business Logic)

app/Services/TaskService.php

PHP
<?php

namespace App\Services;

use App\DTOs\TaskDTO;
use App\Models\Task;
use App\Repositories\TaskRepositoryInterface;

class TaskService
{
    public function __construct(
        private readonly TaskRepositoryInterface $repository
    ) {}

    public function processNewTask(TaskDTO $dto): Task
    {
        // Execute business logic here
        $data = [
            'title'       => $dto->title,
            'description' => $dto->description,
        ];

        // Pass to repository
        return $this->repository->create($data);
    }
}

Step 5: Transformer

app/Http/Resources/TaskResource.php

PHP
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class TaskResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'          => $this->id,
            'title'       => $this->title,
            'description' => $this->description,
            'created_at'  => $this->created_at,
        ];
    }
}

Step 6 & 7: Controller & API Response (The Orchestrator)

app/Http/Controllers/TaskController.php

PHP
<?php

namespace App\Http\Controllers;

use App\DTOs\TaskDTO;
use App\Http\Requests\TaskRequest;
use App\Http\Resources\TaskResource;
use App\Services\TaskService;
use Illuminate\Http\JsonResponse;

class TaskController extends Controller
{
    public function __construct(
        private readonly TaskService $service
    ) {}

    public function store(TaskRequest $request): JsonResponse
    {
        // 1. Validation is already handled by TaskRequest injection

        // 2. DTO Creation
        $dto = new TaskDTO(
            title: $request->validated('title'),
            description: $request->validated('description')
        );

        // 3. Service Layer (which calls the Repository)
        $task = $this->service->processNewTask($dto);

        // 4. Transformer
        $transformedData = new TaskResource($task);

        // 5. API Response
        return response()->json([
            'success' => true,
            'data'    => $transformedData
        ], 201);
    }
}