Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Issue #110: update book tutorial #112

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

Howriq
Copy link
Member

@Howriq Howriq commented May 12, 2025

No description provided.

@Howriq Howriq requested a review from alexmerlin May 12, 2025 20:55
Copy link
Member

@alexmerlin alexmerlin left a comment

Choose a reason for hiding this comment

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

To be continued...

@Howriq Howriq linked an issue May 13, 2025 that may be closed by this pull request
@Howriq Howriq linked an issue May 13, 2025 that may be closed by this pull request
Copy link
Member

@alexmerlin alexmerlin left a comment

Choose a reason for hiding this comment

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

  1. In ReleaseDateInput, replace:
    'message' => sprintf(Message::INVALID_VALUE, 'releaseDate'),
    with:
    'message' => Message::INVALID_VALUE,
    and remove use function sprintf;.

  2. The migrations diff command is outdated, replace it with:

php ./vendor/bin/doctrine-migrations diff
  1. The path mentioned in this sentence has changed in 6.0:

This will check for differences between your entities and database structure and create migration files if necessary, in data/doctrine/migrations.

Version 6.0 stores migrations in the Core module under the following path:
src/Core/src/App/src/Migration

  1. The migrations run command should also be updated:
php ./vendor/bin/doctrine-migrations migrate
  1. Rename across the document GetBookHandler to GetBookResourceHandler

  2. Rename across the document PostBookHandler to PostBookResourceHandler

so that they follow our naming convention.

@Howriq Howriq requested a review from alexmerlin May 14, 2025 10:28
];
}
$routeCollector->post('/book', PostBookHandler::class, 'book::create-book');
$routeCollector->get('/book/' . $uuid, GetBookHandler::class, 'book::view-book');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$routeCollector->get('/book/' . $uuid, GetBookHandler::class, 'book::view-book');
$routeCollector->get('/book/' . $uuid, GetBookResourceHandler::class, 'book::view-book');

Because:

Rename across the document GetBookHandler to GetBookResourceHandler

],
];
}
$routeCollector->post('/book', PostBookHandler::class, 'book::create-book');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$routeCollector->post('/book', PostBookHandler::class, 'book::create-book');
$routeCollector->post('/book', PostBookResourceHandler::class, 'book::create-book');

Because:

Rename across the document PostBookHandler to PostBookResourceHandler

│ └── src/
│ ├── Collection/
│ │ └── BookCollection.php
│ ├── Entity/
Copy link
Member

Choose a reason for hiding this comment

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

Remove node - the Book entity is in the Core module.

> Make sure you read and understand the rbac documentation.

It's time to update the `ConfigProvider` with all the necessary configuration needed, so the above files to work properly like dependency injection, aliases, doctrine mapping and so on.
After we have the handler, we need to register some routes in the `RoutesDelegator` using our new grouping method, the same we created when we registered the module.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
After we have the handler, we need to register some routes in the `RoutesDelegator` using our new grouping method, the same we created when we registered the module.
After we have the handler, we need to register some routes in the `RoutesDelegator`, the same we created when we registered the module.

We abandoned route groups for this tutorial.


In `src/Book/src` we will create 2 php files: `RoutesDelegator.php` and `ConfigProvider.php`. This files will be updated later with all needed configuration.
In `src/Book/src` we will create 2 PHP files: `RoutesDelegator.php` and `ConfigProvider.php`. These files contain the necessary configurations.

* `src/Book/src/RoutesDelegator.php`
Copy link
Member

Choose a reason for hiding this comment

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

This section is duplicate - you are creating the RoutesDelegator towards the end of the tutorial.
Let's keep only that one!

* `src/Book/src/Service/BookService.php` – is a class or component responsible for performing a specific task or providing functionality to other parts of the application
* `src/Core/src/Book/src/ConfigProvider.php` – is a class that provides configuration for Doctrine ORM
* `src/Core/src/Book/src/Entity/Book.php` – an entity refers to a PHP class that represents a persistent object or data structure
* `src/Core/src/Book/src/Repository/BookRepository.php` – a repository is a class responsible for querying and retrieving entities from the database

## Creating and configuring the module
Copy link
Member

Choose a reason for hiding this comment

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

