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
[API] Cart and Cart item CRUD #7297
Conversation
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @author Paweł Jędrzejewski <pawel@sylius.org> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess there should be your name
9a485b6
to
32eea32
Compare
factory: | ||
method: createForCart | ||
arguments: | ||
- "expr:service('sylius.repository.order').findOneBy({id: $cartId, state: 'cart'})" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use the proper method here.
criteria: | ||
order: $orderId | ||
state: 'cart' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
@@ -40,4 +35,5 @@ sylius_api_order_item_delete: | |||
serialization_version: $version | |||
criteria: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here.
@@ -56,6 +56,22 @@ public function findLatest($count) | |||
/** | |||
* {@inheritdoc} | |||
*/ | |||
public function createCartsPaginator(array $criteria = [], array $sorting = []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be replaced with createCartListQueryBuilder
method and used with grids - it will take care of filtering and sorting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be slightly harder, as we don't have a cart
resource... So it would create order routing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand what do you mean - By "grids" I understand grid configuration, not routing generation.
249da04
to
beb8c75
Compare
docs/api/carts.rst
Outdated
], | ||
"adjustments_total":0, | ||
"total":0, | ||
"state":"cart", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We display only carts, so this field is redundant.
template: "@SyliusUi/Grid/Field/state.html.twig" | ||
vars: | ||
labels: "@SyliusAdmin/Order/Label/PaymentState" | ||
shippingState: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shipping and payment states are irrelevant for cart.
resource: | | ||
alias: sylius.order | ||
section: api | ||
only: [show, delete] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should not allow for deleting orders, you can only cancel already created ones.
|
||
<service id="sylius.listener.api.add_to_cart" class="Sylius\Bundle\ApiBundle\EventListener\AddToCartListener"> | ||
<argument type="service" id="sylius.order_processing.order_processor.composite" /> | ||
<tag name="kernel.event_listener" event="sylius.order_item.post_create" method="cartItemResolver" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
event pre_create
method recalculateOrder
?
Shouldn't we recalculate order on update also?
@@ -96,7 +97,7 @@ private function addResourcesSection(ArrayNodeDefinition $node) | |||
->scalarNode('model')->defaultValue(OrderItem::class)->cannotBeEmpty()->end() | |||
->scalarNode('interface')->defaultValue(OrderItemInterface::class)->cannotBeEmpty()->end() | |||
->scalarNode('controller')->defaultValue(OrderItemController::class)->cannotBeEmpty()->end() | |||
->scalarNode('repository')->cannotBeEmpty()->end() | |||
->scalarNode('repository')->defaultValue(OrderItemRepository::class)->cannotBeEmpty()->end() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not be configured here.
"adjustments_total": 0, | ||
"variant": { | ||
"id": @integer@, | ||
"on_hold": 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on_hold
alone is not useful, we should add also on_hand
field or skip both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a part of variant serialization 👍 Will be fixed in #7348
"created_at": "@string@.isDateTime()", | ||
"updated_at": "@string@.isDateTime()", | ||
"enabled": true, | ||
"tax_calculation_strategy": "order_items_based", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need all the above information about the channel for embedded responses?
tests/Controller/CartApiTest.php
Outdated
/** | ||
* @test | ||
*/ | ||
public function it_allows_to_get_order() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_cart
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And everywhere else
tests/Controller/CartApiTest.php
Outdated
/** | ||
* @test | ||
*/ | ||
public function it_does_not_show_orders_in_stare_other_than_cart() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stare 😆
tests/Controller/CartApiTest.php
Outdated
/** | ||
* @test | ||
*/ | ||
public function it_does_not_allow_to_add_item_to_cart_without_providing_all_needed_fields() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all_needed
-> required
?
docs/api/carts.rst
Outdated
+-------------------+----------------------------------------------------------------------+ | ||
| adjustments | List of adjustments related to cart | | ||
+-------------------+----------------------------------------------------------------------+ | ||
| adjustments_total | Sum of all items adjustments | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be Sum of all order adjustments
? Adjustments can be applied on Order
as well as on OrderItem
.
docs/api/carts.rst
Outdated
+-------------------+----------------------------------------------------------------------+ | ||
| Field | Description | | ||
+===================+======================================================================+ | ||
| id | Id of shipping category | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shipping category
? 😱
docs/api/carts.rst
Outdated
.. _Default channel serialization: http://docs.sylius.org/en/latest/api/channels.html#channels-api | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some extra blank lines.
$orderItem = $event->getData(); | ||
|
||
if (null === $orderItem) { | ||
throw new UnexpectedTypeException($orderItem, OrderItemInterface::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it really unexpected type? 😄 In fact we're not getting any type here, it's just nothing (null) we get. UnexpectedTypeException
should be thrown if type assertion is made, not not null assertion. I think we should use there just an Assert::notNull($orderItem)
.
*/ | ||
public function getBlockPrefix() | ||
{ | ||
return 'sylius_api_cart_item'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be somehow connected with class name? So OrderItemType
-> sylius_api_order_item
, or CartItemType
-> sylius_api_cart_item
? Just thinking out loud 😄
version: 1 | ||
exclusion: | ||
groups: [Default, Detailed, DetailedCart] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here.
@@ -0,0 +1,32 @@ | |||
<?php | |||
|
|||
namespace Sylius\Bundle\OrderBundle\Doctrine\ORM; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need a license block?
->getOneOrNullResult() | ||
; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And again not needed blank line.
@@ -0,0 +1,19 @@ | |||
<?php | |||
namespace Sylius\Component\Order\Repository; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:(
@@ -0,0 +1,87 @@ | |||
Sylius\Component\Core\Model\Order: | |||
order_001: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 001
? Will we have hundreds of orders in this fixture? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just the way how we define order fixtures with alice. I just wanted to be consistent. It doesn't change anything. 🐋
cea9f90
to
12dd4fe
Compare
0816ef1
to
e617762
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1681 lines of docs where 90% are tables and api responses is truly tough to review.
docs/api/orders.rst
Outdated
Index of all orders | ||
------------------- | ||
If you request an order, you will receive an object with following fields: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary blank line.
docs/api/orders.rst
Outdated
@@ -3,165 +3,89 @@ Orders API | |||
|
|||
Sylius orders API endpoint is `/api/v1/orders`. | |||
|
|||
Index of all orders | |||
------------------- | |||
If you request an order, you will receive an object with following fields: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the following fields
docs/api/orders.rst
Outdated
+-----------------------+---------------------------------------------+ | ||
| Field | Description | | ||
+=======================+=============================================+ | ||
| id | Id of cart | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of cart? not an order? Seems you are describing an order here :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and a cart btw.
docs/api/orders.rst
Outdated
+-----------------------+---------------------------------------------+ | ||
| checkout_completed_at | Date when the checkout has been completed | | ||
+-----------------------+---------------------------------------------+ | ||
| number | Number of an order | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Number? serial number of the order
maybe?
docs/api/orders.rst
Outdated
+-----------------------+---------------------------------------------+ | ||
| shipping_address | Detailed address serialization | | ||
+-----------------------+---------------------------------------------+ | ||
| billing_address | Detailed address serialization | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one and the above one have identical descriptions, but shouldn't.
Detailed billing address serialization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They should, because it is the same enitity
docs/api/carts.rst
Outdated
|
||
.. note:: | ||
|
||
If it is still confusing to you, you should read more about `Carts (Orders)`_ and `Adjustments`_ first. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
learn more?
docs/api/carts.rst
Outdated
|
||
.. code-block:: text | ||
|
||
POST /api/v1/carts/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we give a full command? with $ curl
part? (in .. code-block:: bash
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I see. The example is below. So here do just a description.
To create a new cart you will need to call the ``/api/v1/carts/`` endpoint with ``POST`` method.
docs/api/carts.rst
Outdated
|
||
Example | ||
....... | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A description of what it does is needed.
To create a new cart for the ``shop@example.com`` user in the ``US_WEB`` channel in the ``en_US`` locale use the below method.
This method replicates the environment of shop usage, when a user is logged in some channel with some locale chosen.
I think such descriptions are required somewhere. The API calls are not self-understandable to everyone probably.
docs/api/carts.rst
Outdated
|
||
.. tip:: | ||
|
||
In this response you can see that promotion and shipping have been taken into account to calculate an appropriate price |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the appropriate price
docs/api/carts.rst
Outdated
|
||
.. code-block:: text | ||
|
||
STATUS: 200 Ok |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it 200 Ok
or 200 OK
coz you have it both in these docs?
I know @TheMadeleine, but I have try to be as clear with our docs as possible :( Any advice, what should be removed would be appreciated :) |
85633aa
to
f772750
Compare
number: desc | ||
fields: | ||
channel: | ||
type: twig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐋
variant: "@hard_available_mug" | ||
order: "@fulfilled_cart" | ||
|
||
Sylius\Component\Core\Model\OrderItemUnit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think creating entire order with units with fixtures can hurt us in the long run, but let's keep it for now, hopefully we can switch to Behat for API too and create these in the background at some point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is just needed to change cart item quantity. Checkout will be checked with request processing
docs/api/carts.rst
Outdated
|
||
A currency code will be added automatically based on a channel settings. :doc:`Read more about channels </book/configuration/channels>` | ||
|
||
If you try to create a resource without name or code, you will receive a 400 error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TheMadeleine made a suggestion that it should be a warning
.
And 400 Bad Request
.
docs/api/carts.rst
Outdated
Example | ||
^^^^^^^ | ||
|
||
To see delete the cart with id 21 use the method below. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To delete
docs/api/map.rst.inc
Outdated
@@ -2,6 +2,7 @@ | |||
* :doc:`/api/authorization` | |||
* :doc:`/api/channels` | |||
* :doc:`/api/orders` | |||
* :doc:`/api/carts` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be before order
type: string | ||
sortable: customer.email | ||
path: customer.email | ||
options: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be removed
@@ -37,7 +37,7 @@ | |||
} | |||
} | |||
}, | |||
"currency_code": "USD", | |||
"currency_code": "USD", "locale_code": "en_US", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:(
@@ -39,6 +39,6 @@ | |||
}, | |||
"payments": [], | |||
"shipments": [], | |||
"currency_code": "USD", | |||
"currency_code": "USD", "locale_code": "en_US", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:(
docs/api/carts.rst
Outdated
+-------------------+-------------------------------------------------------------------+ | ||
| adjustments | List of adjustments related to the cart | | ||
+-------------------+-------------------------------------------------------------------+ | ||
| adjustments_total | Sum of all order adjustments values | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since those are adjustments related to the cart, as stated above, shouldn't this order
be changed to cart
as well?
docs/api/carts.rst
Outdated
+-------------------+-------------------------------------------------------------------+ | ||
| total | Sum of items total and adjustments total | | ||
+-------------------+-------------------------------------------------------------------+ | ||
| customer | :doc:`Customer detailed serialization </api/customers>` for order | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for cart
docs/api/carts.rst
Outdated
.. warning:: | ||
|
||
Remember, that it doesn't replicate the environment of shop usage. It is more like an admin part of cart creation, which will allow you to manage | ||
cart from the admin perspective. ShopAPI is still an experimental concept. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either the cart
or carts
{ | ||
"customer": "shop@example.com", | ||
"channel": "US_WEB", | ||
"locale_code": "en_US" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the locale_code
needed to create the cart, but not included in the response?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
docs/api/carts.rst
Outdated
+===============+================+======================================+ | ||
| Authorization | header | Token received during authentication | | ||
+---------------+----------------+--------------------------------------+ | ||
| id | url attribute | Id of the requested resource | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resource
=> cart
lets be specific
sylius_api_order_item: | ||
resource: "@SyliusApiBundle/Resources/config/routing/order_item.yml" | ||
prefix: /orders/{orderId}/items | ||
sylius_api_cart_item: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move it under the cart's route
tests/Controller/CartApiTest.php
Outdated
*/ | ||
public function it_denies_carts_deletion_for_non_authenticated_user() | ||
{ | ||
$this->client->request('DELETE', '/api/v1/carts/-1'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice if we actually tried to delete an existing cart.
tests/Controller/CartApiTest.php
Outdated
public function it_does_not_allow_to_delete_orders_in_state_different_than_cart() | ||
{ | ||
$this->loadFixturesFromFile('authentication/api_administrator.yml'); | ||
$carts = $this->loadFixturesFromFile('resources/order.yml'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're defining it as a non-cart object (as stated in the test's title), should the variables still be named cart?
"This form should not contain extra fields." | ||
], | ||
"children": { | ||
"quantity": {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the array closed here...
], | ||
"children":{ | ||
"quantity":{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... but here's wide open?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
docs/api/carts.rst
Outdated
|
||
.. warning:: | ||
|
||
If you try to create a resource without name or code, you will receive a ``400 Bad Request`` error, that will contain validation errors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without localeCode, channel or customer
?
docs/api/carts.rst
Outdated
^^^^^^^ | ||
|
||
To add a new item of a variant with code ``MEDIUM_MUG_CUP`` | ||
to the cart with id 21(assuming, that we didn't remove it in the previous example) use the below method: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id = 21
Response | ||
~~~~~~~~ | ||
Example Response | ||
~~~~~~~~~~~~~~~~ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In previous doc you use Exemplary Response
, so which phrase is correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And it is at the same level as Example
or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exemplary Response
👍
docs/api/carts.rst
Outdated
Creating a Cart | ||
--------------- | ||
|
||
To create a new cart you will need to call the ``/api/v1/carts/`` endpoint with ``POST`` method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the ``POST``
docs/api/carts.rst
Outdated
Collection of Carts | ||
------------------- | ||
|
||
To retrieve a paginated list of carts you will need to call the ``/api/v1/carts/`` endpoint with ``GET`` method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the``GET``
Thanks Łukasz! Nice work! |
TODO:
EDIT:
Based on #7315