Skip to content

fix: controller generator#194

Merged
Mohammad-Alavi merged 9 commits intoapiato:8.xfrom
innoflash:fix/controller-gen
Dec 8, 2024
Merged

fix: controller generator#194
Mohammad-Alavi merged 9 commits intoapiato:8.xfrom
innoflash:fix/controller-gen

Conversation

@innoflash
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Contributor 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 self-requested a review January 28, 2024 10:28
@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
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 28, 2024

Codecov Report

Attention: Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.

Project coverage is 4.26%. Comparing base (a9855f1) to head (a194c77).
Report is 10 commits behind head on 8.x.

Files with missing lines Patch % Lines
src/Generator/Commands/ContainerApiGenerator.php 0.00% 2 Missing ⚠️
src/Generator/Commands/ContainerWebGenerator.php 0.00% 2 Missing ⚠️
src/Generator/Commands/ControllerGenerator.php 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##               8.x    #194      +/-   ##
==========================================
- Coverage     4.26%   4.26%   -0.01%     
  Complexity     668     668              
==========================================
  Files           92      92              
  Lines         2742    2746       +4     
==========================================
  Hits           117     117              
- Misses        2625    2629       +4     

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

@Mohammad-Alavi
Copy link
Copy Markdown
Member

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

@innoflash
Copy link
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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
@Mohammad-Alavi Mohammad-Alavi merged commit 41af64d into apiato:8.x Dec 8, 2024
@innoflash innoflash deleted the fix/controller-gen branch December 8, 2024 15:19
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.

Can't set custom Model name when generating a Controller

2 participants