This section could be a bit confusing for devs.
You instruct them to create a ConfigProvider with classes which don't exist yet - better leave it for when all the classes have been created.
We should keep here only the instructions for creating the directory structure and move the creation of the two ConfigProviders and the RouteDelegator to the end of the File creation and contents section.

],
],
];
}
}
```

### Registering the module
Copy link
Member

Choose a reason for hiding this comment

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

This section should also be moved further down to the point where the ConfigProvider has already been created.


To keep things simple in this tutorial our book will have 3 properties: `name`, `author` and `release date`.
To keep things simple in this tutorial, our book will have 3 properties: `name`, `author` and `release date`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
To keep things simple in this tutorial, our book will have 3 properties: `name`, `author` and `release date`.
To keep things simple in this tutorial, our book will have 3 properties: `name`, `author` and `releaseDate`.

@@ -460,29 +555,29 @@ class ReleaseDateInput extends Input

$this->getValidatorChain()
->attachByName(Date::class, [
'message' => sprintf(Message::INVALID_VALUE, 'releaseDate'),
'message' => Message::INVALID_VALUE,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'message' => Message::INVALID_VALUE,
'message' => Message::invalidValue('releaseDate'),

@Howriq Howriq requested a review from alexmerlin May 16, 2025 09:06
Copy link
Member

@alexmerlin alexmerlin left a comment

Choose a reason for hiding this comment

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

After this, these steps will have to be tested in version 6.0 of Dotkernel API.

namespace Api\Book;

use Api\Book\Handler\BookHandler;
use Api\Book\Handler\GetBookCollectionHandler;
use Api\Book\Handler\GetBookHandler;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
use Api\Book\Handler\GetBookHandler;
use Api\Book\Handler\GetBookResourceHandler;


## File creation and contents

In `src` and `src/Core/src` folders we will create one `Book` folder and in those we will create the `src` folder.
So the final structure will be like this: `src/Book/src` and `src/Core/src/Book/src`.

Each file below have a summary description above of what that file does.
Copy link
Member

@alexmerlin alexmerlin May 16, 2025

Choose a reason for hiding this comment

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

Suggested change
Each file below have a summary description above of what that file does.

Devs just read through these descriptions, no need to do it again.

@@ -248,26 +165,22 @@ class Book extends AbstractEntity

```

* `src/Book/src/Repository/BookRepository.php`
* `src/Core/src/Book/src/Repository/BookRepository.php`
Copy link
Member

Choose a reason for hiding this comment

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

Repository structure in general has changed in 6.0.
Having a saveBook method is correct, but note that there is a generic saveResource method.
getBooks should not return a Query instance, but a QueryBuilder, see example here.

use DateTimeImmutable;
use Dot\DependencyInjection\Attribute\Inject;
use Exception;

class BookService implements BookServiceInterface
Copy link
Member

Choose a reason for hiding this comment

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

This needs some refactoring, see AdminService in 6.0 for example.
Then adapt BookServiceInterface.

```

That's it. The module is now registered.

We need to configure access to the newly created endpoints, add `books::list-books`, `book::view-book` and `book::create-book` to the authorization rbac array, under the `UserRole::ROLE_GUEST` key.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
We need to configure access to the newly created endpoints, add `books::list-books`, `book::view-book` and `book::create-book` to the authorization rbac array, under the `UserRole::ROLE_GUEST` key.
We need to configure access to the newly created endpoints.
Open `config/autoload/authorization.global.php` and append the below route names to the `UserRoleEnum::Guest->value` key:
- `books::list-books`
- `book::view-book`
- `book::create-book`


We need to configure access to the newly created endpoints, add `books::list-books`, `book::view-book` and `book::create-book` to the authorization rbac array, under the `UserRole::ROLE_GUEST` key.
> Make sure you read and understand the rbac [documentation](https://docs.dotkernel.org/dot-rbac-guard/v4/configuration/).

## Migrations

We created the `Book` entity, but we didn't create the associated table for it.
Copy link
Member

Choose a reason for hiding this comment

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

In this section:

  • fix typo shel
  • replace php bin/doctrine orm:validate-schema with php ./bin/doctrine orm:validate-schema

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.

update book tutorial
2 participants