<commit_before><?php

/*
 * This file is part of Bens Penhorados, an undergraduate capstone project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Http\Controllers;

use App\Models\Items\Vehicle;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

/**
 * This is the vehicle results controller class.
 *
 * @author <NAME> <<EMAIL>>
 */
class VehicleResultsController extends Controller
{
    /**
     * Show a list of vehicles.
     *
     * @param Request $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function index(Request $request)
    {
        $input = [
            'per_page'    => (int) $request->input('limit') ?: 15,
            'make_id'     => (int) $request->input('make') ?: null,
            'model_id'    => (int) $request->input('model') ?: null,
            'category_id' => (int) $request->input('category') ?: null,
            'type_id'     => (int) $request->input('type') ?: null,
        ];

        $vehicles = $this->getVehicles($input);
        $data = $this->paginateVehicles($vehicles);

        return response()->json($data, 200);
    }

    /**
     * Paginate vehicles.
     *
     * @param LengthAwarePaginator $vehicles
     *
     * @return array
     */
    public function paginateVehicles(LengthAwarePaginator $vehicles)
    {
        $noResults = ($vehicles->isEmpty() ? true : false);

        $data = [];
        $data['from'] = ($noResults ? 0 : $vehicles->firstItem());
        $data['to'] = ($noResults ? 0 : $vehicles->lastItem());
        $data['total'] = ($noResults ? 0 : $vehicles->total());
        $data['limit'] = ($noResults ? 0 : $vehicles->perPage());

        $data['items'] = [];
        foreach ($vehicles as $vehicle) {
            $item = [
                    'title' => $vehicle->item->title,
                    'slug'  => $vehicle->item->slug,
                    'price' => $vehicle->item->price,
                    'image' => json_decode($vehicle->item->images)[0],
                    'make'  => $vehicle->make()->pluck('name'),
                    'model' => $vehicle->model()->pluck('name'),
                    'year'  => $vehicle->year,
                    'fuel'  => $vehicle->fuel()->pluck('name'),
                ];

            $data['items'][] = $item;
        }

        return $data;
    }

    /**
     * Get active vehicles.
     *
     * @param array $input
     *
     * @return LengthAwarePaginator
     */
    public function getVehicles($input)
    {
        $query = Vehicle::active();

        if (isset($input['make_id'])) {
            $query->whereMakeId($input['make_id']);
        }

        if (isset($input['model_id'])) {
            $query->whereModelId($input['model_id']);
        }

        if (isset($input['category_id'])) {
            $query->whereCategoryId($input['category_id']);
        }
<commit_msg>Filter results by district/municipality<commit_after><?php

/*
 * This file is part of Bens Penhorados, an undergraduate capstone project.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Http\Controllers;

use App\Models\Items\Vehicle;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

/**
 * This is the vehicle results controller class.
 *
 * @author <NAME> <<EMAIL>>
 */
class VehicleResultsController extends Controller
{
    /**
     * Show a list of vehicles.
     *
     * @param Request $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function index(Request $request)
    {
        $input = [
            'per_page'        => (int) $request->input('limit') ?: 5,
            'district_id'     => $request->input('district'),
            'municipality_id' => $request->input('municipality'),
            'make_id'         => $request->input('make'),
            'model_id'        => $request->input('model'),
            'category_id'     => $request->input('category'),
            'type_id'         => $request->input('type'),
        ];

        $vehicles = $this->getVehicles($input);
        $data = $this->paginateVehicles($vehicles);

        return response()->json($data, 200);
    }

    /**
     * Paginate vehicles.
     *
     * @param LengthAwarePaginator $vehicles
     *
     * @return array
     */
    public function paginateVehicles(LengthAwarePaginator $vehicles)
    {
        $noResults = ($vehicles->isEmpty() ? true : false);

        $data = [];
        $data['from'] = ($noResults ? 0 : $vehicles->firstItem());
        $data['to'] = ($noResults ? 0 : $vehicles->lastItem());
        $data['total'] = ($noResults ? 0 : $vehicles->total());
        $data['limit'] = ($noResults ? 0 : $vehicles->perPage());

        $data['items'] = [];
        foreach ($vehicles as $vehicle) {
            $item = [
                    'title' => $vehicle->item->title,
                    'slug'  => $vehicle->item->slug,
                    'price' => $vehicle->item->price,
                    'image' => json_decode($vehicle->item->images)[0],
                    'make'  => $vehicle->make()->pluck('name'),
                    'model' => $vehicle->model()->pluck('name'),
                    'year'  => $vehicle->year,
                    'fuel'  => $vehicle->fuel()->pluck('name'),
                ];

            $data['items'][] = $item;
        }

        return $data;
    }

    /**
     * Get active vehicles.
     *
     * @param array $input
     *
     * @return LengthAwarePaginator
     */
    public function getVehicles($input)
    {
        $query = Vehicle::active();

        if (isset($input['district_id'])) {
            $query->ofDistrict($input['district_id']);
        }

        if (isset($input['municipality_id'])) {
            $query->ofMunicipality($input['municipality_id']);
        }

        if (isset($input['make_id'])) {
            $query->whereMakeId($input['make_id']);
        }

        if (isset($input['model_id'])) {
            $query->whereModelId($input['model_id']);
        }

        if (isset($input['category_id'])) {
            $query->whereCategoryId($input['category_id']);
        }

{'fraction_non_alphanumeric': 0.17102994199717825, 'fraction_numerical': 0.002978523279510895, 'mean_word_length': 2.0599520383693046, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5893712', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1857, 'n_words': 361}