Skip to content

Add vehicle and fuel management #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 10, 2025
Merged

Add vehicle and fuel management #1

merged 6 commits into from
Jul 10, 2025

Conversation

tuto1902
Copy link
Owner

Summary

  • Vehicle management (creation and editing forms, vehicle listing, and deletion with modal confirmation).
  • Fuel entry management.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @tuto1902, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces comprehensive vehicle and fuel entry management capabilities to the application. It provides a complete set of user interfaces and backend logic for creating, viewing, editing, and deleting vehicles and their associated fuel records. A key enhancement is the integration of user-configurable distance and volume units, with automatic conversions handled throughout the system to ensure data accuracy and user convenience.

Highlights

  • Vehicle Management Implementation: Full CRUD (Create, Read, Update, Delete) functionality for vehicles has been implemented, including dedicated Livewire components (CreateVehicle, EditVehicle, Vehicles) and a reusable form (VehicleForm). Vehicle deletion now cascades to associated fuel entries, ensuring data integrity.
  • Fuel Entry Management Implementation: Comprehensive CRUD functionality for fuel entries has been added, with dedicated Livewire components (CreateFuelEntry, EditFuelEntry, FuelEntries) and a shared form (FuelEntryForm). This includes real-time calculation of total_cost or fuel_amount based on user input, and custom validation rules for odometer readings.
  • Unit Conversion and User Preferences: The application now supports user-specific settings for distance (miles/kilometers) and volume (gallons/liters) units. Automatic conversion of odometer readings and fuel amounts is handled for both display and storage, ensuring data consistency regardless of the user's preferred units. A new 'Units' settings page has been added for user configuration.
  • Data Model Enhancements: The User, Vehicle, and FuelEntry models have been significantly enhanced to support the new features. This includes adding new attributes for unit preferences, calculated attributes for fuel efficiency (fuelEfficiency), and methods to retrieve sequential odometer readings (previousOdometer, nextOdometer). Proper mass assignment ($fillable) and deletion hooks (booted method for Vehicle) have also been implemented.
  • New UI and Routing: New Blade views and routes have been integrated for all vehicle and fuel entry management pages, as well as a dedicated 'Units' section within user settings. These new interfaces leverage Flux UI components for a consistent and improved user experience.
  • Dependency Update: The livewire/flux-pro dependency has been added to the project, indicating an upgrade or addition of advanced UI components and features.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@tuto1902 tuto1902 merged commit 34b7c7d into main Jul 10, 2025
0 of 2 checks passed
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces vehicle and fuel management features. I've identified a critical security vulnerability, potential N+1 query problems, and bugs in data conversion and calculation logic. Addressing these points will significantly improve the quality, security, and performance of the new features.

Comment on lines +53 to +59
if ($this->fuelEntry->nextFuelEntry) {
$rules['odometer'][] =function ($attribute, $value, $fail) {
if ($value >= $this->fuelEntry->nextOdometer) {
$fail('The odometer reading must be less than the next reading of ' . $this->fuelEntry->nextOdometer . ' ' . $this->vehicle->distance_units);
}
};
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The $this->fuelEntry->nextFuelEntry property seems to be a typo. It should likely be $this->fuelEntry->nextOdometer. Also, the nextOdometer accessor in the FuelEntry model should return a numeric odometer value instead of a FuelEntry model instance. This will cause a type error during validation.

            if ($this->fuelEntry->nextFuelEntry && $this->fuelEntry->nextFuelEntry->odometer < $value) {
                $rules['odometer'][] = function ($attribute, $value, $fail) {
                    $fail('The odometer reading must be less than the next reading of ' . $this->fuelEntry->nextOdometer . ' ' . $this->vehicle->distance_units);
                };
            }


public function mount()
{
$this->vehicles = Vehicle::all();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Vehicle::all() fetches all vehicle records, ignoring ownership. Scope this query to retrieve vehicles belonging to the authenticated user to avoid exposing all vehicles in the system.

        $this->vehicles = Auth::user()->vehicles()->get();

Comment on lines +44 to +51
public function odometer(): Attribute
{
return Attribute::make(
get: function () {
return $this->lastOdometer * 1.60934;
},
);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The odometer accessor unconditionally multiplies lastOdometer by 1.60934, which means it always returns a value in kilometers. However, the view displays this value alongside the vehicle's distance_units label. If a vehicle's units are set to 'mi', this will result in a kilometer value being incorrectly labeled as miles. The conversion should only be applied when the vehicle's distance_units is 'km'.

    public function odometer(): Attribute
    {
        return Attribute::make(
            get: function () {
                if ($this->distance_units === 'km') {
                    return $this->lastOdometer * 1.60934;
                }

                return $this->lastOdometer;
            },
        );
    }

'model' => ['required', 'string', 'max:255'],
'year' => ['required', 'integer', 'min:1900', 'max:' . (date('Y') + 1)],
'initial_odometer' => ['required', 'integer', 'min:0'],
'distance_units' => ['required', 'string', 'in:miles,km'],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The validation rule for distance_units is in:miles,km, but the form submits mi or km. Update the rule to accept mi instead of miles.

            'distance_units' => ['required', 'string', 'in:mi,km'],


public function mount()
{
$this->vehicles = Vehicle::all();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This query will cause an N+1 problem. When rendering the list of vehicles, the odometer accessor on the Vehicle model is called for each vehicle. This accessor, in turn, triggers a separate database query to find the latest fuel entry for each vehicle, leading to a large number of queries and poor performance. Eager load the latest fuel entry to avoid this.

$this->vehicles = Auth::user()->vehicles()->with('fuelEntries')->get();

Comment on lines +59 to 71
public function fuelEfficiency(): Attribute
{
return Attribute::make(
get: function () {
if (Auth::user()->distance_units === 'km') {
$differenceInKilometers = ($this->odometer - $this->previousOdometer) * 1.60934;
return $differenceInKilometers / $this->fuelAmountConverted;
}

return ($this->odometer - $this->previousOdometer) / $this->fuel_amount;
},
);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There's a potential for a division-by-zero error if $this->fuel_amount is zero, which would crash the application. Add a check to handle this case gracefully.

    public function fuelEfficiency(): Attribute
    {
        return Attribute::make(
            get: function () {
                if ($this->fuel_amount == 0) {
                    return 0;
                }

                if (Auth::user()->distance_units === 'km') {
                    $differenceInKilometers = ($this->odometer - $this->previousOdometer) * 1.60934;
                    return $differenceInKilometers / $this->fuelAmountConverted;
                }

                return ($this->odometer - $this->previousOdometer) / $this->fuel_amount;
            },
        );
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant