Skip to content
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

fix: controller generator #194

Open
wants to merge 5 commits into
base: 8.x
Choose a base branch
from

Conversation

innoflash
Copy link

@innoflash innoflash commented Jan 19, 2024

Description

This PR aims to solve the issue I mentioned on #193
The controller generator seems to be misbehaving and we eventually have a need to go fix the generated code.

Short version is controllers are built with the wrong model.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (refactoring a current feature, method, etc...)
  • Code Coverage (adding/removing/updating/refactoring tests)
  • New feature (non-breaking change which adds functionality)
  • Remove feature (non-breaking change which removes functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

I simply added a model option to the inputs.
Updated command is:
php artisan apiato:generate:controller --container Locations --model District
The model can also be requested for if one isnt provided in the command
Screenshot 2024-01-19 at 20 11 02

The generated file has this code:

<?php

namespace App\Containers\AppSection\Locations\UI\API\Controllers;

use Apiato\Core\Exceptions\IncorrectIdException;
use Apiato\Core\Exceptions\InvalidTransformerException;
use App\Containers\AppSection\Locations\Actions\CreateDistrictAction;
use App\Containers\AppSection\Locations\UI\API\Requests\CreateDistrictRequest;
use App\Containers\AppSection\Locations\UI\API\Transformers\DistrictTransformer;
use App\Ship\Exceptions\CreateResourceFailedException;
use App\Ship\Parents\Controllers\ApiController;
use Illuminate\Http\JsonResponse;

class TestFixController extends ApiController
{
    public function __construct(
        private readonly CreateDistrictAction $action,
    ) {
    }

    public function __invoke(CreateDistrictRequest $request): JsonResponse
    {
        $district = $this->action->run($request);

        return $this->created($this->transform($district, DistrictTransformer::class));
    }
}

This now makes use of the desired model in this container

@innoflash
Copy link
Author

Works for plural modelling too:

public function __construct(
        private readonly ListDistrictsAction $action
    ) {
    }

    public function __invoke(ListDistrictsRequest $request): array
    {
        $districts = $this->action->run($request);

        return $this->transform($districts, DistrictTransformer::class);
    }

@innoflash innoflash marked this pull request as draft January 24, 2024 05:08
@innoflash innoflash changed the title Fixed stub path in generator Fixed controller generator Jan 24, 2024
@innoflash innoflash marked this pull request as ready for review January 24, 2024 05:10
@Mohammad-Alavi Mohammad-Alavi added the type: bug A problem or error in the project that needs to be fixed label Jan 28, 2024
@Mohammad-Alavi Mohammad-Alavi linked an issue Jan 28, 2024 that may be closed by this pull request
Copy link

codecov bot commented Jan 28, 2024

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (d61aa42) 3.50% compared to head (31d00a7) 3.50%.

Files Patch % Lines
src/Generator/Commands/ControllerGenerator.php 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##               8.x    #194   +/-   ##
=======================================
  Coverage     3.50%   3.50%           
  Complexity     607     607           
=======================================
  Files           85      85           
  Lines         2539    2539           
=======================================
  Hits            89      89           
  Misses        2450    2450           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Mohammad-Alavi
Copy link
Member

@innoflash Thank you for this! 💪
Ill review it in the comming days. Sorry for the delay 😭

@innoflash
Copy link
Author

@innoflash Thank you for this! 💪 Ill review it in the comming days. Sorry for the delay 😭

Sure, take your time.

I may have found a couple more things I think I can help solve on the framework.
Otherwise Apiato is a marvel to work with @Mohammad-Alavi

@innoflash
Copy link
Author

@Mohammad-Alavi where is the test file for the controller generation so I can add a test for this addition?

@Mohammad-Alavi Mohammad-Alavi changed the title Fixed controller generator Fix controller generator Aug 22, 2024
@Mohammad-Alavi Mohammad-Alavi added type: fix A correction or resolution to an identified issue and removed type: bug A problem or error in the project that needs to be fixed labels Aug 22, 2024
@Mohammad-Alavi Mohammad-Alavi changed the title Fix controller generator fix: controller generator Aug 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: fix A correction or resolution to an identified issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken controller generator
2 participants