Skip to content

Commit

Permalink
Some changes for easier extendability (for example in Vouchers packag…
Browse files Browse the repository at this point in the history
…e) (#49)

* Some changes for easier extendability (for example in Vouchers package)

* Endpoint for finding Product with type `single` for given Productable

* Add return types to Resources `toArray` method
  • Loading branch information
pa-cholek authored Mar 11, 2022
1 parent c3d2412 commit a8e54e4
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 32 deletions.
12 changes: 12 additions & 0 deletions src/Http/Controllers/Admin/ProductableAdminApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use EscolaLms\Cart\Http\Requests\Admin\ProductableAttachRequest;
use EscolaLms\Cart\Http\Requests\Admin\ProductableDetachRequest;
use EscolaLms\Cart\Http\Requests\Admin\ProductableProductRequest;
use EscolaLms\Cart\Http\Requests\Admin\ProductableRegisteredListRequest;
use EscolaLms\Cart\Http\Resources\ProductResource;
use EscolaLms\Cart\Http\Swagger\Admin\ProductableAdminSwagger;
use EscolaLms\Cart\Services\Contracts\ProductServiceContract;
use EscolaLms\Cart\Services\Contracts\ShopServiceContract;
Expand Down Expand Up @@ -38,6 +40,16 @@ public function detach(ProductableDetachRequest $request): JsonResponse
return $this->sendSuccess(__('Productable detached from user'));
}

public function product(ProductableProductRequest $request): JsonResponse
{
$productable = $this->productService->findProductable($request->getProductableType(), $request->getProductableId());
$product = $this->productService->findSingleProductForProductable($productable);
if ($product) {
return $this->sendResponseForResource(ProductResource::make($product), __('Single Product for Productable found'));
}
return $this->sendError(__('Single Product for this productable does not exist'), 404);
}

public function registered(ProductableRegisteredListRequest $request): JsonResponse
{
return $this->sendResponse($this->productService->listRegisteredProductableClasses(), __('List of registered Productable types'));
Expand Down
34 changes: 34 additions & 0 deletions src/Http/Requests/Admin/ProductableProductRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace EscolaLms\Cart\Http\Requests\Admin;

use EscolaLms\Cart\Enums\CartPermissionsEnum;
use EscolaLms\Cart\Rules\ProductableExistsRule;
use EscolaLms\Cart\Rules\ProductableRegisteredRule;
use Illuminate\Foundation\Http\FormRequest;

class ProductableProductRequest extends FormRequest
{
public function authorize()
{
return $this->user()->can(CartPermissionsEnum::MANAGE_PRODUCTS);
}

public function rules(): array
{
return [
'productable_id' => ['required', new ProductableExistsRule()],
'productable_type' => ['required', 'string', new ProductableRegisteredRule()],
];
}

public function getProductableId(): int
{
return $this->validated()['productable_id'];
}

public function getProductableType(): string
{
return $this->validated()['productable_type'];
}
}
2 changes: 1 addition & 1 deletion src/Http/Resources/CartItemResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function getCartItem(): CartItem
return $this->resource;
}

