%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/geotechnics/api/app/Http/Controllers/Api/v1/
Upload File :
Create Path :
Current File : /var/www/html/geotechnics/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\Http\Requests\Api\v1\Clients\UpdateClientRequest;
use App\Models\CompanyProfile;
use App\Models\User;
use App\Notifications\SendResetPasswordToClientNotification;
use App\Transformers\UserTransformer;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
use Spatie\Fractal\Fractal;

class ClientsController extends ApiController
{
    /**
     * @param Request $request
     * @return Fractal
     */
    public function clientsToConfirm(Request $request)
    {
        /** @var LengthAwarePaginator $clients */
        $clients = User::query()
                       ->where('is_enabled', false);

        if ($search = $request->get('search')) {
            $clients = $clients->where(function ($q) use ($search) {
                $q->where('email', 'like', "%$search%")
                  ->orWhere('surname', 'like', "%$search%")
                  ->orWhere('name', 'like', "%$search%")
                  ->orWhereHas('companyProfile', function ($q) use ($search) {
                      $q->where('name', 'like', "%$search%");
                  });
            });
        } else {
            $clients = $clients->whereHas('companyProfile');
        }
        $clients = $clients->paginate($request->get('perPage'));
        return $this->withPaginated($clients, new UserTransformer());
    }

    public function index(Request $request)
    {
        $clients = User::query()
                       ->whereDoesntHave('roles')
                       ->where('is_enabled', true);
        if ($search = $request->get('search')) {
            $clients = $clients->where(function ($q) use ($search) {
                $q->where('email', 'like', "%$search%")
                  ->orWhere('surname', 'like', "%$search%")
                  ->orWhere('name', 'like', "%$search%")
                  ->orWhereHas('companyProfile', function ($q) use ($search) {
                      $q->where('name', 'like', "%$search%");
                  });
            });
        } else {
            $clients = $clients->whereHas('companyProfile');
        }
        $clients = $clients->paginate($request->get('perPage'));
        return $this->withPaginated($clients, new UserTransformer());
    }

    /**
     * @param CreateClientRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(CreateClientRequest $request)
    {
        DB::beginTransaction();
        try {
            /** @var User $user */
            $user = User::query()
                        ->create([
                            'email'        => $request->get('email'),
                            'password'     => $request->get('password'),
                            'is_enabled'   => true,
                            'is_confirmed' => true,
                        ]);
            $user->companyProfile()
                 ->create($request->only([
                     'name',
                     'company',
                     'email',
                     'password',
                     'phone',
                     'country_id',
                     'city',
                     'street',
                     'zip_code',
                 ]));

            DB::commit();

            $token = $user->generateResetPasswordToken();
            $user->notify(new SendResetPasswordToClientNotification($user, $token));

            return $this->item($user->fresh(), new UserTransformer());
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->wrongArguments([
                'message' => $e->getMessage(),
            ]);
        }
    }

    public function update(UpdateClientRequest $request, $client)
    {
        /** @var CompanyProfile $client */
        $client = CompanyProfile::query()
                                ->find($client);
        $client->update($request->only([
            'name',
            'company',
            'email',
            'phone',
            'country_id',
            'city',
            'street',
            'zip_code',
        ]));
        return $this->item($client->user->fresh(), new UserTransformer());
    }

    public function deleteClientConfirmation(User $user)
    {
        DB::beginTransaction();
        try {
            $user->companyProfile()
                 ->delete();
            $user->delete();
            DB::commit();
            return response()->json([], 204);
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->wrongArguments([
                'message' => $e->getMessage(),
            ]);
        }
    }

}

Zerion Mini Shell 1.0