rezasanaeefar/magento-product-review
GitHub: rezasanaeefar/magento-product-review
Stars: 0 | Forks: 0
# Sana_ProductReview — Magento 2 REST API Module
A clean, well-tested Magento 2 module that exposes a **Product Review**
REST API. It is built strictly around Magento's architectural
conventions, so it doubles as a reference for idiomatic module design.
## Why this module demonstrates good Magento practice
- **Service contracts first** — a public `Api\` layer of interfaces
(`ReviewRepositoryInterface`, `ReviewInterface`) that the Web API and
internal callers share.
- **Dependency injection** — implementations bound via `etc/di.xml`;
no direct `ObjectManager` use.
- **Declarative schema** — the table is defined in `db_schema.xml`
with a matching whitelist (the modern approach, not `InstallSchema`).
- **Data patch** — sample data added via a tracked `DataPatchInterface`.
- **Proper error handling** — failures are translated into typed
Magento exceptions (`CouldNotSaveException`, `NoSuchEntityException`)
that the Web API maps to correct HTTP status codes.
- **ACL-protected writes** — read endpoints are anonymous, write
endpoints require an ACL resource.
- **Unit tested** — PHPUnit tests cover the repository's validation
and exception behaviour in isolation.
## Requirements
- Magento 2.4.x
- PHP 8.1+
## Installation
Copy the module into your Magento installation:
cp -R Sana/ProductReview app/code/Sana/ProductReview
Then enable it:
bin/magento module:enable Sana_ProductReview
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:flush
## REST API Endpoints
| Method | Endpoint | Auth | Description |
|--------|-------------------------|-------------|--------------------------|
| GET | `/V1/reviews` | Anonymous | List reviews (paginated) |
| GET | `/V1/reviews/:reviewId` | Anonymous | Get a single review |
| POST | `/V1/reviews` | ACL token | Create a review |
| DELETE | `/V1/reviews/:reviewId` | ACL token | Delete a review |
### Example: create a review
curl -X POST "https:///rest/V1/reviews" \
-H "Authorization: Bearer " \
-H "Content-Type: application/json" \
-d '{
"review": {
"product_id": 1,
"customer_name": "Jane Doe",
"rating": 5,
"title": "Great product",
"content": "Exactly as described."
}
}'
### Example: list reviews
curl "https:///rest/V1/reviews?searchCriteria[pageSize]=20"
## Running Tests
From the module directory:
composer install
vendor/bin/phpunit --testsuite "Sana_ProductReview Unit Tests"
Or via Magento's test runner from the Magento root:
vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist \
app/code/Sana/ProductReview/Test/Unit
## Module Structure
Sana/ProductReview/
├── Api/
│ ├── Data/
│ │ ├── ReviewInterface.php
│ │ └── ReviewSearchResultsInterface.php
│ └── ReviewRepositoryInterface.php # service contract
├── Model/
│ ├── Review.php # entity model
│ ├── ReviewRepository.php # business logic
│ └── ResourceModel/
│ ├── Review.php
│ └── Review/Collection.php
├── Setup/Patch/Data/AddSampleReview.php
├── Test/Unit/Model/ReviewRepositoryTest.php
├── etc/
│ ├── acl.xml
│ ├── di.xml
│ ├── db_schema.xml
│ ├── db_schema_whitelist.json
│ ├── module.xml
│ └── webapi.xml # REST route mapping
├── composer.json
├── registration.php
└── README.md
## License
MIT — see [LICENSE](LICENSE).
# magento-product-review
标签:ffuf