public function toArray($request)
public function toArray($request): array
{
return self::apply([
'id' => $this->getCartItem()->getKey(),
Expand Down
10 changes: 8 additions & 2 deletions src/Http/Resources/CartResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use EscolaLms\Auth\Traits\ResourceExtandable;
use EscolaLms\Cart\Models\Cart;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;

class CartResource extends JsonResource
{
Expand All @@ -23,13 +24,18 @@ protected function getCart(): Cart
return $this->resource;
}

public function toArray($request)
protected function getCartItemsResourceCollection(): ResourceCollection
{
return CartItemResource::collection($this->getCart()->items);
}

public function toArray($request): array
{
return self::apply([
'total' => $this->getCart()->total,
'subtotal' => $this->getCart()->subtotal,
'tax' => $this->getCart()->getTaxAttribute($this->taxRate),
'items' => CartItemResource::collection($this->getCart()->items)
'items' => $this->getCartItemsResourceCollection()
], $this);
}
}
39 changes: 39 additions & 0 deletions src/Http/Resources/OrderItemResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace EscolaLms\Cart\Http\Resources;

use EscolaLms\Auth\Traits\ResourceExtandable;
use EscolaLms\Cart\Models\OrderItem;
use EscolaLms\Cart\Models\Product;
use Illuminate\Http\Resources\Json\JsonResource;

class OrderItemResource extends JsonResource
{
use ResourceExtandable;

public function __construct(OrderItem $orderItem)
{
parent::__construct($orderItem);
}

protected function getOrderItem(): OrderItem
{
return $this->resource;
}

public function toArray($request): array
{
return self::apply([
'price' => $this->getOrderItem()->price,
'quantity' => $this->getOrderItem()->quantity,
'subtotal' => $this->getOrderItem()->subtotal,
'tax' => $this->getOrderItem()->tax,
'total' => $this->getOrderItem()->total,
'total_with_tax' => $this->getOrderItem()->total_with_tax,
'order_id' => $this->getOrderItem()->order_id,
'product_id' => $this->getOrderItem()->buyable_id,
'product_type' => $this->getOrderItem()->buyable_type,
$this->mergeWhen($this->getOrderItem()->buyable instanceof Product, fn () => ['product' => ProductResource::make($this->getOrderItem()->buyable)]),
], $this);
}
}
23 changes: 13 additions & 10 deletions src/Http/Resources/OrderResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace EscolaLms\Cart\Http\Resources;

use EscolaLms\Auth\Traits\ResourceExtandable;
use EscolaLms\Cart\Enums\OrderStatus;
use EscolaLms\Cart\Models\Order;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;

class OrderResource extends JsonResource
{
use ResourceExtandable;

public function __construct(Order $order)
{
parent::__construct($order);
Expand All @@ -18,18 +22,17 @@ protected function getOrder(): Order
return $this->resource;
}

/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
protected function getOrderItemsResourceCollection(): ResourceCollection
{
return OrderItemResource::collection($this->getOrder()->items);
}

public function toArray($request): array
{
return [
return self::apply([
'id' => $this->resource->getKey(),
'status' => OrderStatus::getName($this->status),
'items' => $this->items->toArray(),
'items' => $this->getOrderItemsResourceCollection(),
'total' => $this->total,
'subtotal' => $this->subtotal,
'tax' => $this->tax,
Expand All @@ -42,6 +45,6 @@ public function toArray($request)
'client_country' => $this->client_country,
'client_company' => $this->client_company,
'client_taxid' => $this->client_taxid,
];
], $this);
}
}
2 changes: 1 addition & 1 deletion src/Http/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function getProduct(): Product
return $this->resource;
}

public function toArray($request)
public function toArray($request): array
{
$user = $request ? $request->user() : Auth::user();
return [
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Resources/ProductableGenericResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getProductable(): Productable
return $this->resource;
}

public function toArray($request)
public function toArray($request): array
{
return [
'id' => $this->getProductable()->getKey(),
Expand Down
61 changes: 61 additions & 0 deletions src/Http/Swagger/Admin/ProductableAdminSwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use EscolaLms\Cart\Http\Requests\Admin\ProductableAttachRequest;
use EscolaLms\Cart\Http\Requests\Admin\ProductableDetachRequest;
use EscolaLms\Cart\Http\Requests\Admin\ProductableProductRequest;
use EscolaLms\Cart\Http\Requests\Admin\ProductableRegisteredListRequest;
use Illuminate\Http\JsonResponse;

Expand Down Expand Up @@ -167,4 +168,64 @@ public function detach(ProductableDetachRequest $request): JsonResponse;
* )
*/
public function registered(ProductableRegisteredListRequest $request): JsonResponse;

/**
* @OA\Get(
* path="/api/admin/productables/product",
* description="Get single product for this productable (if it exists)",
* tags={"Admin Product"},
* security={
* {"passport": {}},
* },
* @OA\Parameter(
* name="productable_type",
* description="Productable class",
* required=true,
* in="query",
* @OA\Schema(
* type="string",
* ),
* ),
* @OA\Parameter(
* name="productable_id",
* description="Productable id",
* required=true,
* in="query",
* @OA\Schema(
* type="integer",
* ),
* ),
* @OA\Response(
* response=200,
* description="successful operation",
* @OA\MediaType(
* mediaType="application/json",
* ),
* @OA\Schema(
* type="object",
* @OA\Property(
* property="data",
* type="object",
* @OA\Schema(ref="#/components/schemas/Product")
* ),
* @OA\Property(
* property="success",
* type="boolean"
* ),
* @OA\Property(
* property="message",
* type="string"
* )
* )
* ),
* @OA\Response(
* response=422,
* description="Bad request",
* @OA\MediaType(
* mediaType="application/json"
* )
* )
* )
*/
public function product(ProductableProductRequest $request): JsonResponse;
}
13 changes: 6 additions & 7 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* @property int|null $user_id
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read CartManager $cart_manager
* @property-read int $subtotal
* @property-read int $tax
* @property-read int $total
Expand All @@ -33,8 +34,6 @@
*/
class Cart extends BaseCart
{
private ?CartManager $cartManager = null;

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
Expand All @@ -45,24 +44,24 @@ public function items(): HasMany
return $this->hasMany(CartItem::class);
}

public function getCartManager(): CartManager
public function getCartManagerAttribute(): CartManager
{
return $this->cartManager ?? ($this->cartManager = app(ShopServiceContract::class)->cartManagerForCart($this));
return app(ShopServiceContract::class)->cartManagerForCart($this);
}

public function getSubtotalAttribute(): int
{
return $this->getCartManager()->subtotalInt();
return $this->cartManager->subtotalInt();
}

public function getTotalAttribute(): int
{
return $this->getCartManager()->total();
return $this->cartManager->total();
}

public function getTaxAttribute(?int $rate = null): int
{
return $this->getCartManager()->taxInt($rate);
return $this->cartManager->taxInt($rate);
}

public function getTotalWithTaxAttribute(?int $rate = null): int
Expand Down
7 changes: 7 additions & 0 deletions src/Models/CartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\Cart\Models;

use EscolaLms\Cart\Models\Contracts\Base\Taxable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Config;
use Treestoneit\ShoppingCart\Models\CartItem as BaseCartItem;

Expand All @@ -18,6 +19,7 @@
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $buyable
* @property-read \EscolaLms\Cart\Models\Cart $cart
* @property-read mixed $description
* @property-read float|int $extra_fees
* @property-read string $identifier
Expand All @@ -44,6 +46,11 @@
*/
class CartItem extends BaseCartItem
{
public function cart(): BelongsTo
{
return $this->belongsTo(Cart::class);
}

public function getTaxRateAttribute(?int $rate = null): int
{
if (!$rate && Config::get('shopping-cart.tax.mode') == 'flat') {
Expand Down
6 changes: 4 additions & 2 deletions src/Models/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use EscolaLms\Cart\QueryBuilders\OrderItemModelQueryBuilder;
use EscolaLms\Cart\Support\OrderItemCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* EscolaLms\Cart\Models\OrderItem
Expand Down Expand Up @@ -57,12 +59,12 @@ class OrderItem extends Model

protected $casts = ['options' => 'array'];

public function buyable()
public function buyable(): MorphTo
{
return $this->morphTo('buyable');
}

public function order()
public function order(): BelongsTo
{
return $this->belongsTo(Order::class);
}
Expand Down
Loading

0 comments on commit a8e54e4

Please sign in to comment.