Closed wisdom-diala closed 2 months ago
Hi @wisdom-diala,
Thanks for this PR.
This project only exists to promote the API Platform framework, and must include basic and not-complexe features based on API Platform. Sadly, your Pull Request doesn't add any API Platform feature to promote. Also, you're versionning the vendor directory which is considered as a bad practice by the community.
If you have any idea to improve this project by promoting any API Platform feature, feel free to open an issue, I'll be happy to talk about it.
Hi @wisdom-diala,
Thanks for this PR.
This project only exists to promote the API Platform framework, and must include basic and not-complexe features based on API Platform. Sadly, your Pull Request doesn't add any API Platform feature to promote. Also, you're versionning the vendor directory which is considered as a bad practice by the community.
If you have any idea to improve this project by promoting any API Platform feature, feel free to open an issue, I'll be happy to talk about it.
Sorry, this is for a testing purpose, wasn't expecting it to be merged.
Pull Request: Enhance Book Entity and API with Promotion Status and Slug, Implement Review Analysis Command
Description
This PR introduces several enhancements and new features to the Book entity and API, as well as a console command for review analysis. The changes are categorized into three main sections: Migrations and Entity Changes, API Validation/Serialization, and Console Command. Each section is outlined below with detailed descriptions of the changes and implementations.
1. Migrations + Entity Changes
isPromoted
Field to Book Entity:isPromoted
has been added to the Book entity.isPromoted = false
. Below is the migration file created for it:declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration;
/**
Auto-generated Migration: Please modify to your needs! */ final class Version20240621090715 extends AbstractMigration { public function getDescription(): string { return ''; }
public function up(Schema $schema): void { $this->addSql('ALTER TABLE book ADD is_promoted BOOLEAN DEFAULT FALSE NOT NULL'); }
public function down(Schema $schema): void { // this down() migration is auto-generated, please modify it to your needs $this->addSql('ALTER TABLE book DROP is_promoted'); } }
isPromoted
topromotionStatus
:isPromoted
field has been changed topromotionStatus
, which can have values:None
,Basic
, andPro
. A custom enumType was created to ensure that it only accept these three values:namespace App\Enum;
enum BookPromotionStatus: string { case None = 'None'; case Basic = 'Basic'; case Pro = 'Pro'; }
isPromoted
values are converted as follows:isPromoted == false
toNone
andisPromoted == true
toBasic
. This is the migration file that handles this:namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration;
/**
Auto-generated Migration: Please modify to your needs! */ final class Version20240621112405 extends AbstractMigration { public function getDescription(): string { return ''; }
public function up(Schema $schema): void { // this up() migration is auto-generated, please modify it to your needs // this up() migration is auto-generated, please modify it to your needs $this->addSql("ALTER TABLE book ADD promotion_status VARCHAR(255) DEFAULT 'None' NOT NULL");
}
public function down(Schema $schema): void { // this down() migration is auto-generated, please modify it to your needs $this->addSql('ALTER TABLE book ADD is_promoted BOOLEAN DEFAULT FALSE NOT NULL');
} }
2. API Validation/Serialization
Book Creation (POST /admin/books):
promotionStatus
field is now mandatory for the Book resource. The API ensures that only the allowed values (None
,Basic
,Pro
) are accepted.slug
field is also mandatory and must be at least 5 characters long. It can contain only lowercase Latin letters, numbers, or hyphens. Added this to the Book.php entity to ensure that the right input was used for the slug.Update Book Serialization:
slug
field is now visible in the API and accessible to everyone.promotionStatus
field is visible in the API but accessible only to administrators (OIDC_ADMIN
).3. Console Command
Review Analysis Command:
Enhanced Command with InputOption:
InputOption
to display the month (format Y-m) with the highest number of reviews published. If multiple months have the same maximum number of reviews, the most recent month is displayed.Here are the files created/adjusted for the console command Console Command File
Custom DQL files
DateStringDQL:
This was used to extract date in this format(YYYY-MM-DD)
from the reviews timestampFile Path: src/Doctrine/DateStringDQL.php
SubStringDQL:
This was used to extract only the year and month in this format(YYYY-MM)
from the reviews timestampFile Path: src/Doctrine/DateStringDQL.php
The
config/packages/doctrine.yaml
was edited to register the custom DQL functions:And finally this two methods was added to the
ReviewRepository.php
file.Migration Steps
isPromoted
field to the Book entity and initialize existing records.isPromoted
topromotionStatus
and convert existing values.slug
field and set the value for existing records.Testing and Validation
promotionStatus
andslug
fields correctly.slug
field is visible in the API for all users andpromotionStatus
is visible only to administrators.InputOption
.Console Command without option
Console Command with option