Skip to content
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

List my products #134

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Dtos/PageDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace EscolaLms\Cart\Dtos;

use EscolaLms\Core\Dtos\Contracts\DtoContract;
use EscolaLms\Core\Dtos\Contracts\InstantiateFromRequest;
use Illuminate\Http\Request;

class PageDto implements DtoContract, InstantiateFromRequest
{
private ?int $skip;

private ?int $per_page;

public function __construct(?int $skip, ?int $per_page)
{
$this->skip = $skip;
$this->per_page = $per_page;
}

public function toArray(): array

Check warning on line 21 in src/Dtos/PageDto.php

View check run for this annotation

Codecov / codecov/patch

src/Dtos/PageDto.php#L21

Added line #L21 was not covered by tests
{
return [
'skip' => $this->getSkip(),
'per_page' => $this->getPerPage()
];

Check warning on line 26 in src/Dtos/PageDto.php

View check run for this annotation

Codecov / codecov/patch

src/Dtos/PageDto.php#L23-L26

Added lines #L23 - L26 were not covered by tests
}

public static function instantiateFromRequest(Request $request): self
{
$per_page = config('paginate.default.limit', 15);

if ($request->get('page')) {
return new self(
$request->get('skip', ($request->get('page') - 1) * $per_page),
$request->get('per_page', $per_page),

Check warning on line 36 in src/Dtos/PageDto.php

View check run for this annotation

Codecov / codecov/patch

src/Dtos/PageDto.php#L34-L36

Added lines #L34 - L36 were not covered by tests
);
}

return new self(
$request->get('skip', config('paginate.default.limit', 0)),
$request->get('per_page', $per_page),
);
}

public function getSkip(): ?int

Check warning on line 46 in src/Dtos/PageDto.php

View check run for this annotation

Codecov / codecov/patch

src/Dtos/PageDto.php#L46

Added line #L46 was not covered by tests
{
return $this->skip;

Check warning on line 48 in src/Dtos/PageDto.php

View check run for this annotation

Codecov / codecov/patch

src/Dtos/PageDto.php#L48

Added line #L48 was not covered by tests
}

public function getPerPage(): ?int
{
return $this->per_page;
}
}
44 changes: 44 additions & 0 deletions src/Dtos/ProductSearchMyCriteriaDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace EscolaLms\Cart\Dtos;

use EscolaLms\Core\Dtos\Contracts\DtoContract;
use EscolaLms\Core\Dtos\Contracts\InstantiateFromRequest;
use EscolaLms\Core\Dtos\CriteriaDto as BaseCriteriaDto;
use EscolaLms\Core\Repositories\Criteria\Primitives\EqualCriterion;
use EscolaLms\Core\Repositories\Criteria\Primitives\HasCriterion;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

class ProductSearchMyCriteriaDto extends BaseCriteriaDto implements DtoContract, InstantiateFromRequest
{
public static function instantiateFromRequest(Request $request): self
{
$criteria = new Collection();

$criteria->push(new HasCriterion('users', fn (Builder $query) => $query->where('user_id', $request->user()->getKey())));

if ($request->has('type')) {
$criteria->push(new EqualCriterion('type', $request->get('type')));
}

if ($request->has('active')) {
if ($request->boolean('active'))
$criteria->push(new HasCriterion('users', fn (Builder $query) => $query
->where('user_id', $request->user()->getKey())
->where(fn (Builder $query) => $query
->whereDate('end_date', '>=', Carbon::now())
->orWhereNull('end_date'))
)
);
else
$criteria->push(new HasCriterion('users', fn (Builder $query) => $query
->where('user_id', $request->user()->getKey())
->where('end_date', '<', Carbon::now())));
}

return new self($criteria);
}
}
9 changes: 9 additions & 0 deletions src/Http/Controllers/ProductApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace EscolaLms\Cart\Http\Controllers;

use EscolaLms\Cart\Http\Requests\ProductReadRequest;
use EscolaLms\Cart\Http\Requests\ProductSearchMyRequest;
use EscolaLms\Cart\Http\Requests\ProductSearchRequest;
use EscolaLms\Cart\Http\Resources\MyProductResource;
use EscolaLms\Cart\Http\Resources\ProductResource;
use EscolaLms\Cart\Http\Swagger\ProductSwagger;
use EscolaLms\Cart\Services\Contracts\ProductServiceContract;
Expand Down Expand Up @@ -35,4 +37,11 @@ public function read(ProductReadRequest $request): JsonResponse
{
return $this->sendResponseForResource(ProductResource::make($request->getProduct()), __('Product fetched'));
}

public function indexMy(ProductSearchMyRequest $request): JsonResponse
{
$results = $this->productService->searchMy($request->getCriteria(), $request->getPage(), $request->getOrder());

return $this->sendResponseForResource(MyProductResource::collection($results));
}
}
43 changes: 43 additions & 0 deletions src/Http/Requests/ProductSearchMyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace EscolaLms\Cart\Http\Requests;

use EscolaLms\Cart\Dtos\PageDto;
use EscolaLms\Cart\Dtos\ProductSearchMyCriteriaDto;
use EscolaLms\Cart\Enums\ProductType;
use EscolaLms\Cart\Models\Product;
use EscolaLms\Core\Dtos\OrderDto;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;

class ProductSearchMyRequest extends FormRequest
{
public function authorize(): bool
{
return Gate::allows('viewMy', Product::class);
}

public function rules(): array
{
return [
'type' => ['sometimes', Rule::in(ProductType::getValues())],
'active' => ['sometimes', 'boolean'],
];
}

public function getCriteria(): ProductSearchMyCriteriaDto
{
return ProductSearchMyCriteriaDto::instantiateFromRequest($this);
}

public function getPage(): PageDto
{
return PageDto::instantiateFromRequest($this);
}

public function getOrder(): OrderDto
{
return OrderDto::instantiateFromRequest($this);
}
}
76 changes: 76 additions & 0 deletions src/Http/Resources/MyProductResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace EscolaLms\Cart\Http\Resources;

