%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/hr/api/app/Http/Controllers/Api/v1/
Upload File :
Create Path :
Current File : /var/www/html/hr/api/app/Http/Controllers/Api/v1/ClientsController.php

<?php

namespace App\Http\Controllers\Api\v1;


use App\Http\Controllers\Api\ApiController;
use App\Http\Requests\Api\v1\Clients\CreateClientRequest;
use App\Models\Client;
use App\Transformers\ClientTransformer;
use App\Transformers\DayTransformer;
use Illuminate\Http\Request;
use Throwable;

class ClientsController extends ApiController
{
    /**
     * @var DayTransformer
     */
    private $transformer;

    public function __construct(ClientTransformer $transformer)
    {
        $this->transformer = $transformer;
    }

    public function index(Request $request)
    {
        $clients = Client::query();
        if ($search = $request->get('search')) {
            $clients->where('name', 'like', "%$search%")
                    ->orWhere('vat_number', 'like', "%$search%")
                    ->orWhere('email', 'like', "%$search%")
                    ->orWhere('phone', 'like', "%$search%");
        }
        $clients = $clients->paginate($request->get('per_page'));
        return $this->paginate($clients, $this->transformer);
    }

    public function show($client)
    {
        $client = Client::query()
                        ->findOrFail($client);
        return $this->item($client, $this->transformer);
    }

    public function store(CreateClientRequest $request)
    {
        try {
            $client = Client::createItem($request->only([
                'name',
                'address',
                'email',
                'phone',
                'vat_number',
            ]));
            return $this->item($client, $this->transformer);
        } catch (Throwable $e) {
            return $this->wrongArguments([
                'message' => $e->getMessage(),
            ]);
        }
    }

    public function update(CreateClientRequest $request, $client)
    {
        try {
            /** @var Client $client */
            $client = Client::query()
                            ->findOrFail($client);
            $client->updateItem($request->only([
                'name',
                'address',
                'email',
                'phone',
                'vat_number',
            ]));
            return $this->item($client, $this->transformer);
        } catch (Throwable $e) {
            return $this->wrongArguments([
                'message' => $e->getMessage(),
            ]);
        }
    }

    public function destroy(Client $client)
    {
        if ($client->projects()
                   ->count() > 0) {
            return $this->wrongArguments([
                'message' => trans('clients.cant_delete_due_projects'),
            ]);
        }
        Client::destroy($client->id);
        return $this->noContent();
    }

}

Zerion Mini Shell 1.0