use EscolaLms\Cart\Models\Product;
use EscolaLms\Cart\Models\ProductProductable;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

/**
* @OA\Schema(
* schema="MyProductResource",
* @OA\Property(
* property="id",
* type="integer",
* ),
* @OA\Property(
* property="type",
* type="string",
* ),
* @OA\Property(
* property="name",
* type="string",
* ),
* @OA\Property(
* property="is_active",
* type="boolean",
* ),
* @OA\Property(
* property="end_date",
* type="string",
* format="date-time"
* ),
* @OA\Property(
* property="status",
* type="string",
* ),
* @OA\Property(
* property="productables",
* type="array",
* @OA\Items(
* @OA\Property(
* property="productable_class",
* type="string"
* ),
* @OA\Property(
* property="productable_id",
* type="string",
* ),
* )
* )
* ),
* )
*
* @mixin Product
*/
class MyProductResource extends JsonResource
{
public function toArray($request): array
{
$productUserPivot = $this->users()->where('user_id', $request->user()->getKey())->first()?->pivot;

return [
'id' => $this->getKey(),
'type' => $this->type,
'name' => $this->name,
'is_active' => !$productUserPivot?->end_date || $productUserPivot?->end_date >= Carbon::now(),
'end_date' => $productUserPivot?->end_date,
'status' => $productUserPivot?->status,
'productables' => $this->productables->map(fn(ProductProductable $productProductable) => [
'productable_class' => $productProductable->productable_type,
'productable_id' => $productProductable->productable_id
]),
];
}
}
91 changes: 90 additions & 1 deletion src/Http/Swagger/ProductSwagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace EscolaLms\Cart\Http\Swagger;

use EscolaLms\Cart\Http\Requests\ProductListRequest;
use EscolaLms\Cart\Http\Requests\ProductReadRequest;
use EscolaLms\Cart\Http\Requests\ProductSearchMyRequest;
use EscolaLms\Cart\Http\Requests\ProductSearchRequest;
use Illuminate\Http\JsonResponse;

Expand Down Expand Up @@ -144,6 +144,95 @@ interface ProductSwagger
*/
public function index(ProductSearchRequest $request): JsonResponse;

/**
* @OA\Get(
* path="/api/products/my",
* description="Search my products",
* tags={"Products"},
* security={
* {"passport": {}},
* },
* @OA\Parameter(
* name="order_by",
* required=false,
* in="query",
* @OA\Schema(
* type="string",
* enum={"created_at","updated_at","id"}
* ),
* ),
* @OA\Parameter(
* name="order",
* required=false,
* in="query",
* @OA\Schema(
* type="string",
* enum={"ASC", "DESC"}
* ),
* ),
* @OA\Parameter(
* name="page",
* description="Pagination Page Number",
* required=false,
* in="query",
* @OA\Schema(
* type="integer",
* default=1,
* ),
* ),
* @OA\Parameter(
* name="per_page",
* description="Pagination Per Page",
* required=false,
* in="query",
* @OA\Schema(
* type="integer",
* default=15,
* ),
* ),
* @OA\Parameter(
* name="type",
* description="Type (`single`, `bundle`, `subscription`, `subscription-all-in`)",
* required=false,
* in="query",
* @OA\Schema(
* type="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="successful operation",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* type="object",
* @OA\Property(
* property="success",
* type="boolean"
* ),
* @OA\Property(
* property="data",
* type="array",
* @OA\Items(ref="#/components/schemas/MyProductResource")
* ),
* @OA\Property(
* property="message",
* type="string"
* )
* )
* ),
* ),
* @OA\Response(
* response=422,
* description="Bad request",
* @OA\MediaType(
* mediaType="application/json"
* )
* )
* )
*/
public function indexMy(ProductSearchMyRequest $request): JsonResponse;

/**
* @OA\Get(
* path="/api/products/{id}",
Expand Down
4 changes: 3 additions & 1 deletion src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ public function productables(): HasMany

public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'products_users')->using(ProductUser::class)->withPivot('quantity');
return $this->belongsToMany(User::class, 'products_users')
->using(ProductUser::class)
->withPivot(['quantity', 'end_date', 'status']);
}

public function tags(): MorphMany
Expand Down
5 changes: 5 additions & 0 deletions src/Policies/ProductPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function viewAny(User $user)
return $user->can(CartPermissionsEnum::LIST_ALL_PRODUCTS);
}

public function viewMy(User $user)
{
return $user->can(CartPermissionsEnum::LIST_PURCHASABLE_PRODUCTS);
}

public function viewPurchasable(?User $user = null)
{
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/Services/Contracts/ProductServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace EscolaLms\Cart\Services\Contracts;

use EscolaLms\Cart\Contracts\Productable;
use EscolaLms\Cart\Dtos\PageDto;
use EscolaLms\Cart\Dtos\ProductSearchMyCriteriaDto;
use EscolaLms\Cart\Dtos\ProductsSearchDto;
use EscolaLms\Cart\Models\Product;
use EscolaLms\Cart\Models\ProductProductable;
Expand Down Expand Up @@ -44,4 +46,5 @@ public function detachProductableFromUser(Productable $productable, User $user,

public function productableIsOwnedByUserThroughProduct(Productable $productable, User $user): bool;
public function canDetachProductableFromUser(Productable $productable, User $user): bool;
public function searchMy(ProductSearchMyCriteriaDto $dto, PageDto $pageDto, OrderDto $orderDto): LengthAwarePaginator;
}
Loading
Loading